useObjectAttributeFormFields.ts 2.0 KB

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