Terarea  2
The automation project
Loading...
Searching...
No Matches
parseJson.js
Go to the documentation of this file.
1export const parseJsonToForm = (json) => {
2 const fields = [];
3
4 const traverse = (obj, currentPath) => {
5 Object.entries(obj).forEach(([key, value]) => {
6 if (key.startsWith("ignore:")) return;
7
8 const cleanName = key.replace(/^(drop:|input:|textarea:)/, "");
9 const newPath = currentPath ? `${currentPath}.${cleanName}` : cleanName;
10
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];
17 }
18 }
19 return name;
20 };
21
22 const newName = newNameFormat(cleanName, newPath);
23
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 = "";
29 const options = [];
30
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:", ""));
41 }
42 }
43 });
44
45 fields.push({
46 type: "dropdown",
47 name: newName,
48 path: newPath,
49 options,
50 defaultValue: selectedValue || defaultValue,
51 });
52
53 } else if (key.startsWith("input:")) {
54 fields.push({
55 type: "input",
56 name: cleanName,
57 path: newPath,
58 defaultValue: typeof value === "string" ? value : "",
59 placeholder: typeof value === "string" ? value : ""
60 });
61 } else if (key.startsWith("textarea:")) {
62 fields.push({
63 type: "textarea",
64 name: cleanName,
65 path: newPath,
66 defaultValue: typeof value === "string" ? value : "",
67 placeholder: typeof value === "string" ? value : ""
68 });
69 }
70 });
71 };
72
73 traverse(json, "");
74 return fields;
75};
76
77export const injectFormValuesIntoJson = (json, fields) => {
78 const updatedJson = { ...json };
79
80 fields.forEach((field) => {
81 const pathParts = field.path.split(".");
82 let current = updatedJson;
83
84 for (let i = 0; i < pathParts.length - 1; i++) {
85 if (!current[pathParts[i]]) {
86 current[pathParts[i]] = {};
87 }
88 current = current[pathParts[i]];
89 }
90
91 const finalKey = pathParts[pathParts.length - 1];
92 const originalKey = Object.keys(current).find(k => k.endsWith(finalKey));
93
94 if (!originalKey) return;
95
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);
100
101 current[originalKey] = newOptions;
102 } else {
103 if (field.value) {
104 current[originalKey] = field.value;
105 } else {
106 current[originalKey] = field.defaultValue;
107 }
108 }
109 });
110
111 return updatedJson;
112};