forked from SakshamHaryana-SE/enketo
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (126 loc) · 5.25 KB
/
Copy pathindex.js
File metadata and controls
140 lines (126 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import React, { useState, useEffect, useRef } from 'react';
import styles from './index.module.css';
import beautify from "xml-beautifier";
import { getPrefillXML, saveFormSubmission } from '../../api';
const ENKETO_URL = process.env.REACT_APP_ENKETO_URL
const FORM_MANAGER_URL = process.env.REACT_APP_FORM_MANAGER_URL
const HASURA_URL = process.env.REACT_APP_HASURA_URL
const GenericForm = (props) => {
const { selectedFlow, setSelectedFlow } = props;
const formSpec = require(`../../${selectedFlow.config}`);
const [formData, setFormData] = useState("");
const [formDataJson, setFormDataJSON] = useState("");
const [isXml, setIsXml] = useState(false);
const startingForm = formSpec.startingForm;
const [fileUrls, setFileUrls] = useState({});
const [formId, setFormId] = useState(startingForm);
const [encodedFormSpec, setEncodedFormSpec] = useState(encodeURI(JSON.stringify(formSpec.forms[formId])));
const [onFormSuccessData, setOnFormSuccessData] = useState(undefined);
const [onFormFailureData, setOnFormFailureData] = useState(undefined);
const [encodedFormURI, setEncodedFormURI] = useState('');
const formSubmitted = useRef(false);
useEffect(() => {
// Manage onNext
window.addEventListener('message', async function (e) {
const data = typeof e.data === "string" ? JSON.parse(e.data) : e.data;
try {
const { nextForm, formData, onSuccessData, onFailureData } = data;
console.log("data--->", data)
if (data.fileURLs) setFileUrls(data.fileURLs)
if (data?.state != "ON_FORM_SUCCESS_COMPLETED" && formData) {
setFormData(beautify(formData))
let jsonRes = await parseFormData(formData);
if (jsonRes) setFormDataJSON(JSON.stringify(jsonRes, null, 2));
}
if (data?.state == "ON_FORM_SUCCESS_COMPLETED" && selectedFlow.submitToHasura && !formSubmitted.current) {
formSubmitted.current = true;
await saveFormSubmission({
form_data: formData,
form_name: formSpec.startingForm,
});
formSubmitted.current = false;
}
if (nextForm?.type === 'form') {
setFormId(nextForm.id);
setOnFormSuccessData(onSuccessData);
setOnFormFailureData(onFailureData);
setEncodedFormSpec(encodeURI(JSON.stringify(formSpec.forms[formId])));
let newForm = await getPrefillXML(nextForm.id, formSpec.forms[nextForm.id].onFormSuccess);
console.log("new FORM", newForm)
setEncodedFormURI(newForm);
} else if (nextForm?.type == 'url') {
window.location.href = nextForm.url;
}
}
catch (e) {
console.log(e)
}
});
}, []);
const handleFormView = async (e) => {
setIsXml(e)
if (e) {
let jsonRes = await parseFormData(formData);
if (jsonRes) setFormDataJSON(JSON.stringify(jsonRes, null, 2));
}
}
const parseFormData = async (formData) => {
let jsonRes = await fetch(`${FORM_MANAGER_URL}/parse`, {
method: 'POST',
body: JSON.stringify({
xml: formData,
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
},
});
jsonRes = await jsonRes.json();
return jsonRes?.data;
}
const getForm = async () => {
let prefilledForm = await getPrefillXML(startingForm, formSpec.forms[formId].onFormSuccess);
setEncodedFormURI(prefilledForm)
}
useEffect(() => {
getForm();
}, [])
return (
<div className={styles.container}>
<div className={styles.header + ' animate__animated animate__slideInLeft animate__faster'}>
<div onClick={() => setSelectedFlow({})}>Go Back</div>
<div>Workflow /{selectedFlow.name}</div>
</div>
{Object.keys(fileUrls)?.length > 0 && <div className={styles.imageLinks}>
<p>Uploaded Images</p>
{Object.keys(fileUrls)?.map(el => <a href={fileUrls[el].url} target="_blank">{fileUrls[el].url}</a>)}
</div>
}
{
selectedFlow.offline && <p className='animate__animated animate__fadeIn' style={{ color: '#fff', fontSize: '1.5rem' }}>Disable internet and try submitting the form</p>
}
{
selectedFlow.submitToHasura && <p className='animate__animated animate__fadeIn' style={{ color: '#fff', fontSize: '1.5rem' }}>Submit the form and check <a style={{ color: '#ffc119' }} target="_blank" href={`${HASURA_URL}`}>Hasura</a></p>
}
<div className={styles.formContainer}>
<iframe title='current-form'
className={styles.odkForm}
src={
`${ENKETO_URL}/preview?formSpec=${encodedFormSpec}&xform=${encodedFormURI}`
}
/>
<div className={styles.jsonResponse}>
<div className={styles.toggleBtn}>
<label className={styles.switch}>
<input type="checkbox" value={isXml} onChange={e => handleFormView(e.target.checked)} />
<span className={styles.slider}></span>
</label>
{isXml ? <span className='animate__animated animate__fadeIn'>XML</span> : <span className='animate__animated animate__fadeIn'>JSON</span>}
</div>
<textarea value={!isXml ? formData : formDataJson} className={styles.formText}>
</textarea>
</div>
</div>
</div>
);
}
export default GenericForm;