Skip to content

Commit bbf9e68

Browse files
Added functionality to create and download code.json using form data
Signed-off-by: Natalia Luzuriaga <[email protected]>
1 parent f00a7e9 commit bbf9e68

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

form.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<body>
2424
<div id="formio"></div>
2525
<script src="js/generateFormComponents.js"></script>
26+
<script src="js/formDataToJson.js"></script>
2627
<script type="text/javascript">
2728
createFormComponents().then(components => {
2829
Formio.createForm(document.getElementById('formio'), {
@@ -36,6 +37,12 @@
3637
},
3738
"components": components
3839
})
40+
.then(function(form) {
41+
form.on('submit', function(submission) {
42+
console.log("form submission here:", submission);
43+
downloadFile(submission.data);
44+
});
45+
})
3946
})
4047
.catch(error => {
4148
console.error("Error creating components:", error);

js/formDataToJson.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Retrieves file and returns as json object
2+
async function retrieveFile(filePath) {
3+
try {
4+
const response = await fetch(filePath);
5+
6+
if (!response.ok) {
7+
throw new Error('Network response was not ok');
8+
}
9+
// Returns file contents in a json format
10+
return await response.json();
11+
} catch (error) {
12+
console.error('There was a problem with the fetch operation:', error);
13+
return null;
14+
}
15+
}
16+
17+
// Populates blank code.json object with values from form
18+
function populateObject(data, blankObject) {
19+
for (let key in data) {
20+
console.log("current key", key)
21+
console.log("check if value exists", data.hasOwnProperty(key))
22+
if (blankObject.hasOwnProperty(key)) {
23+
if (typeof data[key] === 'object' && data[key] !== null) {
24+
// If object, recursively populate
25+
// TODO: test out for permissions and description objects
26+
populateObject(data[key], blankObject[key]);
27+
} else {
28+
// Add value
29+
blankObject[key] = data[key];
30+
}
31+
}
32+
}
33+
}
34+
35+
async function populateCodeJson(data) {
36+
// Path to the blank json file
37+
const filePath = 'schemas/template-code.json';
38+
39+
var codeJson = await retrieveFile(filePath);
40+
41+
if (codeJson) {
42+
populateObject(data, codeJson);
43+
} else {
44+
console.error("Failed to retrieve JSON data.");
45+
}
46+
47+
console.log("FINAL CODE JSON HERE", codeJson);
48+
return codeJson;
49+
}
50+
51+
// Creates code.json and triggers file download
52+
async function downloadFile(data){
53+
delete data.submit;
54+
const codeJson = await populateCodeJson(data);
55+
56+
const jsonString = JSON.stringify(codeJson, null, 2);
57+
const blob = new Blob([jsonString], { type: 'application/json' });
58+
59+
// Create anchor element and create download link
60+
const link = document.createElement('a');
61+
link.href = URL.createObjectURL(blob);
62+
link.download = "code.json";
63+
64+
// Trigger the download
65+
link.click();
66+
}
67+
68+
window.downloadFile = downloadFile;

0 commit comments

Comments
 (0)