Terarea  2
The automation project
Loading...
Searching...
No Matches
ParseJson.ts
Go to the documentation of this file.
1export interface FormField {
2 type: 'text' | 'dropdown' | 'input' | 'textarea';
3 name: string;
4 path: string;
5 value?: string;
6 placeholder?: string;
7 defaultValue?: string;
8 options?: string[];
9}
10
11export const parseJsonToForm = (json: Record<string, any>): FormField[] => {
12 const fields: FormField[] = [];
13
14 const traverse = (obj: Record<string, any>, currentPath: string) => {
15 Object.entries(obj).forEach(([key, value]) => {
16 if (key.startsWith("ignore:")) return;
17
18 const cleanName = key.replace(/^(drop:|input:|textarea:)/, "");
19 const newPath = currentPath ? `${currentPath}.${cleanName}` : cleanName;
20
21 const newNameFormat = (name: string, path: string) => {
22 if (path.includes("verification")) {
23 const parts = path.split(".");
24 const index = parts.indexOf("verification");
25 if (index !== -1 && index + 1 < parts.length) {
26 return parts[index + 1];
27 }
28 }
29 return name;
30 };
31
32 const newName = newNameFormat(cleanName, newPath);
33
34 if (typeof value === "object" && !Array.isArray(value)) {
35 traverse(value, newPath);
36
37 } else if (key.startsWith("drop:") && Array.isArray(value)) {
38 let defaultValue = "";
39 let selectedValue = "";
40 const options: string[] = [];
41
42 value.forEach(option => {
43 if (typeof option === "string") {
44 if (option.startsWith("default:")) {
45 defaultValue = option.replace("default:", "");
46 options.push(option.replace("default:", ""));
47 } else if (option.startsWith("selected:")) {
48 selectedValue = option.replace("selected:", "");
49 options.push(option.replace("selected:", ""));
50 } else if (option.startsWith("opt:")) {
51 options.push(option.replace("opt:", ""));
52 }
53 }
54 });
55
56 fields.push({
57 type: "dropdown",
58 name: newName,
59 path: newPath,
60 options,
61 defaultValue: selectedValue || defaultValue,
62 });
63
64 } else if (key.startsWith("input:")) {
65 fields.push({
66 type: "input",
67 name: cleanName,
68 path: newPath,
69 defaultValue: typeof value === "string" ? value : "",
70 value: "",
71 placeholder: typeof value === "string" ? value : ""
72 });
73 } else if (key.startsWith("textarea:")) {
74 fields.push({
75 type: "textarea",
76 name: cleanName,
77 path: newPath,
78 defaultValue: typeof value === "string" ? value : "",
79 value: "",
80 placeholder: typeof value === "string" ? value : ""
81 });
82 }
83 });
84 };
85
86 traverse(json, "");
87 return fields;
88};
89
90export const injectFormValuesIntoJson = (json: Record<string, any>, fields: FormField[]) => {
91 const updatedJson = { ...json };
92
93 fields.forEach((field: any) => {
94 const pathParts = field.path.split(".");
95 let current = updatedJson;
96
97 for (let i = 0; i < pathParts.length - 1; i++) {
98 if (!current[pathParts[i]]) {
99 current[pathParts[i]] = {};
100 }
101 current = current[pathParts[i]];
102 }
103
104 const finalKey = pathParts[pathParts.length - 1];
105 const originalKey = Object.keys(current).find(k => k.endsWith(finalKey));
106
107 if (!originalKey) return;
108
109 if (field.type === "dropdown") {
110 const newOptions = current[originalKey]
111 .map((option: any) => option.startsWith("selected:") ? option.replace("selected:", "opt:") : option)
112 .map((option: any) => option === `opt:${field.defaultValue}` ? `selected:${field.defaultValue}` : option);
113
114 current[originalKey] = newOptions;
115 } else {
116 if (field.value) {
117 current[originalKey] = field.value;
118 } else {
119 current[originalKey] = field.defaultValue;
120 }
121 }
122 });
123
124 return updatedJson;
125};