|
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 | +// }; |
2 | 13 |
|
3 | 14 | export class FormController { |
4 | 15 |
|
@@ -74,13 +85,68 @@ export class FormController { |
74 | 85 | return this._state; |
75 | 86 | } |
76 | 87 |
|
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) { |
78 | 139 | 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 | + } |
80 | 147 | if (await this.formSpec.isSuccessExecute() === true) { |
81 | 148 | this._state = 'FORM_SUCCESS'; |
82 | 149 | this._onFormSuccessData = await this.formSpec.onFormSuccessExecute(); |
83 | | - console.log(this._onFormFailureData); |
84 | 150 | this._state = 'ON_FORM_SUCCESS_COMPLETED'; |
85 | 151 | this.nextForm = this.formSpec.onSuccess.next; |
86 | 152 | this._message = this.formSpec.messageOnSuccess; |
|
0 commit comments