@@ -14,58 +14,62 @@ 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-
3517function 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
4527function 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
39+ function populateObject ( data , schema ) {
40+ let reorderedObject = { }
41+
42+ // Array of fields following proper order of fields in schema
43+ const fields = Object . keys ( schema . properties . items ) ;
4744
48- for ( let key in options ) {
49- if ( options [ key ] ) {
50- selectedOptions . push ( key ) ;
45+ for ( const key of fields ) {
46+ let value = data [ key ] ;
47+
48+ // Adjusts value accordingly if multi-select field
49+ if ( ( typeof value === "object" && isMultiSelect ( value ) ) ) {
50+ value = getSelectedOptions ( value ) ;
5151 }
52+
53+ reorderedObject [ key ] = value ;
5254 }
53- return selectedOptions ;
55+
56+ return reorderedObject ;
5457}
5558
5659async function populateCodeJson ( data ) {
57- // Path to the blank json file
58- const filePath = "schemas/template-code.json" ;
60+ const filePath = "schemas/schema-0.0.0.json" ;
5961
60- let codeJson = await retrieveFile ( filePath ) ;
62+ // Retrieves schema with fields in correct order
63+ const schema = await retrieveFile ( filePath ) ;
64+ let codeJson = { } ;
6165
62- if ( codeJson ) {
63- populateObject ( data , codeJson ) ;
66+ // Populates fields with form data
67+ if ( schema ) {
68+ codeJson = populateObject ( data , schema ) ;
6469 } else {
6570 console . error ( "Failed to retrieve JSON data." ) ;
6671 }
6772
68- console . log ( "FINAL CODE JSON HERE" , codeJson ) ;
6973 return codeJson ;
7074}
7175
0 commit comments