form.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import {formState} from '~/types/interfaces'
  2. import {FORM_FUNCTION} from "~/types/enum/enums";
  3. import {defineStore} from "pinia";
  4. import {AnyJson} from "~/types/enum/data";
  5. export const useFormStore = defineStore('form', {
  6. state: (): formState => {
  7. return {
  8. formFunction: FORM_FUNCTION.EDIT,
  9. violations: {},
  10. readonly: false,
  11. dirty: false,
  12. showConfirmToLeave: false,
  13. goAfterLeave: null
  14. }
  15. },
  16. actions: {
  17. setViolations (violations: Array<string>) {
  18. this.violations = violations
  19. },
  20. addViolations(invalidFields: AnyJson){
  21. this.violations = invalidFields
  22. },
  23. setReadOnly (readonly: boolean) {
  24. this.readonly = readonly
  25. },
  26. setFormFunction (formFunction: FORM_FUNCTION) {
  27. this.formFunction = formFunction
  28. },
  29. setDirty (dirty: boolean) {
  30. this.dirty = dirty
  31. },
  32. setShowConfirmToLeave (showConfirmToLeave: boolean) {
  33. this.showConfirmToLeave = showConfirmToLeave
  34. },
  35. setGoAfterLeave (goAfterLeave: string) {
  36. this.goAfterLeave = goAfterLeave
  37. },
  38. /**
  39. * Actions devant être gérées si on souhaite quitter une page
  40. * @param to
  41. */
  42. handleActionsAfterLeavePage(to: any){
  43. if (this.dirty) {
  44. this.showConfirmToLeave = true
  45. this.goAfterLeave = to
  46. } else {
  47. this.formFunction = FORM_FUNCTION.EDIT
  48. this.violations = []
  49. }
  50. }
  51. }
  52. })