|
|
@@ -1,53 +1,74 @@
|
|
|
-import {formState} from '~/types/interfaces'
|
|
|
import {FORM_FUNCTION} from "~/types/enum/enums";
|
|
|
import {defineStore} from "pinia";
|
|
|
-import {AnyJson} from "~/types/enum/data";
|
|
|
-
|
|
|
-export const useFormStore = defineStore('form', {
|
|
|
- state: (): formState => {
|
|
|
- return {
|
|
|
- formFunction: FORM_FUNCTION.EDIT,
|
|
|
- violations: {},
|
|
|
- readonly: false,
|
|
|
- dirty: false,
|
|
|
- showConfirmToLeave: false,
|
|
|
- goAfterLeave: null
|
|
|
- }
|
|
|
- },
|
|
|
- actions: {
|
|
|
- setViolations (violations: Array<string>) {
|
|
|
- this.violations = violations
|
|
|
- },
|
|
|
- addViolations(invalidFields: AnyJson){
|
|
|
- this.violations = invalidFields
|
|
|
- },
|
|
|
- setReadOnly (readonly: boolean) {
|
|
|
- this.readonly = readonly
|
|
|
- },
|
|
|
- setFormFunction (formFunction: FORM_FUNCTION) {
|
|
|
- this.formFunction = formFunction
|
|
|
- },
|
|
|
- setDirty (dirty: boolean) {
|
|
|
- this.dirty = dirty
|
|
|
- },
|
|
|
- setShowConfirmToLeave (showConfirmToLeave: boolean) {
|
|
|
- this.showConfirmToLeave = showConfirmToLeave
|
|
|
- },
|
|
|
- setGoAfterLeave (goAfterLeave: string) {
|
|
|
- this.goAfterLeave = goAfterLeave
|
|
|
- },
|
|
|
- /**
|
|
|
- * Actions devant être gérées si on souhaite quitter une page
|
|
|
- * @param to
|
|
|
- */
|
|
|
- handleActionsAfterLeavePage(to: any){
|
|
|
- if (this.dirty) {
|
|
|
- this.showConfirmToLeave = true
|
|
|
- this.goAfterLeave = to
|
|
|
- } else {
|
|
|
- this.formFunction = FORM_FUNCTION.EDIT
|
|
|
- this.violations = []
|
|
|
- }
|
|
|
+import {Ref, ref} from "@vue/reactivity";
|
|
|
+import {AnyJson} from "~/types/data";
|
|
|
+
|
|
|
+export const useFormStore = defineStore('form', () => {
|
|
|
+ const formFunction = ref(FORM_FUNCTION.EDIT)
|
|
|
+ const violations = ref({})
|
|
|
+ const readonly = ref(false)
|
|
|
+ const dirty = ref(false)
|
|
|
+ const showConfirmToLeave = ref(false)
|
|
|
+ const goAfterLeave: Ref<string | null> = ref(null)
|
|
|
+
|
|
|
+ const setViolations = (newViolations: Array<string>) => {
|
|
|
+ violations.value = newViolations
|
|
|
+ }
|
|
|
+
|
|
|
+ const addViolation = (invalidFields: AnyJson) => {
|
|
|
+ // TODO: revoir
|
|
|
+ violations.value = invalidFields
|
|
|
+ }
|
|
|
+
|
|
|
+ const setReadOnly = (value: boolean) => {
|
|
|
+ readonly.value = value
|
|
|
+ }
|
|
|
+
|
|
|
+ const setFormFunction = (newFormFunction: FORM_FUNCTION) => {
|
|
|
+ formFunction.value = newFormFunction
|
|
|
+ }
|
|
|
+
|
|
|
+ const setDirty = (value: boolean) => {
|
|
|
+ dirty.value = value
|
|
|
+ }
|
|
|
+
|
|
|
+ const setShowConfirmToLeave = (value: boolean) => {
|
|
|
+ showConfirmToLeave.value = value
|
|
|
+ }
|
|
|
+
|
|
|
+ const setGoAfterLeave = (value: string) => {
|
|
|
+ goAfterLeave.value = value
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Actions devant être gérées si on souhaite quitter une page
|
|
|
+ *
|
|
|
+ * @param to
|
|
|
+ */
|
|
|
+ const handleActionsAfterLeavingPage = (to: any) => {
|
|
|
+ if (dirty) {
|
|
|
+ showConfirmToLeave.value = true
|
|
|
+ goAfterLeave.value = to
|
|
|
+ } else {
|
|
|
+ formFunction.value = FORM_FUNCTION.EDIT
|
|
|
+ violations.value = []
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return [
|
|
|
+ formFunction,
|
|
|
+ violations,
|
|
|
+ readonly,
|
|
|
+ dirty,
|
|
|
+ showConfirmToLeave,
|
|
|
+ goAfterLeave,
|
|
|
+ setViolations,
|
|
|
+ addViolation,
|
|
|
+ setReadOnly,
|
|
|
+ setFormFunction,
|
|
|
+ setDirty,
|
|
|
+ setShowConfirmToLeave,
|
|
|
+ setGoAfterLeave,
|
|
|
+ handleActionsAfterLeavingPage
|
|
|
+ ]
|
|
|
})
|