ovictoraurelio

Untitled

Mar 30th, 2022
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. import createModuleLogger from '@/logger/winston'
  4. const l = createModuleLogger('FORMS')
  5. import vueRecursiveSet from './recursiveSetter'
  6.  
  7. export const deleteItem = (
  8.   storeModule,
  9.   { confirmationParams, refresh = true, refreshOpts } = {}
  10. ) =>
  11.   async function (item) {
  12.     const setDeleting = (id) => {
  13.       if (this.isDeleting) {
  14.         this.$set(this.isDeleting, id, false)
  15.       }
  16.     }
  17.     const params = {
  18.       title: 'Atenção',
  19.       text: 'Tem certeza que deseja remover esse item?'
  20.     }
  21.     const userConfirmation = await this.$store.dispatch(
  22.       'app/askConfirmation',
  23.       confirmationParams && Object.keys(confirmationParams).length > 0
  24.         ? confirmationParams
  25.         : params
  26.     )
  27.  
  28.     if (userConfirmation === true) {
  29.       if (this.isDeleting) {
  30.         this.$set(this.isDeleting, item.id, true)
  31.       }
  32.       await this.$store.dispatch(`${storeModule}/delete`, item)
  33.       if (refresh) {
  34.         this.refresh({ refreshOpts }).then((el) => {
  35.           setDeleting(item.id)
  36.           return true
  37.         })
  38.       } else {
  39.         setDeleting(item.id)
  40.         return true
  41.       }
  42.     }
  43.     return false
  44.   }
  45.  
  46. export const setFormData = function (item) {
  47.   Object.keys(item).map((property) => {
  48.     this.$set(this.form, property, item[property])
  49.   })
  50. }
  51.  
  52. export const resetForm = function () {
  53.   if (this.$refs.form) {
  54.     this.$refs.form.reset()
  55.     this.$refs.form.resetValidation()
  56.   }
  57. }
  58.  
  59. export const editItem = async function (item) {
  60.   if (!this) {
  61.     return
  62.   }
  63.  
  64.   resetForm.call(this)
  65.  
  66.   this.$nextTick(() => {
  67.     setFormData.call(this, item)
  68.     this.editorDialog.visible = true
  69.   })
  70. }
  71.  
  72. export const editRemoteItem = async function (item) {
  73.   if (!this) {
  74.     return
  75.   }
  76.   this.loadings.itemData = true
  77.   resetForm.call(this)
  78.   if (typeof this.editorDialog?.visible !== 'undefined') {
  79.     this.editorDialog.visible = true
  80.   }
  81.   const current = await this.listOne(item.id)
  82.   setFormData.call(this, current)
  83.   this.loadings.itemData = false
  84. }
  85. export const duplicateItem = function (item) {
  86.   delete item.id
  87.   editItem.call(this, item)
  88. }
  89.  
  90. export const openCreateDialog = function () {
  91.   if (!this) {
  92.     return
  93.   }
  94.   if (this.$refs.form) {
  95.     this.$refs.form.reset()
  96.     this.$refs.form.resetValidation()
  97.   }
  98.   this.form.id = null
  99.   this.editorDialog.visible = true
  100. }
  101.  
  102. const saveAditionalFormInfos = function () {
  103.   if (!this.$refs.additionalForm?.$children?.length) {
  104.     return Promise.resolve()
  105.   }
  106.   const additionalForm = this.$refs.additionalForm.$children[0]
  107.   return additionalForm.sendForm()
  108. }
  109.  
  110. export const save = (storeModule, formParam = null) =>
  111.   /**
  112.    *
  113.    * @param {{
  114.    * refreshOpts: Object,
  115.    * autoClose: Boolean,
  116.    * additionalFormAttribute: String<formResponseId|formId>,
  117.    * }} param0
  118.    * @returns
  119.    */
  120.   async function ({
  121.     refreshOpts,
  122.     autoClose = true,
  123.     additionalFormAttribute = 'formResponseId'
  124.   } = {}) {
  125.     const formRef = this.$refs[formParam] || this.$refs.form
  126.     if (!formRef.validate()) {
  127.       this.$store.dispatch(
  128.         'snackbar/warning',
  129.         'Verifique se os campos estão corretamente preenchidos'
  130.       )
  131.       return null
  132.     }
  133.     this.loadings.form = true
  134.  
  135.     try {
  136.       const savedForms = await saveAditionalFormInfos.call(this, {
  137.         additionalFormAttribute
  138.       })
  139.       // Atualizando o ID do form salvo como referência para o objeto principal.
  140.       if (savedForms?.id) {
  141.         this.$set(this.form, additionalFormAttribute, savedForms.id)
  142.       }
  143.  
  144.       if (this.isUpdating && this.form.id) {
  145.         this.$set(this.isUpdating, this.form.id, true)
  146.       }
  147.  
  148.       const action = this.form.id ? 'update' : 'create'
  149.  
  150.       const actionResult = await this.$store.dispatch(
  151.         `${storeModule}/${action}`,
  152.         this.form
  153.       )
  154.  
  155.       this.refresh({ refreshOpts })
  156.  
  157.       if (this.isUpdating && this.form.id) {
  158.         this.$set(this.isUpdating, this.form.id, false)
  159.       }
  160.  
  161.       this.$store.dispatch('snackbar/success', 'Salvo com sucesso')
  162.       if (autoClose) {
  163.         if (this.editorDialog) {
  164.           this.editorDialog.visible = false
  165.         }
  166.       }
  167.       if (this.loadings) {
  168.         this.loadings.form = false
  169.       }
  170.  
  171.       return actionResult
  172.     } catch (err) {
  173.       l.error(err)
  174.       this.$store.dispatch('snackbar/error', 'Erro ao salvar')
  175.       if (this.loadings) {
  176.         this.loadings.form = false
  177.       }
  178.       return null
  179.     }
  180.   }
  181.  
  182. export const refresh = (storeModule) =>
  183.   async function ({ refreshOpts = {} } = {}) {
  184.     await this.$store.dispatch(`${storeModule}/list`, {
  185.       force: true,
  186.       ...refreshOpts
  187.     })
  188.   }
  189.  
  190. export const updateForm = function (
  191.   form,
  192.   object,
  193.   { useObjKeys, ignoreEmptyArray, ignoreUndefined } = {}
  194. ) {
  195.   l.debug('Called update form', Object.keys(form), object)
  196.   vueRecursiveSet(form, object, {
  197.     useObjKeys,
  198.     ignoreEmptyArray,
  199.     ignoreUndefined
  200.   })
  201. }
  202.  
  203. export const formMethods = function (storeModule) {
  204.   return {
  205.     deleteItem: deleteItem(storeModule),
  206.     duplicateItem,
  207.     editItem,
  208.     editRemoteItem,
  209.     openCreateDialog,
  210.     refresh: refresh(storeModule),
  211.     resetForm,
  212.     save: save(storeModule),
  213.     updateForm
  214.   }
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment