Form.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <div>
  3. <v-card
  4. v-if="!jobApplicationSent"
  5. >
  6. <v-card-title
  7. class="text-center"
  8. >
  9. Formulaire de Candidature
  10. </v-card-title>
  11. <v-card-text>
  12. <v-form
  13. ref="form"
  14. validate-on="submit lazy"
  15. @submit.prevent="submit"
  16. >
  17. <v-text-field
  18. id="jobApplicationName"
  19. v-model="jobApplication.name"
  20. :rules="[validateName]"
  21. label="Nom*"
  22. required
  23. />
  24. <v-text-field
  25. id="jobApplicationSurname"
  26. v-model="jobApplication.surname"
  27. :rules="[validateSurname]"
  28. label="Prénom*"
  29. required
  30. />
  31. <v-text-field
  32. id="jobApplicationPhone"
  33. v-model="jobApplication.phone"
  34. :rules="[validatePhone]"
  35. label="Téléphone*"
  36. required
  37. />
  38. <v-text-field
  39. id="jobApplicationEmail"
  40. v-model="jobApplication.email"
  41. :rules="[validateEmail]"
  42. label="Email*"
  43. required
  44. />
  45. <v-file-input
  46. id="jobApplicationResume"
  47. v-model="resumeUpload"
  48. :rules="[validateResume]"
  49. label="Dépôt de CV*"
  50. accept=".pdf, .jpeg, .png"
  51. required
  52. />
  53. <v-file-input
  54. id="jobApplicationMotivationLetter"
  55. v-model="motivationLetterUpload"
  56. label="Dépôt de lettre de motivation"
  57. accept=".pdf, .jpeg, .png"
  58. />
  59. <v-textarea
  60. id="jobApplicationMessage"
  61. v-model="jobApplication.message"
  62. :rules="[validateNonEmptyMessage, validateMessageLength]"
  63. label="Message*"
  64. required
  65. />
  66. <span class="remaining-cars-notice">{{ leftCars }} caractères restants</span>
  67. <div class="d-flex flex-column align-center mt-4">
  68. <!-- @see https://github.com/hCaptcha/vue-hcaptcha -->
  69. <LayoutCaptcha/>
  70. </div>
  71. </v-form>
  72. </v-card-text>
  73. <p class="text-right mr-6">
  74. * Champs obligatoires
  75. </p>
  76. <v-card-actions class="justify-center">
  77. <v-btn
  78. class="btn-more mb-4"
  79. @click="submit"
  80. >
  81. Envoyer
  82. </v-btn>
  83. </v-card-actions>
  84. </v-card>
  85. </div>
  86. </template>
  87. <script setup lang="ts">
  88. import ContactRequest from "~/models/Maestro/ContactRequest";
  89. import { useEntityManager } from "~/composables/data/useEntityManager";
  90. import JobApplication from "~/models/Maestro/JobApplication";
  91. import FileUtils from "~/services/utils/FileUtils";
  92. const { em } = useEntityManager()
  93. const form: Ref<any | null> = ref(null)
  94. const jobApplicationSent: Ref<boolean> = ref(false)
  95. const emit = defineEmits(['submit'])
  96. //@ts-ignore
  97. const jobApplication: ContactRequest = reactive(em.newInstance(JobApplication))
  98. const resumeUpload = ref(null)
  99. const motivationLetterUpload = ref(null)
  100. // --- Validation ---
  101. const maxMessageLength = 2000
  102. const leftCars: ComputedRef<number> = computed(() =>
  103. maxMessageLength - (jobApplication.message ? jobApplication.message.length : 0)
  104. )
  105. const validateName = (name: string | null) => !!name || "Le nom est obligatoire";
  106. const validateSurname = (surname: string | null) => !!surname || "Le prénom est obligatoire";
  107. const validateEmail = (email: string | null) =>
  108. (!!email && /.+@.+\..+/.test(email)) || "L'adresse e-mail doit être valide";
  109. const validatePhone = (email: string | null) =>
  110. (!!email && /^((\+|00)33\s?|0)[1-7]([\s.]?\d{2}){4}$/.test(email)) || "Le numéro de téléphone doit être valide";
  111. const validateResume = (surname: string | null) =>
  112. resumeUpload.value !== null && resumeUpload.value[0] !== null || "Vous devez joindre un CV à l'un des formats indiqués";
  113. const validateNonEmptyMessage = (message: string | null) =>
  114. (!!message && message.length > 0) ||
  115. "Le message ne peut pas être vide";
  116. const validateMessageLength = async (message: string | null) =>
  117. (!!message && message.length <= maxMessageLength) ||
  118. "Le message ne doit pas dépasser " + maxMessageLength + " caractères";
  119. /**
  120. * Soumet le formulaire de candidature (boite de dialogue)
  121. */
  122. const submit = async () => {
  123. const { valid } = await form.value.validate()
  124. if (!valid) {
  125. jobApplicationSent.value = false
  126. return
  127. }
  128. jobApplication.resume = (resumeUpload.value !== null && resumeUpload.value[0] !== null) ?
  129. {
  130. //@ts-ignore
  131. 'name': resumeUpload.value[0].name,
  132. 'content': await FileUtils.blobToBase64(resumeUpload.value[0])
  133. } : null
  134. jobApplication.motivationLetter = (motivationLetterUpload.value !== null && motivationLetterUpload.value[0] !== null) ?
  135. {
  136. //@ts-ignore
  137. 'name': motivationLetterUpload.value[0].name,
  138. 'content': await FileUtils.blobToBase64(motivationLetterUpload.value[0])
  139. } : null
  140. await em.persist(JobApplication, jobApplication)
  141. jobApplicationSent.value = true;
  142. emit('submit')
  143. };
  144. </script>
  145. <style scoped lang="scss">
  146. </style>