useUserEdit.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { FormSchemaField } from '#shared/components/Form/types.ts'
  3. import { defineFormSchema } from '#shared/form/defineFormSchema.ts'
  4. import type { UserQuery } from '#shared/graphql/types.ts'
  5. import {
  6. EnumFormUpdaterId,
  7. EnumObjectManagerObjects,
  8. } from '#shared/graphql/types.ts'
  9. import { useApplicationStore } from '#shared/stores/application.ts'
  10. import type { ConfidentTake } from '#shared/types/utils.ts'
  11. import { useDialogObjectForm } from '#mobile/components/CommonDialogObjectForm/useDialogObjectForm.ts'
  12. import { useUserUpdateMutation } from '#mobile/pages/user/graphql/mutations/update.api.ts'
  13. export const useUserEdit = () => {
  14. const dialog = useDialogObjectForm('user-edit', EnumObjectManagerObjects.User)
  15. const schema = defineFormSchema(
  16. [
  17. {
  18. screen: 'edit',
  19. object: EnumObjectManagerObjects.User,
  20. },
  21. {
  22. name: 'active',
  23. required: true,
  24. screen: 'edit',
  25. object: EnumObjectManagerObjects.User,
  26. },
  27. ],
  28. { showDirtyMark: true },
  29. )
  30. const application = useApplicationStore()
  31. const openEditUserDialog = async (user: ConfidentTake<UserQuery, 'user'>) => {
  32. const formChangeFields: Record<string, Partial<FormSchemaField>> = {
  33. note: {
  34. props: {
  35. meta: {
  36. mentionText: {
  37. disabled: true,
  38. },
  39. mentionKnowledgeBase: {
  40. disabled: true,
  41. },
  42. mentionUser: {
  43. disabled: true,
  44. },
  45. },
  46. },
  47. },
  48. organization_id: {
  49. helpClass: '',
  50. },
  51. }
  52. dialog.openDialog({
  53. object: user,
  54. mutation: useUserUpdateMutation,
  55. schema,
  56. formChangeFields,
  57. onChangedField: (fieldName, newValue) => {
  58. if (
  59. fieldName === 'organization_id' &&
  60. application.config.ticket_organization_reassignment
  61. ) {
  62. formChangeFields.organization_id ||= {}
  63. let msg = __(
  64. "Attention! Changing the organization will update the user's most recent tickets to the new organization.",
  65. )
  66. let helpClass = 'text-yellow'
  67. if (user.organization?.internalId === newValue) {
  68. msg = ''
  69. helpClass = ''
  70. }
  71. formChangeFields.organization_id.help = msg
  72. formChangeFields.organization_id.helpClass = helpClass
  73. }
  74. },
  75. formUpdaterId: EnumFormUpdaterId.FormUpdaterUpdaterUserEdit,
  76. errorNotificationMessage: __('User could not be updated.'),
  77. })
  78. }
  79. return { openEditUserDialog }
  80. }