|
|
@@ -8,30 +8,31 @@ Formulaire générique
|
|
|
<main>
|
|
|
<v-form
|
|
|
ref="form"
|
|
|
- v-model="properties.valid"
|
|
|
lazy-validation
|
|
|
:readonly="readonly"
|
|
|
>
|
|
|
<v-container fluid class="container btnActions">
|
|
|
<v-row>
|
|
|
<v-col cols="12" sm="12">
|
|
|
- <slot name="form.button" />
|
|
|
- <v-btn v-if="!readonly" class="mr-4 ot_green ot_white--text" @click="submit">
|
|
|
- {{ $t('save') }}
|
|
|
- </v-btn>
|
|
|
+ <slot name="form.button"/>
|
|
|
+ <UiButtonSubmit
|
|
|
+ @submit="submit"
|
|
|
+ :actions="actions"
|
|
|
+ ></UiButtonSubmit>
|
|
|
</v-col>
|
|
|
</v-row>
|
|
|
</v-container>
|
|
|
|
|
|
- <slot name="form.input" v-bind="{entry,updateRepository}" />
|
|
|
+ <slot name="form.input" v-bind="{entry,updateRepository}"/>
|
|
|
|
|
|
<v-container fluid class="container btnActions">
|
|
|
<v-row>
|
|
|
<v-col cols="12" sm="12">
|
|
|
- <slot name="form.button" />
|
|
|
- <v-btn v-if="!readonly" class="mr-4 ot_green ot_white--text" @click="submit">
|
|
|
- {{ $t('save') }}
|
|
|
- </v-btn>
|
|
|
+ <slot name="form.button"/>
|
|
|
+ <UiButtonSubmit
|
|
|
+ @submit="submit"
|
|
|
+ :actions="actions"
|
|
|
+ ></UiButtonSubmit>
|
|
|
</v-col>
|
|
|
</v-row>
|
|
|
</v-container>
|
|
|
@@ -56,24 +57,27 @@ Formulaire générique
|
|
|
<v-btn class="mr-4 submitBtn ot_green ot_white--text" @click="saveAndQuit">
|
|
|
{{ $t('save_and_quit') }}
|
|
|
</v-btn>
|
|
|
- <v-btn class="mr-4 submitBtn ot_danger ot_white--text" @click="goEvenUnsavedData">
|
|
|
+ <v-btn class="mr-4 submitBtn ot_danger ot_white--text" @click="quitForm">
|
|
|
{{ $t('quit_form') }}
|
|
|
</v-btn>
|
|
|
</template>
|
|
|
</lazy-LayoutDialog>
|
|
|
+
|
|
|
</main>
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts">
|
|
|
-import {
|
|
|
- computed, defineComponent, reactive, toRefs, useContext, ref, Ref, ComputedRef, ToRefs, UnwrapRef
|
|
|
-} from '@nuxtjs/composition-api'
|
|
|
-import { Query } from '@vuex-orm/core'
|
|
|
-import { repositoryHelper } from '~/services/store/repository'
|
|
|
-import { queryHelper } from '~/services/store/query'
|
|
|
-import { QUERY_TYPE, TYPE_ALERT } from '~/types/enums'
|
|
|
-import { alert, AnyJson } from '~/types/interfaces'
|
|
|
-import { $useDirtyForm } from '~/use/form/useDirtyForm'
|
|
|
+import {computed, ComputedRef, defineComponent, toRefs, ToRefs, useContext} from '@nuxtjs/composition-api'
|
|
|
+import {Query} from '@vuex-orm/core'
|
|
|
+import {repositoryHelper} from '~/services/store/repository'
|
|
|
+import {queryHelper} from '~/services/store/query'
|
|
|
+import {FORM_STATUS, QUERY_TYPE, SUBMIT_TYPE, TYPE_ALERT} from '~/types/enums'
|
|
|
+import {AnyJson} from '~/types/interfaces'
|
|
|
+import {$useForm} from '~/use/form/useForm'
|
|
|
+import * as _ from 'lodash'
|
|
|
+import Form from "~/services/store/form";
|
|
|
+import Page from "~/services/store/page";
|
|
|
+import UseNextStepFactory from "~/use/form/useNextStepFactory";
|
|
|
|
|
|
export default defineComponent({
|
|
|
props: {
|
|
|
@@ -82,26 +86,29 @@ export default defineComponent({
|
|
|
required: true
|
|
|
},
|
|
|
id: {
|
|
|
- type: Number,
|
|
|
+ type: [Number, String],
|
|
|
required: true
|
|
|
},
|
|
|
query: {
|
|
|
type: Object as () => Query,
|
|
|
required: true
|
|
|
+ },
|
|
|
+ submitActions: {
|
|
|
+ type: Object,
|
|
|
+ required: false,
|
|
|
+ default: () => {
|
|
|
+ let actions:AnyJson = {}
|
|
|
+ actions[SUBMIT_TYPE.SAVE] = {}
|
|
|
+ return actions
|
|
|
+ }
|
|
|
}
|
|
|
},
|
|
|
- setup (props) {
|
|
|
- const { $dataPersister, store, app: { router, i18n } } = useContext()
|
|
|
- const { markFormAsDirty, markFormAsNotDirty } = $useDirtyForm(store)
|
|
|
-
|
|
|
- const { id, query }: ToRefs = toRefs(props)
|
|
|
- const properties: UnwrapRef<AnyJson> = reactive({
|
|
|
- valid: false,
|
|
|
- saveOk: false,
|
|
|
- saveKo: false
|
|
|
- })
|
|
|
-
|
|
|
- const readonly: Ref<boolean> = ref(false)
|
|
|
+ setup(props) {
|
|
|
+ const {$dataPersister, store, app: {router, i18n}} = useContext()
|
|
|
+ const {markFormAsDirty, markFormAsNotDirty, readonly} = $useForm()
|
|
|
+ const nextStepFactory = new UseNextStepFactory()
|
|
|
+ const {id, query}: ToRefs = toRefs(props)
|
|
|
+ const page = new Page(store)
|
|
|
|
|
|
const entry: ComputedRef<AnyJson> = computed(() => {
|
|
|
return queryHelper.getFlattenEntry(query.value, id.value)
|
|
|
@@ -112,31 +119,43 @@ export default defineComponent({
|
|
|
repositoryHelper.updateStoreFromField(props.model, entry.value, newValue, field)
|
|
|
}
|
|
|
|
|
|
- const submit = async () => {
|
|
|
+ const submit = async (next: string|null = null) => {
|
|
|
+ markFormAsNotDirty()
|
|
|
+
|
|
|
try {
|
|
|
- markFormAsNotDirty()
|
|
|
- await $dataPersister.invoke({
|
|
|
+ const response = await $dataPersister.invoke({
|
|
|
type: QUERY_TYPE.MODEL,
|
|
|
model: props.model,
|
|
|
- id: id.value,
|
|
|
+ id: store.state.form.formStatus === FORM_STATUS.EDIT ? id.value : null,
|
|
|
+ idTemp: store.state.form.formStatus === FORM_STATUS.CREATE ? id.value : null,
|
|
|
query: props.query
|
|
|
})
|
|
|
|
|
|
- const alert:alert = {
|
|
|
- type: TYPE_ALERT.SUCCESS,
|
|
|
- message: i18n.t('saveSuccess') as string
|
|
|
- }
|
|
|
- store.commit('page/setAlert', alert)
|
|
|
+ page.addAlerts(TYPE_ALERT.SUCCESS, [i18n.t('saveSuccess') as string])
|
|
|
+ nextStep(next, response.data)
|
|
|
} catch (error) {
|
|
|
- const alert:alert = {
|
|
|
- type: TYPE_ALERT.ALERT,
|
|
|
- message: error.message
|
|
|
+ if (error.response.status === 422) {
|
|
|
+ if(error.response.data['violations']){
|
|
|
+ const violations:Array<string> = []
|
|
|
+ const fields:Array<string> = []
|
|
|
+ for(const violation of error.response.data['violations']){
|
|
|
+ violations.push(i18n.t(violation['message']) as string)
|
|
|
+ fields.push(violation['propertyPath'])
|
|
|
+ }
|
|
|
+
|
|
|
+ new Form(store).addViolations(fields)
|
|
|
+ page.addAlerts(TYPE_ALERT.ALERT, violations)
|
|
|
+ }
|
|
|
}
|
|
|
- store.commit('page/setAlert', alert)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- const showDialog:ComputedRef<boolean> = computed(() => {
|
|
|
+ const nextStep = (next: string|null, response: AnyJson) =>{
|
|
|
+ if(next === null) return
|
|
|
+ nextStepFactory.invoke(props.submitActions[next], response)[next]()
|
|
|
+ }
|
|
|
+
|
|
|
+ const showDialog: ComputedRef<boolean> = computed(() => {
|
|
|
return store.state.form.showConfirmToLeave
|
|
|
})
|
|
|
|
|
|
@@ -146,10 +165,10 @@ export default defineComponent({
|
|
|
|
|
|
const saveAndQuit = async () => {
|
|
|
await submit()
|
|
|
- goEvenUnsavedData()
|
|
|
+ quitForm()
|
|
|
}
|
|
|
|
|
|
- const goEvenUnsavedData = () => {
|
|
|
+ const quitForm = () => {
|
|
|
markFormAsNotDirty()
|
|
|
store.commit('form/setShowConfirmToLeave', false)
|
|
|
|
|
|
@@ -163,23 +182,27 @@ export default defineComponent({
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ const actions = computed(()=>{
|
|
|
+ return _.keys(props.submitActions)
|
|
|
+ })
|
|
|
+
|
|
|
return {
|
|
|
submit,
|
|
|
updateRepository,
|
|
|
- properties,
|
|
|
readonly,
|
|
|
showDialog,
|
|
|
entry,
|
|
|
- goEvenUnsavedData,
|
|
|
+ quitForm,
|
|
|
closeDialog,
|
|
|
- saveAndQuit
|
|
|
+ saveAndQuit,
|
|
|
+ actions
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
</script>
|
|
|
|
|
|
<style scoped>
|
|
|
- .btnActions {
|
|
|
- text-align: right;
|
|
|
- }
|
|
|
+.btnActions {
|
|
|
+ text-align: right;
|
|
|
+}
|
|
|
</style>
|