Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- import createModuleLogger from '@/logger/winston'
- const l = createModuleLogger('FORMS')
- import vueRecursiveSet from './recursiveSetter'
- export const deleteItem = (
- storeModule,
- { confirmationParams, refresh = true, refreshOpts } = {}
- ) =>
- async function (item) {
- const setDeleting = (id) => {
- if (this.isDeleting) {
- this.$set(this.isDeleting, id, false)
- }
- }
- const params = {
- title: 'Atenção',
- text: 'Tem certeza que deseja remover esse item?'
- }
- const userConfirmation = await this.$store.dispatch(
- 'app/askConfirmation',
- confirmationParams && Object.keys(confirmationParams).length > 0
- ? confirmationParams
- : params
- )
- if (userConfirmation === true) {
- if (this.isDeleting) {
- this.$set(this.isDeleting, item.id, true)
- }
- await this.$store.dispatch(`${storeModule}/delete`, item)
- if (refresh) {
- this.refresh({ refreshOpts }).then((el) => {
- setDeleting(item.id)
- return true
- })
- } else {
- setDeleting(item.id)
- return true
- }
- }
- return false
- }
- export const setFormData = function (item) {
- Object.keys(item).map((property) => {
- this.$set(this.form, property, item[property])
- })
- }
- export const resetForm = function () {
- if (this.$refs.form) {
- this.$refs.form.reset()
- this.$refs.form.resetValidation()
- }
- }
- export const editItem = async function (item) {
- if (!this) {
- return
- }
- resetForm.call(this)
- this.$nextTick(() => {
- setFormData.call(this, item)
- this.editorDialog.visible = true
- })
- }
- export const editRemoteItem = async function (item) {
- if (!this) {
- return
- }
- this.loadings.itemData = true
- resetForm.call(this)
- if (typeof this.editorDialog?.visible !== 'undefined') {
- this.editorDialog.visible = true
- }
- const current = await this.listOne(item.id)
- setFormData.call(this, current)
- this.loadings.itemData = false
- }
- export const duplicateItem = function (item) {
- delete item.id
- editItem.call(this, item)
- }
- export const openCreateDialog = function () {
- if (!this) {
- return
- }
- if (this.$refs.form) {
- this.$refs.form.reset()
- this.$refs.form.resetValidation()
- }
- this.form.id = null
- this.editorDialog.visible = true
- }
- const saveAditionalFormInfos = function () {
- if (!this.$refs.additionalForm?.$children?.length) {
- return Promise.resolve()
- }
- const additionalForm = this.$refs.additionalForm.$children[0]
- return additionalForm.sendForm()
- }
- export const save = (storeModule, formParam = null) =>
- /**
- *
- * @param {{
- * refreshOpts: Object,
- * autoClose: Boolean,
- * additionalFormAttribute: String<formResponseId|formId>,
- * }} param0
- * @returns
- */
- async function ({
- refreshOpts,
- autoClose = true,
- additionalFormAttribute = 'formResponseId'
- } = {}) {
- const formRef = this.$refs[formParam] || this.$refs.form
- if (!formRef.validate()) {
- this.$store.dispatch(
- 'snackbar/warning',
- 'Verifique se os campos estão corretamente preenchidos'
- )
- return null
- }
- this.loadings.form = true
- try {
- const savedForms = await saveAditionalFormInfos.call(this, {
- additionalFormAttribute
- })
- // Atualizando o ID do form salvo como referência para o objeto principal.
- if (savedForms?.id) {
- this.$set(this.form, additionalFormAttribute, savedForms.id)
- }
- if (this.isUpdating && this.form.id) {
- this.$set(this.isUpdating, this.form.id, true)
- }
- const action = this.form.id ? 'update' : 'create'
- const actionResult = await this.$store.dispatch(
- `${storeModule}/${action}`,
- this.form
- )
- this.refresh({ refreshOpts })
- if (this.isUpdating && this.form.id) {
- this.$set(this.isUpdating, this.form.id, false)
- }
- this.$store.dispatch('snackbar/success', 'Salvo com sucesso')
- if (autoClose) {
- if (this.editorDialog) {
- this.editorDialog.visible = false
- }
- }
- if (this.loadings) {
- this.loadings.form = false
- }
- return actionResult
- } catch (err) {
- l.error(err)
- this.$store.dispatch('snackbar/error', 'Erro ao salvar')
- if (this.loadings) {
- this.loadings.form = false
- }
- return null
- }
- }
- export const refresh = (storeModule) =>
- async function ({ refreshOpts = {} } = {}) {
- await this.$store.dispatch(`${storeModule}/list`, {
- force: true,
- ...refreshOpts
- })
- }
- export const updateForm = function (
- form,
- object,
- { useObjKeys, ignoreEmptyArray, ignoreUndefined } = {}
- ) {
- l.debug('Called update form', Object.keys(form), object)
- vueRecursiveSet(form, object, {
- useObjKeys,
- ignoreEmptyArray,
- ignoreUndefined
- })
- }
- export const formMethods = function (storeModule) {
- return {
- deleteItem: deleteItem(storeModule),
- duplicateItem,
- editItem,
- editRemoteItem,
- openCreateDialog,
- refresh: refresh(storeModule),
- resetForm,
- save: save(storeModule),
- updateForm
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment