Skip to content

Commit 59e98d4

Browse files
Added functionality to create code.json using schema
Signed-off-by: Natalia Luzuriaga <natalia.luzuriaga@cms.hhs.gov>
1 parent df09c7d commit 59e98d4

1 file changed

Lines changed: 37 additions & 34 deletions

File tree

js/formDataToJson.js

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,61 @@ async function retrieveFile(filePath) {
1414
}
1515
}
1616

17-
// Populates blank code.json object with values from form
18-
function populateObject(data, blankObject) {
19-
20-
for (const key in data) {
21-
if (blankObject.hasOwnProperty(key)) {
22-
console.log(`${key}: ${data[key]}`);
23-
24-
if(typeof data[key] === "object" && isMultiSelect(data[key])) {
25-
blankObject[key] = getSelectedOptions(data[key]);
26-
}
27-
else {
28-
blankObject[key] = data[key];
29-
}
30-
31-
}
32-
}
33-
}
34-
3517
function isMultiSelect(obj) {
36-
for (const key in obj) {
37-
if (typeof obj[key] !== 'boolean') {
38-
return false;
39-
}
18+
for (const key in obj) {
19+
if (typeof obj[key] !== 'boolean') {
20+
return false;
4021
}
41-
return true; // Return true if all values are booleans
22+
}
23+
return true; // Returns true if all values are booleans
4224
}
4325

4426
// Convert from dictionary to array
4527
function getSelectedOptions(options) {
46-
let selectedOptions = [];
28+
let selectedOptions = [];
29+
30+
for (let key in options) {
31+
if(options[key]) {
32+
selectedOptions.push(key);
33+
}
34+
}
35+
return selectedOptions;
36+
}
37+
38+
// Populates fields with form data and follows field order in schema
39+
function populateObject(data, schema) {
40+
let reorderedObject = {}
41+
42+
const fields = Object.keys(schema.properties.items);
4743

48-
for (let key in options) {
49-
if(options[key]) {
50-
selectedOptions.push(key);
44+
for (const key of fields) {
45+
let value = data[key];
46+
47+
// Adjusts value accordingly if multi-select field
48+
if((typeof value === "object" && isMultiSelect(value))) {
49+
value = getSelectedOptions(value);
5150
}
51+
52+
reorderedObject[key] = value;
5253
}
53-
return selectedOptions;
54+
55+
return reorderedObject;
5456
}
5557

5658
async function populateCodeJson(data) {
57-
// Path to the blank json file
58-
const filePath = "schemas/template-code.json";
59+
const filePath = "schemas/schema-0.0.0.json";
5960

60-
let codeJson = await retrieveFile(filePath);
61+
// Retrieves schema with fields in correct order
62+
const schema = await retrieveFile(filePath);
63+
let codeJson = {};
6164

62-
if (codeJson) {
63-
populateObject(data, codeJson);
65+
// Populates fields with form data
66+
if (schema) {
67+
codeJson = populateObject(data, schema);
6468
} else {
6569
console.error("Failed to retrieve JSON data.");
6670
}
6771

68-
console.log("FINAL CODE JSON HERE", codeJson);
6972
return codeJson;
7073
}
7174

0 commit comments

Comments
 (0)