Form.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!--
  2. Formulaire générique
  3. @see https://vuetifyjs.com/en/components/forms/#usage
  4. -->
  5. <template>
  6. <main>
  7. <v-form
  8. ref="form"
  9. lazy-validation
  10. :readonly="readonly"
  11. >
  12. <v-container fluid class="container btnActions">
  13. <v-row>
  14. <v-col cols="12" sm="12">
  15. <slot name="form.button"/>
  16. <UiButtonSubmit
  17. @submit="submit"
  18. :actions="actions"
  19. ></UiButtonSubmit>
  20. </v-col>
  21. </v-row>
  22. </v-container>
  23. <slot name="form.input" v-bind="{entry,updateRepository}"/>
  24. <v-container fluid class="container btnActions">
  25. <v-row>
  26. <v-col cols="12" sm="12">
  27. <slot name="form.button"/>
  28. <UiButtonSubmit
  29. @submit="submit"
  30. :actions="actions"
  31. ></UiButtonSubmit>
  32. </v-col>
  33. </v-row>
  34. </v-container>
  35. </v-form>
  36. <lazy-LayoutDialog
  37. :show="showDialog"
  38. >
  39. <template v-slot:dialogText>
  40. <v-card-title class="text-h5 grey lighten-2">
  41. {{ $t('attention') }}
  42. </v-card-title>
  43. <v-card-text>
  44. <br>
  45. <p>{{ $t('quit_without_saving_warning') }}</p>
  46. </v-card-text>
  47. </template>
  48. <template v-slot:dialogBtn>
  49. <v-btn class="mr-4 submitBtn ot_green ot_white--text" @click="closeDialog">
  50. {{ $t('back_to_form') }}
  51. </v-btn>
  52. <v-btn class="mr-4 submitBtn ot_green ot_white--text" @click="saveAndQuit">
  53. {{ $t('save_and_quit') }}
  54. </v-btn>
  55. <v-btn class="mr-4 submitBtn ot_danger ot_white--text" @click="quitForm">
  56. {{ $t('quit_form') }}
  57. </v-btn>
  58. </template>
  59. </lazy-LayoutDialog>
  60. </main>
  61. </template>
  62. <script lang="ts">
  63. import {computed, ComputedRef, defineComponent, toRefs, ToRefs, useContext} from '@nuxtjs/composition-api'
  64. import {Query} from '@vuex-orm/core'
  65. import {repositoryHelper} from '~/services/store/repository'
  66. import {queryHelper} from '~/services/store/query'
  67. import {FORM_STATUS, QUERY_TYPE, SUBMIT_TYPE, TYPE_ALERT} from '~/types/enums'
  68. import {AnyJson} from '~/types/interfaces'
  69. import {$useForm} from '~/use/form/useForm'
  70. import * as _ from 'lodash'
  71. import Form from "~/services/store/form";
  72. import Page from "~/services/store/page";
  73. import UseNextStepFactory from "~/use/form/useNextStepFactory";
  74. export default defineComponent({
  75. props: {
  76. model: {
  77. type: Function,
  78. required: true
  79. },
  80. id: {
  81. type: [Number, String],
  82. required: true
  83. },
  84. query: {
  85. type: Object as () => Query,
  86. required: true
  87. },
  88. submitActions: {
  89. type: Object,
  90. required: false,
  91. default: () => {
  92. let actions:AnyJson = {}
  93. actions[SUBMIT_TYPE.SAVE] = {}
  94. return actions
  95. }
  96. }
  97. },
  98. setup(props) {
  99. const {$dataPersister, store, app: {router, i18n}} = useContext()
  100. const {markFormAsDirty, markFormAsNotDirty, readonly} = $useForm()
  101. const nextStepFactory = new UseNextStepFactory()
  102. const {id, query}: ToRefs = toRefs(props)
  103. const page = new Page(store)
  104. const entry: ComputedRef<AnyJson> = computed(() => {
  105. return queryHelper.getFlattenEntry(query.value, id.value)
  106. })
  107. const updateRepository = (newValue: string, field: string) => {
  108. markFormAsDirty()
  109. repositoryHelper.updateStoreFromField(props.model, entry.value, newValue, field)
  110. }
  111. const submit = async (next: string|null = null) => {
  112. markFormAsNotDirty()
  113. try {
  114. const response = await $dataPersister.invoke({
  115. type: QUERY_TYPE.MODEL,
  116. model: props.model,
  117. id: store.state.form.formStatus === FORM_STATUS.EDIT ? id.value : null,
  118. idTemp: store.state.form.formStatus === FORM_STATUS.CREATE ? id.value : null,
  119. query: props.query
  120. })
  121. page.addAlerts(TYPE_ALERT.SUCCESS, [i18n.t('saveSuccess') as string])
  122. nextStep(next, response.data)
  123. } catch (error) {
  124. if (error.response.status === 422) {
  125. if(error.response.data['violations']){
  126. const violations:Array<string> = []
  127. const fields:Array<string> = []
  128. for(const violation of error.response.data['violations']){
  129. violations.push(i18n.t(violation['message']) as string)
  130. fields.push(violation['propertyPath'])
  131. }
  132. new Form(store).addViolations(fields)
  133. page.addAlerts(TYPE_ALERT.ALERT, violations)
  134. }
  135. }
  136. }
  137. }
  138. const nextStep = (next: string|null, response: AnyJson) =>{
  139. if(next === null) return
  140. nextStepFactory.invoke(props.submitActions[next], response)[next]()
  141. }
  142. const showDialog: ComputedRef<boolean> = computed(() => {
  143. return store.state.form.showConfirmToLeave
  144. })
  145. const closeDialog = () => {
  146. store.commit('form/setShowConfirmToLeave', false)
  147. }
  148. const saveAndQuit = async () => {
  149. await submit()
  150. quitForm()
  151. }
  152. const quitForm = () => {
  153. markFormAsNotDirty()
  154. store.commit('form/setShowConfirmToLeave', false)
  155. const entryCopy = query.value.first()
  156. if (entryCopy && entryCopy.$getAttributes().originalState) {
  157. repositoryHelper.persist(props.model, entryCopy.$getAttributes().originalState)
  158. }
  159. if (router) {
  160. router.push(store.state.form.goAfterLeave)
  161. }
  162. }
  163. const actions = computed(()=>{
  164. return _.keys(props.submitActions)
  165. })
  166. return {
  167. submit,
  168. updateRepository,
  169. readonly,
  170. showDialog,
  171. entry,
  172. quitForm,
  173. closeDialog,
  174. saveAndQuit,
  175. actions
  176. }
  177. }
  178. })
  179. </script>
  180. <style scoped>
  181. .btnActions {
  182. text-align: right;
  183. }
  184. </style>