1export const parseJsonToForm = (json) => {
4 const traverse = (obj, currentPath) => {
5 Object.entries(obj).forEach(([key, value]) => {
6 if (key.startsWith("ignore:")) return;
8 const cleanName = key.replace(/^(drop:|input:|textarea:)/, "");
9 const newPath = currentPath ? `${currentPath}.${cleanName}` : cleanName;
11 const newNameFormat = (name, path) => {
12 if (path.includes("verification")) {
13 const parts = path.split(".");
14 const index = parts.indexOf("verification");
15 if (index !== -1 && index + 1 < parts.length) {
16 return parts[index + 1];
22 const newName = newNameFormat(cleanName, newPath);
24 if (typeof value === "object" && !Array.isArray(value)) {
25 traverse(value, newPath);
26 } else if (key.startsWith("drop:") && Array.isArray(value)) {
27 let defaultValue = "";
28 let selectedValue = "";
31 value.forEach(option => {
32 if (typeof option === "string") {
33 if (option.startsWith("default:")) {
34 defaultValue = option.replace("default:", "");
35 options.push(option.replace("default:", ""));
36 } else if (option.startsWith("selected:")) {
37 selectedValue = option.replace("selected:", "");
38 options.push(option.replace("selected:", ""));
39 } else if (option.startsWith("opt:")) {
40 options.push(option.replace("opt:", ""));
50 defaultValue: selectedValue || defaultValue,
53 } else if (key.startsWith("input:")) {
58 defaultValue: typeof value === "string" ? value : "",
59 placeholder: typeof value === "string" ? value : ""
61 } else if (key.startsWith("textarea:")) {
66 defaultValue: typeof value === "string" ? value : "",
67 placeholder: typeof value === "string" ? value : ""
77export const injectFormValuesIntoJson = (json, fields) => {
78 const updatedJson = { ...json };
80 fields.forEach((field) => {
81 const pathParts = field.path.split(".");
82 let current = updatedJson;
84 for (let i = 0; i < pathParts.length - 1; i++) {
85 if (!current[pathParts[i]]) {
86 current[pathParts[i]] = {};
88 current = current[pathParts[i]];
91 const finalKey = pathParts[pathParts.length - 1];
92 const originalKey = Object.keys(current).find(k => k.endsWith(finalKey));
94 if (!originalKey) return;
96 if (field.type === "dropdown") {
97 const newOptions = current[originalKey]
98 .map(option => option.startsWith("selected:") ? option.replace("selected:", "opt:") : option)
99 .map(option => option === `opt:${field.defaultValue}` ? `selected:${field.defaultValue}` : option);
101 current[originalKey] = newOptions;
104 current[originalKey] = field.value;
106 current[originalKey] = field.defaultValue;