import { defineStore } from 'pinia' import { ref } from 'vue' import { FORM_FUNCTION } from '~/types/enum/enums' import type { AnyJson } from '~/types/data' export const useFormStore = defineStore('form', () => { const formFunction: Ref = ref(FORM_FUNCTION.EDIT) const violations: Ref = ref({}) const readonly: Ref = ref(false) const dirty: Ref = ref(false) const showConfirmToLeave: Ref = ref(false) const goAfterLeave: Ref = ref(null) const setViolations = (newViolations: Array) => { 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: string) => { if (dirty.value) { 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, } })