import {formState} from '~/types/interfaces' import {FORM_FUNCTION} from "~/types/enums"; import {defineStore} from "pinia"; import {AnyJson} from "~/types/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) { 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 = [] } } } })