Contact.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/>
  43. <v-checkbox
  44. v-model="honeyPotChecked"
  45. :rules="[validateCaptcha]"
  46. class="hidden-ctrl"
  47. />
  48. </v-col>
  49. </v-row>
  50. <!-- Submit Button -->
  51. <div class="d-flex flex-row justify-center">
  52. <v-btn
  53. type="submit"
  54. variant="outlined"
  55. :height="54"
  56. :width="180"
  57. class="submit-btn"
  58. >
  59. {{ $t('contact_submit') }}
  60. </v-btn>
  61. </div>
  62. <div v-if="errorMsg" class="error">
  63. {{ errorMsg }}
  64. </div>
  65. </v-container>
  66. </v-form>
  67. <div v-else class="confirmation-message d-flex flex-row justify-center">
  68. <v-card>
  69. <v-icon icon="fas fa-check mr-1" />
  70. {{ $t('contact_confirmation') }}
  71. </v-card>
  72. </div>
  73. </div>
  74. </template>
  75. <script setup lang="ts">
  76. import type { Ref } from '@vue/reactivity'
  77. const router = useRouter()
  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. appStore.isNoBot && !honeyPotChecked.value || 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. };
  118. try {
  119. const response = await fetch(url, {
  120. method: 'POST',
  121. headers: headers,
  122. body: JSON.stringify(body)
  123. });
  124. if (!response.ok) {
  125. throw new Error(`HTTP error! status: ${response.status}`);
  126. }
  127. await response.json();
  128. } catch (error) {
  129. console.error('There was a problem with the fetch operation: ', error);
  130. }
  131. contactRequestSent.value = true
  132. errorMsg.value = null
  133. // Défile vers le début de page pour afficher le message de confirmation
  134. setTimeout(() => router.push({ path: '', hash: '#anchor' }), 30)
  135. }
  136. </script>
  137. <style scoped lang="scss">
  138. .captcha-container {
  139. display: flex;
  140. flex-direction: column;
  141. align-items: center;
  142. margin: 24px 0;
  143. :deep(altcha-widget) {
  144. min-width: 280px;
  145. @media (max-width: 600px) {
  146. .altcha {
  147. margin: 0 auto;
  148. }
  149. }
  150. }
  151. }
  152. .confirmation-message .v-card {
  153. padding: 14px;
  154. }
  155. .hidden-ctrl {
  156. :deep(.v-input__control) {
  157. display: none;
  158. }
  159. }
  160. </style>