useTicketDuplicateDetectionHandler.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. export type TicketDuplicateDetectionItem = [
  10. id: number,
  11. number: string,
  12. title: string,
  13. ]
  14. export interface TicketDuplicateDetectionPayload {
  15. count: number
  16. items: TicketDuplicateDetectionItem[]
  17. }
  18. export const useTicketDuplicateDetectionHandler = (
  19. showTicketDuplicateDetectionDialog: (
  20. data: TicketDuplicateDetectionPayload,
  21. ) => void,
  22. ): FormHandler => {
  23. const executeHandler = (
  24. execution: FormHandlerExecution,
  25. schemaData: ReactiveFormSchemData,
  26. changedField?: ChangedField,
  27. ) => {
  28. if (!schemaData.fields.ticket_duplicate_detection) return false
  29. if (
  30. execution === FormHandlerExecution.FieldChange &&
  31. (!changedField || changedField.name !== 'ticket_duplicate_detection')
  32. ) {
  33. return false
  34. }
  35. return true
  36. }
  37. const handleTicketDuplicateDetection: FormHandlerFunction = async (
  38. execution,
  39. reactivity,
  40. data,
  41. ) => {
  42. const { changedField } = data
  43. const { schemaData } = reactivity
  44. if (!executeHandler(execution, schemaData, changedField)) return
  45. const newFieldData =
  46. changedField?.newValue as unknown as TicketDuplicateDetectionPayload
  47. if (!newFieldData?.count) return
  48. showTicketDuplicateDetectionDialog(newFieldData)
  49. }
  50. return {
  51. execution: [FormHandlerExecution.Initial, FormHandlerExecution.FieldChange],
  52. callback: handleTicketDuplicateDetection,
  53. }
  54. }