useEmailInboundForm.ts 5.4 KB

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