useEmailInboundForm.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. import { shallowRef, computed, ref, reactive } from 'vue'
  3. import type {
  4. FormFieldValue,
  5. FormRef,
  6. FormSchemaField,
  7. FormValues,
  8. } from '#shared/components/Form/types.ts'
  9. import { useForm } from '#shared/components/Form/useForm.ts'
  10. import type { ChannelEmailInboundMailboxStats } from '#shared/graphql/types.ts'
  11. import type {
  12. EmailInboundData,
  13. EmailInboundMetaInformation,
  14. EmailInboundMetaInformationNextAction,
  15. } from '../types/email-inbound-outbound.ts'
  16. import type { ShallowRef } from 'vue'
  17. export const useEmailInboundForm = () => {
  18. const formEmailInbound: ShallowRef<FormRef | undefined> = shallowRef()
  19. const { values, updateFieldValues, formSetErrors, onChangedField } =
  20. useForm<EmailInboundData>(formEmailInbound)
  21. const metaInformationInbound = ref<Maybe<EmailInboundMetaInformation>>(null)
  22. const updateMetaInformationInbound = (
  23. data: ChannelEmailInboundMailboxStats,
  24. nextAction: EmailInboundMetaInformationNextAction,
  25. ) => {
  26. metaInformationInbound.value = {
  27. contentMessages: data.contentMessages || 0,
  28. nextAction,
  29. }
  30. }
  31. const inboundSSLOptions = computed(() => {
  32. const options = [
  33. {
  34. value: 'off',
  35. label: __('No SSL'),
  36. },
  37. {
  38. value: 'ssl',
  39. label: __('SSL'),
  40. },
  41. ]
  42. if (values.value.adapter === 'imap') {
  43. options.push({
  44. value: 'starttls',
  45. label: __('STARTTLS'),
  46. })
  47. }
  48. return options
  49. })
  50. const emailInboundFormChangeFields = reactive<
  51. Record<string, Partial<FormSchemaField>>
  52. >({
  53. sslVerify: {},
  54. port: {},
  55. })
  56. onChangedField('ssl', (newValue: FormFieldValue) => {
  57. const disabled = Boolean(newValue === 'off')
  58. emailInboundFormChangeFields.sslVerify = {
  59. disabled,
  60. }
  61. const newValues: FormValues = {
  62. sslVerify: !disabled,
  63. }
  64. if (newValue === 'off') {
  65. newValues.port = 143
  66. } else if (newValue === 'ssl') {
  67. newValues.port = 993
  68. }
  69. updateFieldValues(newValues)
  70. })
  71. const emailInboundSchema = [
  72. {
  73. isLayout: true,
  74. element: 'div',
  75. attrs: {
  76. class: 'grid grid-cols-2 gap-y-2.5 gap-x-3',
  77. },
  78. children: [
  79. {
  80. type: 'group',
  81. name: 'inbound',
  82. isGroupOrList: true,
  83. children: [
  84. {
  85. name: 'adapter',
  86. label: __('Type'),
  87. type: 'select',
  88. outerClass: 'col-span-2',
  89. required: true,
  90. },
  91. {
  92. name: 'host',
  93. label: __('Host'),
  94. type: 'text',
  95. outerClass: 'col-span-2',
  96. props: {
  97. maxLength: 120,
  98. },
  99. required: true,
  100. },
  101. {
  102. name: 'user',
  103. label: __('User'),
  104. type: 'text',
  105. outerClass: 'col-span-2',
  106. props: {
  107. maxLength: 120,
  108. },
  109. required: true,
  110. },
  111. {
  112. name: 'password',
  113. label: __('Password'),
  114. type: 'password',
  115. outerClass: 'col-span-2',
  116. props: {
  117. maxLength: 120,
  118. },
  119. required: true,
  120. },
  121. {
  122. name: 'ssl',
  123. label: __('SSL/STARTTLS'),
  124. type: 'select',
  125. outerClass: 'col-span-1',
  126. value: 'ssl',
  127. options: inboundSSLOptions,
  128. },
  129. {
  130. name: 'sslVerify',
  131. label: __('SSL verification'),
  132. type: 'toggle',
  133. outerClass: 'col-span-1',
  134. wrapperClass: 'mt-6',
  135. value: true,
  136. props: {
  137. variants: {
  138. true: 'yes',
  139. false: 'no',
  140. },
  141. },
  142. },
  143. {
  144. name: 'port',
  145. label: __('Port'),
  146. type: 'text',
  147. outerClass: 'col-span-1',
  148. validation: 'number',
  149. props: {
  150. maxLength: 6,
  151. },
  152. value: 993,
  153. required: true,
  154. },
  155. {
  156. if: '$values.adapter === "imap"',
  157. name: 'folder',
  158. label: __('Folder'),
  159. type: 'text',
  160. outerClass: 'col-span-1',
  161. props: {
  162. maxLength: 120,
  163. },
  164. },
  165. {
  166. if: '$values.adapter === "imap"',
  167. name: 'keepOnServer',
  168. label: __('Keep messages on server'),
  169. type: 'toggle',
  170. outerClass: 'col-span-2',
  171. value: false,
  172. props: {
  173. variants: {
  174. true: 'yes',
  175. false: 'no',
  176. },
  177. },
  178. },
  179. ],
  180. },
  181. ],
  182. },
  183. ]
  184. return {
  185. formEmailInbound,
  186. emailInboundSchema,
  187. formEmailInboundValues: values,
  188. updateEmailInboundFieldValues: updateFieldValues,
  189. formEmailInboundSetErrors: formSetErrors,
  190. metaInformationInbound,
  191. emailInboundFormChangeFields,
  192. updateMetaInformationInbound,
  193. }
  194. }