Form.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, validateFileSize]"
  49. label="Dépôt de CV*"
  50. accept=".pdf, .jpeg, .png"
  51. show-size
  52. required
  53. />
  54. <v-file-input
  55. id="jobApplicationMotivationLetter"
  56. v-model="motivationLetterUpload"
  57. :rules="[validateFileSize]"
  58. label="Dépôt de lettre de motivation"
  59. accept=".pdf, .jpeg, .png"
  60. show-size
  61. />
  62. <v-textarea
  63. id="jobApplicationMessage"
  64. v-model="jobApplication.message"
  65. :rules="[validateNonEmptyMessage, validateMessageLength]"
  66. label="Message*"
  67. required
  68. />
  69. <span class="remaining-cars-notice">{{ leftCars }} caractères restants</span>
  70. <div class="d-flex flex-column align-center mt-4">
  71. <!-- @see https://github.com/hCaptcha/vue-hcaptcha -->
  72. <LayoutCaptcha/>
  73. </div>
  74. </v-form>
  75. </v-card-text>
  76. <p class="text-right mr-6">
  77. * Champs obligatoires
  78. </p>
  79. <v-card-actions class="justify-center">
  80. <v-btn
  81. class="btn-more mb-4 submit"
  82. @click="submit"
  83. >
  84. Envoyer
  85. </v-btn>
  86. </v-card-actions>
  87. </v-card>
  88. </div>
  89. </template>
  90. <script setup lang="ts">
  91. import ContactRequest from "~/models/Maestro/ContactRequest";
  92. import { useEntityManager } from "~/composables/data/useEntityManager";
  93. import JobApplication from "~/models/Maestro/JobApplication";
  94. import FileUtils from "~/services/utils/FileUtils";
  95. const { em } = useEntityManager()
  96. const form: Ref<any | null> = ref(null)
  97. const jobApplicationSent: Ref<boolean> = ref(false)
  98. const emit = defineEmits(['submit'])
  99. //@ts-ignore
  100. const jobApplication: ContactRequest = reactive(em.newInstance(JobApplication))
  101. const resumeUpload = ref(null)
  102. const motivationLetterUpload = ref(null)
  103. // --- Validation ---
  104. const maxMessageLength = 2000
  105. const leftCars: ComputedRef<number> = computed(() =>
  106. maxMessageLength - (jobApplication.message ? jobApplication.message.length : 0)
  107. )
  108. // Taille maximum en Mo
  109. const maxFileSize = 5
  110. const validateName = (name: string | null) => !!name || "Le nom est obligatoire";
  111. const validateSurname = (surname: string | null) => !!surname || "Le prénom est obligatoire";
  112. const validateEmail = (email: string | null) =>
  113. (!!email && /.+@.+\..+/.test(email)) || "L'adresse e-mail doit être valide";
  114. const validatePhone = (email: string | null) =>
  115. (!!email && /^((\+|00)33\s?|0)[1-7]([\s.]?\d{2}){4}$/.test(email)) || "Le numéro de téléphone doit être valide";
  116. const validateResume = () =>
  117. resumeUpload.value !== null && resumeUpload.value[0] !== null || "Vous devez joindre un CV à l'un des formats indiqués";
  118. const validateFileSize = () =>
  119. resumeUpload.value !== null
  120. && resumeUpload.value[0] !== null
  121. //@ts-ignore
  122. && resumeUpload.value[0].size < (maxFileSize * 1024 * 1024)
  123. || "La taille du fichier ne doit pas dépasser " + maxFileSize + " Mo";
  124. const validateNonEmptyMessage = (message: string | null) =>
  125. (!!message && message.length > 0) ||
  126. "Le message ne peut pas être vide";
  127. const validateMessageLength = async (message: string | null) =>
  128. (!!message && message.length <= maxMessageLength) ||
  129. "Le message ne doit pas dépasser " + maxMessageLength + " caractères";
  130. /**
  131. * Soumet le formulaire de candidature (boite de dialogue)
  132. */
  133. const submit = async () => {
  134. const { valid } = await form.value.validate()
  135. if (!valid) {
  136. jobApplicationSent.value = false
  137. return
  138. }
  139. jobApplication.resume = (resumeUpload.value !== null && resumeUpload.value[0] !== null) ?
  140. {
  141. //@ts-ignore
  142. 'name': resumeUpload.value[0].name,
  143. 'content': await FileUtils.blobToBase64(resumeUpload.value[0])
  144. } : null
  145. jobApplication.motivationLetter = (motivationLetterUpload.value !== null && motivationLetterUpload.value[0] !== null) ?
  146. {
  147. //@ts-ignore
  148. 'name': motivationLetterUpload.value[0].name,
  149. 'content': await FileUtils.blobToBase64(motivationLetterUpload.value[0])
  150. } : null
  151. await em.persist(JobApplication, jobApplication)
  152. jobApplicationSent.value = true;
  153. emit('submit')
  154. };
  155. </script>
  156. <style scoped lang="scss">
  157. .submit {
  158. width: 100%;
  159. margin-bottom: 0 !important;
  160. height: 55px;
  161. background: var(--secondary-color);
  162. }
  163. .submit:hover {
  164. background-color: var(--on-neutral-color-extra-light);
  165. }
  166. </style>