useTicketDuplicateDetectionHandler.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { FormHandlerExecution } from '#shared/components/Form/types.ts'
  3. import type {
  4. ChangedField,
  5. FormHandler,
  6. FormHandlerFunction,
  7. ReactiveFormSchemData,
  8. } from '#shared/components/Form/types.ts'
  9. import type { TicketDuplicateDetectionItem } from '#shared/entities/ticket/types.ts'
  10. export interface TicketDuplicateDetectionPayload {
  11. count: number
  12. items: TicketDuplicateDetectionItem[]
  13. }
  14. export const useTicketDuplicateDetectionHandler = (
  15. showTicketDuplicateDetectionDialog: (
  16. data: TicketDuplicateDetectionPayload,
  17. ) => void,
  18. ): FormHandler => {
  19. const executeHandler = (
  20. execution: FormHandlerExecution,
  21. schemaData: ReactiveFormSchemData,
  22. changedField?: ChangedField,
  23. ) => {
  24. if (!schemaData.fields.ticket_duplicate_detection) return false
  25. if (
  26. execution === FormHandlerExecution.FieldChange &&
  27. (!changedField || changedField.name !== 'ticket_duplicate_detection')
  28. ) {
  29. return false
  30. }
  31. return true
  32. }
  33. const handleTicketDuplicateDetection: FormHandlerFunction = async (
  34. execution,
  35. reactivity,
  36. data,
  37. ) => {
  38. const { changedField } = data
  39. const { schemaData } = reactivity
  40. if (!executeHandler(execution, schemaData, changedField)) return
  41. const newFieldData =
  42. changedField?.newValue as unknown as TicketDuplicateDetectionPayload
  43. if (!newFieldData?.count) return
  44. showTicketDuplicateDetectionDialog(newFieldData)
  45. }
  46. return {
  47. execution: [FormHandlerExecution.Initial, FormHandlerExecution.FieldChange],
  48. callback: handleTicketDuplicateDetection,
  49. }
  50. }