Contact.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 mail = useMail()
  69. const form: Ref<HTMLElement | null> = ref(null)
  70. const contactRequestSent: Ref<boolean> = ref(false)
  71. const errorMsg: Ref<string | null> = ref(null)
  72. const email: Ref<string | null> = ref(null)
  73. const name: Ref<string | null> = ref(null)
  74. const message: Ref<string | null> = ref(null)
  75. const validateEmail = (email: string | null) =>
  76. (!!email && /.+@.+\..+/.test(email)) || "L'adresse e-mail doit être valide"
  77. const validateNonEmptyMessage = (message: string | null) =>
  78. (!!message && message.length > 0) || 'Le message ne peut pas être vide'
  79. /**
  80. * Submits the contact form.
  81. *
  82. * This function validates the form and sets the value of a variable to indicate whether the form submission was successful.
  83. *
  84. * @function
  85. *
  86. * @returns {void}
  87. */
  88. const submit = async (): Promise<void> => {
  89. const { valid } = await form.value!.validate()
  90. if (!valid) {
  91. contactRequestSent.value = false
  92. return
  93. }
  94. const url = 'https://api.ogene.fr/api/contact';
  95. const headers = {
  96. 'Content-Type': 'application/ld+json'
  97. };
  98. const body = {
  99. "email": email.value,
  100. "name": name.value,
  101. "message": message.value
  102. };
  103. try {
  104. const response = await fetch(url, {
  105. method: 'POST',
  106. headers: headers,
  107. body: JSON.stringify(body)
  108. });
  109. if (!response.ok) {
  110. throw new Error(`HTTP error! status: ${response.status}`);
  111. }
  112. await response.json();
  113. } catch (error) {
  114. console.error('There was a problem with the fetch operation: ', error);
  115. }
  116. contactRequestSent.value = true
  117. errorMsg.value = null
  118. // Défile vers le début de page pour afficher le message de confirmation
  119. setTimeout(() => router.push({ path: '', hash: '#anchor' }), 30)
  120. }
  121. </script>
  122. <style scoped lang="scss">
  123. </style>