useError.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { AnyJson, AnyStore } from '~/types/interfaces'
  2. import {computed, ComputedRef, useStore} from "@nuxtjs/composition-api";
  3. /**
  4. * @category Use/form
  5. * @class UseError
  6. * Use Classe pour gérer l'apparition de message si le formulaire est dirty
  7. */
  8. export class UseError {
  9. private store: AnyStore
  10. constructor () {
  11. this.store = useStore()
  12. }
  13. /**
  14. * Composition function
  15. */
  16. public invoke (field: string, emit: any): AnyJson {
  17. const error:ComputedRef<Boolean> = computed(()=>{
  18. return this.store.state.form.violations.indexOf(field) >= 0
  19. })
  20. return {
  21. onChange: (fieldValue:any) => this.onChange(emit, fieldValue, field),
  22. error
  23. }
  24. }
  25. /**
  26. *
  27. */
  28. private onChange (emit:any, fieldValue:any, field:string) {
  29. const errors = this.store.state.form.violations.filter((field:string) => field !== field)
  30. this.store.commit('form/setViolations', errors)
  31. emit('update', fieldValue, field)
  32. }
  33. }
  34. export const $useError = (field: string, emit: any) => new UseError().invoke(field, emit)