form.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {FORM_FUNCTION} from "~/types/enum/enums";
  2. import {defineStore} from "pinia";
  3. import {ref} from "@vue/reactivity";
  4. import type {AnyJson} from "~/types/data";
  5. export const useFormStore = defineStore('form', () => {
  6. const formFunction: Ref<FORM_FUNCTION> = ref(FORM_FUNCTION.EDIT)
  7. const violations: Ref<AnyJson> = ref({})
  8. const readonly: Ref<boolean> = ref(false)
  9. const dirty: Ref<boolean> = ref(false)
  10. const showConfirmToLeave: Ref<boolean> = ref(false)
  11. const goAfterLeave: Ref<string | null> = ref(null)
  12. const setViolations = (newViolations: Array<string>) => {
  13. violations.value = newViolations
  14. }
  15. const addViolation = (invalidFields: AnyJson) => {
  16. // TODO: à revoir
  17. violations.value = invalidFields
  18. }
  19. const setReadOnly = (value: boolean) => {
  20. readonly.value = value
  21. }
  22. const setFormFunction = (newFormFunction: FORM_FUNCTION) => {
  23. formFunction.value = newFormFunction
  24. }
  25. const setDirty = (value: boolean) => {
  26. dirty.value = value
  27. }
  28. const setShowConfirmToLeave = (value: boolean) => {
  29. showConfirmToLeave.value = value
  30. }
  31. const setGoAfterLeave = (value: string) => {
  32. goAfterLeave.value = value
  33. }
  34. /**
  35. * Actions devant être gérées si on souhaite quitter une page
  36. *
  37. * @param to
  38. */
  39. const handleActionsAfterLeavingPage = (to: any) => {
  40. if (dirty) {
  41. showConfirmToLeave.value = true
  42. goAfterLeave.value = to
  43. } else {
  44. formFunction.value = FORM_FUNCTION.EDIT
  45. violations.value = []
  46. }
  47. }
  48. return {
  49. formFunction,
  50. violations,
  51. readonly,
  52. dirty,
  53. showConfirmToLeave,
  54. goAfterLeave,
  55. setViolations,
  56. addViolation,
  57. setReadOnly,
  58. setFormFunction,
  59. setDirty,
  60. setShowConfirmToLeave,
  61. setGoAfterLeave,
  62. handleActionsAfterLeavingPage
  63. }
  64. })