useUserCreate.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useRouter } from 'vue-router'
  3. import { useUserAddMutation } from '#shared/entities/user/graphql/mutations/add.api.ts'
  4. import { defineFormSchema } from '#shared/form/defineFormSchema.ts'
  5. import type { User, UserAddPayload } from '#shared/graphql/types.ts'
  6. import {
  7. EnumFormUpdaterId,
  8. EnumObjectManagerObjects,
  9. } from '#shared/graphql/types.ts'
  10. import { useDialogObjectForm } from '#mobile/components/CommonDialogObjectForm/useDialogObjectForm.ts'
  11. interface UserCreateOptions {
  12. onUserCreated?: (user: User) => void
  13. }
  14. export const useUserCreate = (options: UserCreateOptions = {}) => {
  15. const dialogCreate = useDialogObjectForm(
  16. 'user-create',
  17. EnumObjectManagerObjects.User,
  18. )
  19. const schema = defineFormSchema([
  20. {
  21. screen: 'create',
  22. object: EnumObjectManagerObjects.User,
  23. },
  24. {
  25. name: 'active',
  26. required: true,
  27. screen: 'create',
  28. object: EnumObjectManagerObjects.User,
  29. },
  30. ])
  31. const router = useRouter()
  32. const onSuccess = (data: { userAdd: UserAddPayload }) => {
  33. const { internalId } = data.userAdd.user as User
  34. router.push(`/users/${internalId}`)
  35. }
  36. const openCreateUserDialog = async () => {
  37. dialogCreate.openDialog({
  38. mutation: useUserAddMutation,
  39. schema,
  40. onSuccess: options.onUserCreated
  41. ? (query: { userAdd: UserAddPayload }) =>
  42. options.onUserCreated!(query.userAdd.user!)
  43. : onSuccess,
  44. formUpdaterId: EnumFormUpdaterId.FormUpdaterUpdaterUserCreate,
  45. errorNotificationMessage: __('User could not be created.'),
  46. })
  47. }
  48. return { openCreateUserDialog }
  49. }