forked from SakshamHaryana-SE/enketo
-
Notifications
You must be signed in to change notification settings - Fork 29
/
form-controller.js
328 lines (290 loc) · 13.3 KB
/
form-controller.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { xml2json } from './xml2json';
import settings from './settings';
import imageCompression from 'browser-image-compression';
const options = {
maxSizeMB: 0.5,
maxWidthOrHeight: 840,
useWebWorker: true,
}
// import { config } from '../../../../app/models/config-model';
// const config = require( '../../../../app/models/config-model' ).server;
// console.log(config)
// const config = {
// 'formManagerBaseURI': 'http:https://localhost:3002/'
// };
export class FormController {
/* formSpec = {
"submissionURL": "http:https://esamwad.samagra.io/api/v4/form/submit",
"name": "SampleForm",
"messageOnSubmit": "Form submitted successfully",
"messageOnFailure": "Form submission failed",
"isSuccess": "async (formData) => { console.log(formData); }",
"onFormSuccess": "async (formData) => { console.log(formData); }",
"onFormFailure": "async (formData) => { console.log(formData); }",
"nextFormOnSuccess": "formID2",
"nextFormOnFailure": "formID3"
}
*/
/* formSpec = {
"skipOnSuccessMessage": true,
"prefill": {},
"submissionURL": "http:https://esamwad.samagra.io/api/v4/form/submit",
"name": "SampleForm",
"successCheck": "async (formData) => { console.log('From isSuccess', formData.getElementsByTagName('reg_no')[0].textContent); return formData.getElementsByTagName('reg_no')[0].textContent === 'registration123'; }",
"onSuccess": {
"notificationMessage": "Form submitted successfully or not Maybe",
"sideEffect": "async (formData) => { return JSON.parse(decodeURIComponent('%7B%0A%20%20%20%20%20%20%20%20%22name%22%3A%20%22DEVA%22%2C%0A%20%20%20%20%20%20%20%20%22batch%22%3A%20%222021-2023%22%2C%0A%20%20%20%20%20%20%20%20%22id%22%3A%208%2C%0A%20%20%20%20%20%20%20%20%22DOB%22%3A%20%222005-03-04%22%2C%0A%20%20%20%20%20%20%20%20%22affiliationType%22%3A%20%22NCVT%22%2C%0A%20%20%20%20%20%20%20%20%22registrationNumber%22%3A%20%22ICA211021569832%22%2C%0A%20%20%20%20%20%20%20%20%22tradeName%22%3A%20%22Electrician%22%2C%0A%20%20%20%20%20%20%20%20%22iti%22%3A%207%2C%0A%20%20%20%20%20%20%20%20%22industry%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%22itiByIti%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22id%22%3A%207%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22GITI%20Nagina%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%22industryByIndustry%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22id%22%3A%201%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Kaushal%20Bhawan%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22latitude%22%3A%2030.695753%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22longitude%22%3A%2076.872025%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22schedules%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22is_industry%22%3A%20true%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D')); }",
"next": {
"type": "form",
"id": "form2"
}
},
"onFailure": {
"message": "Form submission failed",
"sideEffect": "async (formData) => { console.log(formData); }",
"next": {
"type": "url",
"id": "google"
}
}
}
*/
constructor() {
/* Valid States:
INITIALIZED
FORM_SUCCESS
FORM_FAILURE
ON_SUBMIT_SUCCESS
ON_SUBMIT_FAILURE
ON_FORM_SUCCESS_COMPLETED
ON_FORM_FAILURE_COMPLETED
*/
this._state = 'INITIALIZED';
this._message = '';
// this.formSpec = formSpec;
this._parser = new DOMParser();
this.formFiles = null;
// this.formSpec.isSuccessExecute = () => this.executeMethod(this.formSpec.successCheck);
// this.formSpec.onFormSuccessExecute = () => this.executeMethod(this.formSpec.onSuccess.sideEffect);
// this.formSpec.onFormFailureExecute = () => this.executeMethod(this.formSpec.onFailure.sideEffect);
}
async executeMethod(functionString) {
// execute method string
if (functionString) {
//TODO: fix this with a sandbox
return (0, eval)(functionString)(this.formData);
} else {
throw new Error('No function string provided');
}
}
get state() {
return this._state;
}
findKey(obj, val, keyToFind, currentDotNotation) {
if (typeof obj === 'object') {
for (const key in obj) {
if (obj[key] === val && key === keyToFind) {
return currentDotNotation;
} else {
const result = this.findKey(obj[key], val, keyToFind, currentDotNotation + '.' + key);
if (result !== null) return result;
}
}
}
return null;
}
async uploadFile(data) {
console.log("Ab dekh data:", data)
if (data) {
const fd = new FormData();
var newFile = new File([data], data.name, { type: data.type });
// Compressing this file
newFile = await imageCompression(newFile, options)
fd.append('file', newFile, data.name);
const response = await fetch(`${settings.formManagerBaseURI}/form/uploadFile`, {
method: 'POST',
body: fd
}).then(s => {
return s.json();
}).catch(e => {
console.log(e);
return null;
});
return response ? response.fileURL : null;
}
return null;
}
set(obj, path, value) {
if (Object(obj) !== obj) return obj; // When obj is not an object
// If not yet an array, get the keys from the string-path
if (!Array.isArray(path)) path = path.toString().match(/[^.[\]]+/g) || [];
path.slice(0, -1).reduce((a, c, i) => // Iterate all of them except the last one
Object(a[c]) === a[c] // Does the key exist and is its value an object?
// Yes: then follow that path
? a[c]
// No: create the key. Is the next key a potential array-index?
: a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1]
? [] // Yes: assign a new array object
: {}, // No: assign a new plain object
obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
return obj; // Return the top-level object to allow chaining
}
// async processForm(formData, formFiles) {
// const doc = this._parser.parseFromString(formData, 'text/xml');
// // Uploading images to Minio and replacing in formData
// for (let i = 0; i < formFiles.length; i++) {
// let minioUri = await this.uploadFile(formFiles[i]);
// console.log(minioUri, formFiles[i].name);
// if (minioUri) {
// formData = formData.replace(formFiles[i].name, minioUri);
// }
// }
// console.log("Updated FormData:", formData);
// const parseRes = await fetch(`${settings.formManagerBaseURI}/parse`, {
// method: "POST",
// body: JSON.stringify({ xml: formData.toString() }),
// headers: {
// "Content-type": "application/json; charset=UTF-8"
// }
// }).then(res => res.json()).catch(e => {
// this._state = 'FORM_FAILURE_OFFLINE';
// this.formFiles = formFiles;
// window.parent.postMessage(JSON.stringify({
// state: this._state,
// formDataXml: formData,
// formFilesXml: formFiles
// }), '*');
// });
// if (parseRes == undefined)
// return Promise.resolve({
// status: "offline",
// message: "You are oflline. Your form data has been saved. Please re-submit using the submit button once back online"
// });
// this.formData = parseRes.data;
// // for (let i = 0; i < formFiles.length; i++) {
// // const file = formFiles[i];
// // const fileURL = await this.uploadFile(file);
// // // console.log({ fileURL });
// // console.log(this.findKey)
// // const kk = this.findKey(this.formData, file.name, '$t', '');
// // this.formData = this.set(this.formData, kk.substring(1), fileURL);
// // }
// if (await this.formSpec.isSuccessExecute() === true) {
// this._state = 'FORM_SUCCESS';
// this._onFormSuccessData = await this.formSpec.onFormSuccessExecute();
// this._state = 'ON_FORM_SUCCESS_COMPLETED';
// this.nextForm = this.formSpec.onSuccess.next;
// this._message = this.formSpec.messageOnSuccess;
// } else {
// this._state = 'FORM_FAILURE';
// this._onFormFailureData = this.formSpec.onFormFailureExecute();
// this._state = 'ON_FORM_FAILURE_COMPLETED';
// this.nextForm = this.formSpec.onFailure.next;
// this._message = this.formSpec.messageOnFailure;
// }
// return Promise.resolve({
// state: this._state,
// status: this._state.includes('FAILURE') ? 'failure' : 'success',
// message: this._message,
// nextForm: this.nextForm,
// onFormSuccessData: this._onFormSuccessData,
// onFormFailureData: this._onFormFailureData
// });
// }
async processFormNew(formData, formFiles) {
const doc = this._parser.parseFromString(formData, 'text/xml');
// Uploading images to Minio and replacing in formData
for (let i = 0; i < formFiles.length; i++) {
let minioUri = await this.uploadFile(formFiles[i]);
console.log(minioUri, formFiles[i].name);
if (minioUri) {
formData = formData.replace(formFiles[i].name, minioUri);
}
}
console.log("Updated FormData:", formData);
const parseRes = await fetch(`${settings.formManagerBaseURI}/parse`, {
method: "POST",
body: JSON.stringify({ xml: formData.toString() }),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
}).then(res => res.json()).catch(e => {
this._state = 'FORM_FAILURE_OFFLINE';
this.formFiles = formFiles;
window.parent.postMessage(JSON.stringify({
state: this._state,
formDataXml: formData,
formFilesXml: formFiles
}), '*');
});
if (parseRes == undefined) {
// this._state = 'FORM_FAILURE';
// this._onFormFailureData = this.formSpec.onFormFailureExecute();
this._state = 'ON_FORM_FAILURE_COMPLETED';
// this.nextForm = this.formSpec.onFailure.next;
// this._message = this.formSpec.messageOnFailure;
} else {
this.formData = parseRes.data;
// this._state = 'FORM_SUCCESS';
// this._onFormSuccessData = await this.formSpec.onFormSuccessExecute();
this._state = 'ON_FORM_SUCCESS_COMPLETED';
// this.nextForm = this.formSpec.onSuccess.next;
// this._message = this.formSpec.messageOnSuccess;
}
return Promise.resolve({
state: this._state,
status: this._state.includes('FAILURE') ? 'failure' : 'success',
message: this._message,
nextForm: this.nextForm,
onFormSuccessData: this._onFormSuccessData,
onFormFailureData: this._onFormFailureData
});
}
async broadcastFormData() {
// broadcast form data to parent window
window.parent.postMessage(JSON.stringify({
nextForm: this.nextForm,
formData: this.formData,
onSuccessData: this._onFormSuccessData,
onFailureData: this._onFormFailureData,
state: this._state
}), '*');
}
async broadcastFormDataUpdate(xml, fileURLs) {
console.log("Broadcasting file update")
// broadcast form data to parent window
window.parent.postMessage(JSON.stringify({
formData: xml,
formXML: xml,
state: this._state,
fileURLs: fileURLs
}), '*');
}
async broadcastFileRemoveUpdate(xml, fileURLs) {
console.log("Broadcasting file update")
// broadcast form data to parent window
window.parent.postMessage(JSON.stringify({
formData: xml,
formXML: xml,
state: this._state,
fileURLs: fileURLs
}), '*');
}
async submit() {
// submit form data to server
const response = await fetch(this.formSpec.submissionURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.formData)
});
const data = await response.json();
if (data.status === 'success') {
this._state = 'ON_SUBMIT_SUCCESS';
} else {
this._state = 'ON_SUBMIT_FAILURE';
}
return data;
}
}