2020-02-26 01:32:40 +03:00
|
|
|
import { cloneDeep } from './libs/utils'
|
|
|
|
import FileUpload from './FileUpload'
|
2020-10-10 22:45:28 +03:00
|
|
|
import FormularioForm from '@/FormularioForm.vue'
|
2020-02-26 01:32:40 +03:00
|
|
|
|
|
|
|
export default class FormSubmission {
|
2020-10-10 22:45:28 +03:00
|
|
|
public form: FormularioForm
|
|
|
|
|
2020-05-22 14:22:56 +03:00
|
|
|
/**
|
|
|
|
* Initialize a formulario form.
|
|
|
|
* @param {vm} form an instance of FormularioForm
|
|
|
|
*/
|
2020-10-10 22:45:28 +03:00
|
|
|
constructor (form: FormularioForm) {
|
2020-05-22 14:22:56 +03:00
|
|
|
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 () {
|
2020-10-10 22:45:28 +03:00
|
|
|
return (this.form as any).hasValidationErrors()
|
2020-05-22 14:22:56 +03:00
|
|
|
}
|
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) => {
|
2020-10-10 22:45:28 +03:00
|
|
|
const form = this.form as any
|
2020-05-22 14:22:56 +03:00
|
|
|
const pending = []
|
2020-10-10 22:45:28 +03:00
|
|
|
const values = cloneDeep(form.proxy)
|
|
|
|
|
2020-05-22 14:22:56 +03:00
|
|
|
for (const key in values) {
|
2020-10-10 22:45:28 +03:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(values, key) &&
|
|
|
|
typeof form.proxy[key] === 'object' &&
|
|
|
|
form.proxy[key] instanceof FileUpload) {
|
2020-05-22 14:22:56 +03:00
|
|
|
pending.push(
|
2020-10-10 22:45:28 +03:00
|
|
|
form.proxy[key].upload().then((data: Object) => Object.assign(values, { [key]: data }))
|
2020-05-22 14:22:56 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Promise.all(pending)
|
|
|
|
.then(() => resolve(values))
|
|
|
|
.catch(err => reject(err))
|
|
|
|
})
|
|
|
|
}
|
2020-02-26 01:32:40 +03:00
|
|
|
}
|