useEmailOutboundForm.ts 3.7 KB

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