useEmailOutboundForm.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { ShallowRef } from 'vue'
  3. import { shallowRef, 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 { EmailOutboundData } from '../types/email-inbound-outbound.ts'
  11. export const useEmailOutboundForm = () => {
  12. const formEmailOutbound: ShallowRef<FormRef | undefined> = shallowRef()
  13. const { updateFieldValues, values, formSetErrors } =
  14. useForm<EmailOutboundData>(formEmailOutbound)
  15. const emailOutboundFormChangeFields = reactive<
  16. Record<string, Partial<FormSchemaField>>
  17. >({
  18. sslVerify: {},
  19. })
  20. const emailOutboundFormOnChanged = (
  21. fieldName: string,
  22. newValue: FormFieldValue,
  23. ) => {
  24. if (fieldName === 'port') {
  25. const disabled = Boolean(
  26. newValue && !(newValue === '465' || newValue === '587'),
  27. )
  28. emailOutboundFormChangeFields.sslVerify = {
  29. disabled,
  30. }
  31. updateFieldValues({
  32. sslVerify: !disabled,
  33. })
  34. }
  35. }
  36. const emailOutboundSchema = [
  37. {
  38. isLayout: true,
  39. element: 'div',
  40. attrs: {
  41. class: 'grid grid-cols-2 gap-y-2.5 gap-x-3',
  42. },
  43. children: [
  44. {
  45. type: 'group',
  46. name: 'outbound',
  47. isGroupOrList: true,
  48. children: [
  49. {
  50. name: 'adapter',
  51. label: __('Send Mails via'),
  52. type: 'select',
  53. outerClass: 'col-span-2',
  54. required: true,
  55. },
  56. {
  57. if: '$values.adapter === "smtp"',
  58. isLayout: true,
  59. element: 'div',
  60. attrs: {
  61. class: 'grid grid-cols-2 gap-y-2.5 gap-x-3 col-span-2',
  62. },
  63. children: [
  64. {
  65. name: 'host',
  66. label: __('Host'),
  67. type: 'text',
  68. outerClass: 'col-span-2',
  69. props: {
  70. maxLength: 120,
  71. },
  72. required: true,
  73. },
  74. {
  75. name: 'user',
  76. label: __('User'),
  77. type: 'text',
  78. outerClass: 'col-span-2',
  79. props: {
  80. maxLength: 120,
  81. },
  82. required: true,
  83. },
  84. {
  85. name: 'password',
  86. label: __('Password'),
  87. type: 'password',
  88. outerClass: 'col-span-2',
  89. props: {
  90. maxLength: 120,
  91. },
  92. required: true,
  93. },
  94. {
  95. name: 'port',
  96. label: __('Port'),
  97. type: 'text',
  98. outerClass: 'col-span-1',
  99. validation: 'number',
  100. props: {
  101. maxLength: 6,
  102. },
  103. required: true,
  104. },
  105. {
  106. name: 'sslVerify',
  107. label: __('SSL verification'),
  108. type: 'toggle',
  109. outerClass: 'col-span-1',
  110. wrapperClass: 'mt-6',
  111. value: true,
  112. props: {
  113. variants: {
  114. true: 'yes',
  115. false: 'no',
  116. },
  117. },
  118. },
  119. ],
  120. },
  121. ],
  122. },
  123. ],
  124. },
  125. ]
  126. return {
  127. formEmailOutbound,
  128. emailOutboundSchema,
  129. emailOutboundFormOnChanged,
  130. emailOutboundFormChangeFields,
  131. updateEmailOutboundFieldValues: updateFieldValues,
  132. formEmailOutboundSetErrors: formSetErrors,
  133. formEmailOutboundValues: values,
  134. }
  135. }