Skip to content

Commit a0e510b

Browse files
ChakshuGautamAnsh-Sarkarchoxxamit-s19
authored
File upload (#11)
* File upload * fix: added formBaseURIManager to config Signed-off-by: Ansh Sarkar <anshsark18@gmail.com> * Forms: DST cancel & update forms updated FormManager: sample.env file added gitignore: ignoring IDE files * Forms: removed style page * Forms: updated Creation form for DST * Forms: updated Creation form for DST - fixed the pagination class issue * Changes for enketo ports * Changes in DST MC Creaction XML form * Revert "Changes in DST MC Creaction XML form" * Revert "Revert "Changes in DST MC Creaction XML form"" Signed-off-by: Ansh Sarkar <anshsark18@gmail.com> Co-authored-by: Ansh Sarkar <anshsark18@gmail.com> Co-authored-by: Abhishek Mishra <abhishek@luezoid.com> Co-authored-by: Amit Sharma <coutamit.s19@gmail.com> Co-authored-by: Amit Sharma <46066481+amit-s19@users.noreply.github.com>
1 parent 5c2672f commit a0e510b

File tree

18 files changed

+21700
-20096
lines changed

18 files changed

+21700
-20096
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.turbo
2-
node_modules
2+
node_modules
3+
.idea

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"build": "turbo run build",
88
"docker-cleanup": "docker rm -f enketo-redis-main enketo-redis-cache",
99
"enketo-core": "cd packages/enketo-core && npm i && npm start",
10-
"enketo-express": "cd packages/enketo-express && docker run --name enketo-redis-main -p 6379:6379 -d redis && docker run --name enketo-redis-cache -p 6380:6379 -d redis && npm i && npm i -g grunt && grunt develop",
10+
"enketo-express": "cd packages/enketo-express && docker run --name enketo-redis-main -p 6381:6379 -d redis && docker run --name enketo-redis-cache -p 6382:6379 -d redis && npm i && npm i -g grunt && grunt develop",
1111
"form-manager": "cd packages/form-manager && npm i && npm start",
1212
"wrapper": "cd apps/wrapper && npm i && npm start",
1313
"concurrent-packages": "concurrently \"npm run enketo-core\" \"npm run enketo-express\" \"npm run form-manager\"",
@@ -16,8 +16,8 @@
1616
"start-redis-cache": "docker run --name enketo-redis-cache -p 6380:6379 -d redis",
1717
"start-redis-main": "docker run --name enketo-redis-main -p 6379:6379 -d redis",
1818
"start-ec": "cd enketo-core && npm start",
19-
"start-ee": "cd enketo-express && grunt develop",
20-
"start-fm": "cd form-manager && npm run start:dev",
19+
"start-ee": "cd packages/enketo-express && grunt develop",
20+
"start-fm": "cd packages/form-manager && npm run start:dev",
2121
"start-web": "cd wrapper && npm start"
2222
},
2323
"workspaces": [

packages/enketo-express/app/models/config-model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,8 @@ module.exports = {
329329
validateContinuously: config[ 'validate continuously' ],
330330
validatePage: config[ 'validate page' ],
331331
swipePage: config[ 'swipe page' ],
332-
textMaxChars: config[ 'text field character limit' ]
332+
textMaxChars: config[ 'text field character limit' ],
333+
formManagerBaseURI : config['formManagerBaseURI']
333334
},
334335
getThemesSupported
335336
};

packages/enketo-express/config/default-config.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"app name": "Enketo Smart Paper for KoBoCAT",
3-
"port": "8005",
3+
"port": "8065",
44
"offline enabled": true,
55
"id length": 8,
66
"linked form and data server": {
@@ -91,12 +91,12 @@
9191
"redis": {
9292
"main": {
9393
"host": "0.0.0.0",
94-
"port": "6379",
94+
"port": "6381",
9595
"password": null
9696
},
9797
"cache": {
9898
"host": "0.0.0.0",
99-
"port": "6380",
99+
"port": "6382",
100100
"password": null
101101
}
102102
},
@@ -109,5 +109,6 @@
109109
"validate continuously": false,
110110
"validate page": true,
111111
"payload limit": "100kb",
112-
"text field character limit": 2000
112+
"text field character limit": 2000,
113+
"formManagerBaseURI": "https://fm.dst.samagra.io"
113114
}

packages/enketo-express/public/js/src/module/connection.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,13 @@ async function _uploadBatch(recordBatch, record) {
152152
controller.abort();
153153
}, settings.timeout);
154154

155-
156155
function getMeta(metaName) {
157156
return document.querySelector(`meta[name=${metaName}]`).content;
158157
}
159-
console.log('REC', recordBatch.formData);
160158

161159
const formSpec = getMeta('formSpec');
162160
const formController = new FormController(JSON.parse(formSpec));
163-
const response = await formController.processForm(record.xml);
161+
const response = await formController.processForm(record.xml, record.files);
164162
// TODO: transforrm the form response
165163
// TODO: submit the form
166164
formController.broadcastFormData();

packages/enketo-express/public/js/src/module/form-controller.js

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import { xml2json } from "./xml2json";
1+
import { xml2json } from './xml2json';
2+
3+
import settings from './settings';
4+
5+
// import { config } from '../../../../app/models/config-model';
6+
7+
// const config = require( '../../../../app/models/config-model' ).server;
8+
// console.log(config)
9+
10+
// const config = {
11+
// 'formManagerBaseURI': 'http://localhost:3002/'
12+
// };
213

314
export class FormController {
415

@@ -74,13 +85,68 @@ export class FormController {
7485
return this._state;
7586
}
7687

77-
async processForm(formData) {
88+
findKey(obj, val, keyToFind, currentDotNotation) {
89+
if (typeof obj === 'object') {
90+
for (const key in obj) {
91+
if (obj[key] === val && key === keyToFind) {
92+
return currentDotNotation;
93+
} else {
94+
const result = this.findKey(obj[key], val, keyToFind, currentDotNotation + '.' + key);
95+
if (result !== null) return result;
96+
}
97+
}
98+
}
99+
100+
return null;
101+
}
102+
103+
async uploadFile(data) {
104+
const fd = new FormData();
105+
var newFile = new File([data], data.name, { type: data.type });
106+
console.log(newFile);
107+
fd.append('file', newFile, data.name);
108+
const response = await fetch(settings.formManagerBaseURI + '/form/uploadFile', {
109+
method: 'POST',
110+
body: fd
111+
}).then(s => {
112+
return s.json();
113+
}).catch(e => {
114+
console.log(e);
115+
});
116+
117+
return response.fileURL;
118+
}
119+
120+
set(obj, path, value) {
121+
if (Object(obj) !== obj) return obj; // When obj is not an object
122+
// If not yet an array, get the keys from the string-path
123+
if (!Array.isArray(path)) path = path.toString().match(/[^.[\]]+/g) || [];
124+
path.slice(0, -1).reduce((a, c, i) => // Iterate all of them except the last one
125+
Object(a[c]) === a[c] // Does the key exist and is its value an object?
126+
// Yes: then follow that path
127+
? a[c]
128+
// No: create the key. Is the next key a potential array-index?
129+
: a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1]
130+
? [] // Yes: assign a new array object
131+
: {}, // No: assign a new plain object
132+
obj)[path[path.length - 1]] = value; // Finally assign the value to the last key
133+
134+
return obj; // Return the top-level object to allow chaining
135+
}
136+
137+
138+
async processForm(formData, formFiles) {
78139
const doc = this._parser.parseFromString(formData, 'text/xml');
79-
this.formData = (await fetch('http://localhost:3002/form/parse/' + encodeURIComponent(formData)).then(res => res.json())).data;
140+
this.formData = (await fetch(settings.formManagerBaseURI + '/form/parse/' + encodeURIComponent(formData)).then(res => res.json())).data;
141+
for (let i = 0; i < formFiles.length; i++) {
142+
const file = formFiles[i];
143+
const fileURL = await this.uploadFile(file);
144+
const kk = this.findKey(this.formData, file.name, '$t', '');
145+
this.formData = this.set(this.formData, kk.substring(1), fileURL);
146+
}
80147
if (await this.formSpec.isSuccessExecute() === true) {
81148
this._state = 'FORM_SUCCESS';
82149
this._onFormSuccessData = await this.formSpec.onFormSuccessExecute();
83-
console.log(this._onFormFailureData);
84150
this._state = 'ON_FORM_SUCCESS_COMPLETED';
85151
this.nextForm = this.formSpec.onSuccess.next;
86152
this._message = this.formSpec.messageOnSuccess;

packages/enketo-express/public/js/src/module/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ settingsMap.forEach( obj => {
2424

2525
//add default login Url
2626
settings.loginUrl = config[ 'basePath' ] + DEFAULT_LOGIN_URL;
27+
settings.formManagerBaseURI = config['formManagerBaseURI'];
2728

2829
// add default return Url
2930
settings.defaultReturnUrl = config[ 'basePath' ] + DEFAULT_THANKS_URL;

packages/form-manager/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ lerna-debug.log*
3232
!.vscode/settings.json
3333
!.vscode/tasks.json
3434
!.vscode/launch.json
35-
!.vscode/extensions.json
35+
!.vscode/extensions.json
36+
37+
.development.env

0 commit comments

Comments
 (0)