Contact.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. <!-- Submit Button -->
  41. <div class="d-flex flex-row justify-center">
  42. <v-btn
  43. type="submit"
  44. variant="outlined"
  45. :height="54"
  46. :width="180"
  47. class="submit-btn"
  48. >
  49. {{ $t('contact_submit') }}
  50. </v-btn>
  51. </div>
  52. <div v-if="errorMsg" class="error">
  53. {{ errorMsg }}
  54. </div>
  55. </v-container>
  56. </v-form>
  57. <div v-else class="confirmation-message d-flex flex-row justify-center">
  58. <v-card>
  59. <v-icon icon="fas fa-check mr-1" />
  60. {{ $t('contact_confirmation') }}
  61. </v-card>
  62. </div>
  63. </div>
  64. </template>
  65. <script setup lang="ts">
  66. import type { Ref } from '@vue/reactivity'
  67. const router = useRouter()
  68. const form: Ref<HTMLElement | null> = ref(null)
  69. const contactRequestSent: Ref<boolean> = ref(false)
  70. const errorMsg: Ref<string | null> = ref(null)
  71. const email: Ref<string | null> = ref(null)
  72. const name: Ref<string | null> = ref(null)
  73. const message: Ref<string | null> = ref(null)
  74. const validateEmail = (email: string | null) =>
  75. (!!email && /.+@.+\..+/.test(email)) || "L'adresse e-mail doit être valide"
  76. const validateNonEmptyMessage = (message: string | null) =>
  77. (!!message && message.length > 0) || 'Le message ne peut pas être vide'
  78. /**
  79. * Submits the contact form.
  80. *
  81. * This function validates the form and sets the value of a variable to indicate whether the form submission was successful.
  82. *
  83. * @function
  84. *
  85. * @returns {void}
  86. */
  87. const submit = async (): Promise<void> => {
  88. const { valid } = await form.value!.validate()
  89. if (!valid) {
  90. contactRequestSent.value = false
  91. return
  92. }
  93. const url = 'https://api.ogene.fr/api/contact';
  94. const headers = {
  95. 'Content-Type': 'application/ld+json'
  96. };
  97. const body = {
  98. "email": email.value,
  99. "name": name.value,
  100. "message": message.value
  101. };
  102. try {
  103. const response = await fetch(url, {
  104. method: 'POST',
  105. headers: headers,
  106. body: JSON.stringify(body)
  107. });
  108. if (!response.ok) {
  109. throw new Error(`HTTP error! status: ${response.status}`);
  110. }
  111. await response.json();
  112. } catch (error) {
  113. console.error('There was a problem with the fetch operation: ', error);
  114. }
  115. contactRequestSent.value = true
  116. errorMsg.value = null
  117. // Défile vers le début de page pour afficher le message de confirmation
  118. setTimeout(() => router.push({ path: '', hash: '#anchor' }), 30)
  119. }
  120. </script>
  121. <style scoped lang="scss">
  122. </style>