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