Contact.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <div>
  3. <div id="anchor" />
  4. <v-form
  5. v-if="!contactRequestSent"
  6. ref="form"
  7. validate-on="submit lazy"
  8. @submit.prevent="submit"
  9. >
  10. <v-container>
  11. <v-row>
  12. <v-col cols="12" md="6">
  13. <v-text-field
  14. v-model="email"
  15. :rules="[validateEmail]"
  16. :label="$t('contact_email')"
  17. required
  18. type="email"
  19. />
  20. </v-col>
  21. <v-col cols="12" md="6">
  22. <v-text-field
  23. v-model="name"
  24. :label="$t('contact_name')"
  25. required
  26. />
  27. </v-col>
  28. </v-row>
  29. <v-row>
  30. <v-col cols="12">
  31. <v-textarea
  32. v-model="message"
  33. :rules="[validateNonEmptyMessage]"
  34. :label="$t('contact_message')"
  35. required
  36. maxlength="400"
  37. />
  38. </v-col>
  39. </v-row>
  40. <v-row>
  41. <v-col cols="12" class="captcha-container">
  42. <AltchaValidation v-if="!appStore.altchaPayload"/>
  43. <v-card v-else class="pa-2">{{ $t('captcha_already_verified') }}</v-card>
  44. <v-checkbox
  45. v-model="honeyPotChecked"
  46. :rules="[validateCaptcha]"
  47. class="hidden-ctrl"
  48. />
  49. </v-col>
  50. </v-row>
  51. <!-- Submit Button -->
  52. <div class="d-flex flex-row justify-center">
  53. <v-btn
  54. type="submit"
  55. variant="outlined"
  56. :height="54"
  57. :width="180"
  58. class="submit-btn"
  59. >
  60. {{ $t('contact_submit') }}
  61. </v-btn>
  62. </div>
  63. <div v-if="errorMsg" class="error">
  64. {{ errorMsg }}
  65. </div>
  66. </v-container>
  67. </v-form>
  68. <div v-else class="confirmation-message d-flex flex-row justify-center">
  69. <v-card>
  70. <v-icon icon="fas fa-check mr-1" />
  71. {{ $t('contact_confirmation') }}
  72. </v-card>
  73. </div>
  74. </div>
  75. </template>
  76. <script setup lang="ts">
  77. import type { Ref } from '@vue/reactivity'
  78. const appStore = useAppStore()
  79. const form: Ref<HTMLElement | null> = ref(null)
  80. const contactRequestSent: Ref<boolean> = ref(false)
  81. const errorMsg: Ref<string | null> = ref(null)
  82. const i18n = useI18n()
  83. const email: Ref<string | null> = ref(null)
  84. const name: Ref<string | null> = ref(null)
  85. const message: Ref<string | null> = ref(null)
  86. // Honeypot checkbox (if checked: it's probably a bot)
  87. const honeyPotChecked: Ref<boolean> = ref(false)
  88. const validateEmail = (email: string | null) =>
  89. (!!email && /.+@.+\..+/.test(email)) || i18n.t("email_must_be_valid")
  90. const validateNonEmptyMessage = (message: string | null) =>
  91. (!!message && message.length > 10) || i18n.t("message_must_be_valid")
  92. const validateCaptcha = () =>
  93. !honeyPotChecked.value && appStore.altchaPayload !== null || i18n.t("captcha_must_be_validated")
  94. /**
  95. * Submits the contact form.
  96. *
  97. * This function validates the form and sets the value of a variable to indicate whether the form submission was successful.
  98. *
  99. * @function
  100. *
  101. * @returns {void}
  102. */
  103. const submit = async (): Promise<void> => {
  104. const { valid } = await form.value!.validate()
  105. if (!valid) {
  106. contactRequestSent.value = false
  107. return
  108. }
  109. const url = 'https://api.ogene.fr/api/contact';
  110. const headers = {
  111. 'Content-Type': 'application/ld+json'
  112. };
  113. const body = {
  114. "email": email.value,
  115. "name": name.value ?? '-',
  116. "message": message.value,
  117. "altchaPayload": appStore.altchaPayload
  118. };
  119. try {
  120. const response = await fetch(url, {
  121. method: 'POST',
  122. headers: headers,
  123. body: JSON.stringify(body)
  124. });
  125. if (!response.ok) {
  126. throw new Error(`HTTP error! status: ${response.status}`);
  127. }
  128. await response.json();
  129. } catch (error) {
  130. console.error('There was a problem with the fetch operation: ', error);
  131. }
  132. contactRequestSent.value = true
  133. errorMsg.value = null
  134. }
  135. </script>
  136. <style scoped lang="scss">
  137. .captcha-container {
  138. display: flex;
  139. flex-direction: column;
  140. align-items: center;
  141. margin: 24px 0;
  142. :deep(altcha-widget) {
  143. min-width: 280px;
  144. @media (max-width: 600px) {
  145. .altcha {
  146. margin: 0 auto;
  147. }
  148. }
  149. }
  150. }
  151. .confirmation-message .v-card {
  152. padding: 14px;
  153. }
  154. .hidden-ctrl {
  155. :deep(.v-input__control) {
  156. display: none;
  157. }
  158. }
  159. </style>