2020-02-26 01:32:40 +03:00
|
|
|
import { cloneDeep } from './libs/utils'
|
|
|
|
import FileUpload from './FileUpload'
|
|
|
|
|
|
|
|
export default class FormSubmission {
|
|
|
|
/**
|
|
|
|
* Initialize a formulate form.
|
|
|
|
* @param {vm} form an instance of FormulateForm
|
|
|
|
*/
|
|
|
|
constructor (form) {
|
|
|
|
this.form = form
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the form has any validation errors.
|
|
|
|
*
|
|
|
|
* @return {Promise} resolves a boolean
|
|
|
|
*/
|
|
|
|
hasValidationErrors () {
|
|
|
|
return this.form.hasValidationErrors()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously generate the values payload of this form.
|
|
|
|
* @return {Promise} resolves to json
|
|
|
|
*/
|
|
|
|
values () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const pending = []
|
2020-02-26 08:21:10 +03:00
|
|
|
const values = cloneDeep(this.form.internalFormModelProxy)
|
2020-02-26 01:32:40 +03:00
|
|
|
for (const key in values) {
|
2020-02-26 08:21:10 +03:00
|
|
|
if (typeof this.form.internalFormModelProxy[key] === 'object' && this.form.internalFormModelProxy[key] instanceof FileUpload) {
|
2020-02-29 08:18:58 +03:00
|
|
|
pending.push(
|
|
|
|
this.form.internalFormModelProxy[key].upload().then(data => Object.assign(values, { [key]: data }))
|
|
|
|
)
|
2020-02-26 01:32:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Promise.all(pending)
|
|
|
|
.then(() => resolve(values))
|
|
|
|
.catch(err => reject(err))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|