FieldCustomerWrapper.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { markRaw, defineAsyncComponent, nextTick } from 'vue'
  4. import type { SelectValue } from '#shared/components/CommonSelect/types.ts'
  5. import type { AutoCompleteProps } from '#shared/components/Form/fields/FieldAutocomplete/types.ts'
  6. import { AutocompleteSearchUserDocument } from '#shared/components/Form/fields/FieldCustomer/graphql/queries/autocompleteSearch/user.api.ts'
  7. import type { AutoCompleteCustomerUserOption } from '#shared/components/Form/fields/FieldCustomer/types.ts'
  8. import type { FormFieldContext } from '#shared/components/Form/types/field.ts'
  9. import type { User } from '#shared/graphql/types.ts'
  10. import type { ObjectLike } from '#shared/types/utils.ts'
  11. import { closeDialog } from '#mobile/composables/useDialog.ts'
  12. import { useUserCreate } from '#mobile/entities/user/composables/useUserCreate.ts'
  13. import FieldCustomerOptionIcon from './FieldCustomerOptionIcon.vue'
  14. const FieldAutoCompleteInput = defineAsyncComponent(
  15. () =>
  16. import(
  17. '#mobile/components/Form/fields/FieldAutoComplete/FieldAutoCompleteInput.vue'
  18. ),
  19. )
  20. interface Props {
  21. context: FormFieldContext<
  22. AutoCompleteProps & {
  23. options?: AutoCompleteCustomerUserOption[]
  24. }
  25. >
  26. }
  27. const props = defineProps<Props>()
  28. const buildEntityOption = (entity: User) => {
  29. return {
  30. value: entity.internalId,
  31. label: entity.fullname || entity.phone || entity.login,
  32. heading: entity.organization?.name,
  33. user: entity,
  34. }
  35. }
  36. const { openCreateUserDialog } = useUserCreate({
  37. async onUserCreated(user) {
  38. const { props: nodeProps } = props.context.node
  39. // If the user is not in options, add it
  40. if (
  41. !nodeProps.options?.some(
  42. (v: AutoCompleteCustomerUserOption) => v.value === user.internalId,
  43. )
  44. ) {
  45. nodeProps.options = [
  46. ...(nodeProps.options || []),
  47. buildEntityOption(user),
  48. ]
  49. }
  50. await nextTick()
  51. props.context.node.input(user.internalId, false)
  52. closeDialog(`field-auto-complete-${props.context.id}`)
  53. },
  54. })
  55. Object.assign(props.context, {
  56. optionIconComponent: markRaw(FieldCustomerOptionIcon),
  57. initialOptionBuilder: (
  58. initialEntityObject: ObjectLike,
  59. value: SelectValue,
  60. context: Props['context'],
  61. ) => {
  62. if (!context.belongsToObjectField || !initialEntityObject) return null
  63. const belongsToObject = initialEntityObject[context.belongsToObjectField]
  64. if (!belongsToObject) return null
  65. return buildEntityOption(belongsToObject)
  66. },
  67. actionIcon: 'new-customer',
  68. actionLabel: __('Create new customer'),
  69. gqlQuery: AutocompleteSearchUserDocument,
  70. onActionClick: openCreateUserDialog,
  71. })
  72. </script>
  73. <template>
  74. <FieldAutoCompleteInput :context="context" v-bind="$attrs" />
  75. </template>