Contact.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 appStore = useAppStore()
  78. const form: Ref<HTMLElement | null> = ref(null)
  79. const contactRequestSent: Ref<boolean> = ref(false)
  80. const errorMsg: Ref<string | null> = ref(null)
  81. const i18n = useI18n()
  82. const email: Ref<string | null> = ref(null)
  83. const name: Ref<string | null> = ref(null)
  84. const message: Ref<string | null> = ref(null)
  85. // Honeypot checkbox (if checked: it's probably a bot)
  86. const honeyPotChecked: Ref<boolean> = ref(false)
  87. const validateEmail = (email: string | null) =>
  88. (!!email && /.+@.+\..+/.test(email)) || i18n.t("email_must_be_valid")
  89. const validateNonEmptyMessage = (message: string | null) =>
  90. (!!message && message.length > 10) || i18n.t("message_must_be_valid")
  91. const validateCaptcha = () =>
  92. !honeyPotChecked.value && appStore.altchaPayload !== null || i18n.t("captcha_must_be_validated")
  93. /**
  94. * Submits the contact form.
  95. *
  96. * This function validates the form and sets the value of a variable to indicate whether the form submission was successful.
  97. *
  98. * @function
  99. *
  100. * @returns {void}
  101. */
  102. const submit = async (): Promise<void> => {
  103. const { valid } = await form.value!.validate()
  104. if (!valid) {
  105. contactRequestSent.value = false
  106. return
  107. }
  108. const url = 'https://api.ogene.fr/api/contact';
  109. const headers = {
  110. 'Content-Type': 'application/ld+json'
  111. };
  112. const body = {
  113. "email": email.value,
  114. "name": name.value ?? '-',
  115. "message": message.value,
  116. "altchaPayload": appStore.altchaPayload
  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. }
  134. </script>
  135. <style scoped lang="scss">
  136. .captcha-container {
  137. display: flex;
  138. flex-direction: column;
  139. align-items: center;
  140. margin: 24px 0;
  141. :deep(altcha-widget) {
  142. min-width: 280px;
  143. @media (max-width: 600px) {
  144. .altcha {
  145. margin: 0 auto;
  146. }
  147. }
  148. }
  149. }
  150. .confirmation-message .v-card {
  151. padding: 14px;
  152. }
  153. .hidden-ctrl {
  154. :deep(.v-input__control) {
  155. display: none;
  156. }
  157. }
  158. </style>