useObjectAttributeFormFields.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { FormSchemaField } from '#shared/components/Form/types.ts'
  3. import type {
  4. EnumObjectManagerObjects,
  5. ObjectManagerFrontendAttribute,
  6. } from '#shared/graphql/types.ts'
  7. import { transformResolvedFieldForScreen } from '../form/getFieldFromAttribute.ts'
  8. import { useObjectAttributesStore } from '../stores/objectAttributes.ts'
  9. export const useObjectAttributeFormFields = (skippedFields: string[] = []) => {
  10. const { getObjectAttributesForObject } = useObjectAttributesStore()
  11. const getFormFieldSchema = (
  12. name: string,
  13. object: EnumObjectManagerObjects,
  14. screen?: string,
  15. ) => {
  16. const objectAttributesObject = getObjectAttributesForObject(object)
  17. const resolvedField = (
  18. objectAttributesObject.formFieldAttributesLookup as unknown as Map<
  19. string,
  20. FormSchemaField
  21. >
  22. ).get(name)
  23. if (!screen) return resolvedField
  24. // We need to transform the resolved the field for the current screen (e.g. for the required information).
  25. const screenConfig = (
  26. objectAttributesObject.attributesLookup as unknown as Map<
  27. string,
  28. ObjectManagerFrontendAttribute
  29. >
  30. ).get(name)?.screens[screen]
  31. if (resolvedField && screenConfig) {
  32. transformResolvedFieldForScreen(screenConfig, resolvedField)
  33. }
  34. return resolvedField
  35. }
  36. const getFormFieldsFromScreen = (
  37. screen: string,
  38. object: EnumObjectManagerObjects,
  39. ) => {
  40. const screens = getObjectAttributesForObject(object)
  41. .screens as unknown as Record<string, string[]>
  42. if (!screens[screen]) return []
  43. const formFields: FormSchemaField[] = []
  44. screens[screen].forEach((attributeName) => {
  45. if (skippedFields.includes(attributeName)) {
  46. return
  47. }
  48. const formField = getFormFieldSchema(attributeName, object, screen)
  49. if (!formField) {
  50. return
  51. }
  52. formFields.push(formField)
  53. })
  54. return formFields
  55. }
  56. return {
  57. getFormFieldSchema,
  58. getFormFieldsFromScreen,
  59. }
  60. }