useEmailInboundForm.ts 5.5 KB

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