Form.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. label="Dépôt de CV*"
  49. accept=".pdf, .jpeg, .png"
  50. required
  51. />
  52. <v-file-input
  53. id="jobApplicationMotivationLetter"
  54. v-model="motivationLetterUpload"
  55. label="Dépôt de lettre de motivation"
  56. accept=".pdf, .jpeg, .png"
  57. />
  58. <v-textarea
  59. id="jobApplicationMessage"
  60. v-model="jobApplication.message"
  61. :rules="[validateNonEmptyMessage, validateMessageLength]"
  62. label="Message*"
  63. required
  64. />
  65. <span class="remaining-cars-notice">{{ leftCars }} caractères restants</span>
  66. </v-form>
  67. </v-card-text>
  68. <p class="text-right mr-6">
  69. * Champs obligatoires
  70. </p>
  71. <v-card-actions class="justify-center">
  72. <v-btn
  73. class="btn-more mb-4"
  74. @click="submit"
  75. >
  76. Envoyer
  77. </v-btn>
  78. </v-card-actions>
  79. </v-card>
  80. </div>
  81. </template>
  82. <script setup lang="ts">
  83. import ContactRequest from "~/models/Maestro/ContactRequest";
  84. import { useEntityManager } from "~/composables/data/useEntityManager";
  85. import JobApplication from "~/models/Maestro/JobApplication";
  86. import FileUtils from "~/services/utils/FileUtils";
  87. const { em } = useEntityManager()
  88. const form: Ref<any | null> = ref(null)
  89. const jobApplicationSent: Ref<boolean> = ref(false)
  90. const emit = defineEmits(['submit'])
  91. //@ts-ignore
  92. const jobApplication: ContactRequest = reactive(em.newInstance(JobApplication))
  93. const resumeUpload = ref(null)
  94. const motivationLetterUpload = ref(null)
  95. // --- Validation ---
  96. const maxMessageLength = 2000
  97. const leftCars: ComputedRef<number> = computed(() =>
  98. maxMessageLength - (jobApplication.message ? jobApplication.message.length : 0)
  99. )
  100. const validateName = (name: string | null) => !!name || "Le nom est obligatoire";
  101. const validateSurname = (surname: string | null) => !!surname || "Le prénom est obligatoire";
  102. const validateEmail = (email: string | null) =>
  103. (!!email && /.+@.+\..+/.test(email)) || "L'adresse e-mail doit être valide";
  104. const validatePhone = (email: string | null) =>
  105. (!!email && /^((\+|00)33\s?|0)[1-7]([\s.]?\d{2}){4}$/.test(email)) || "Le numéro de téléphone doit être valide";
  106. const validateNonEmptyMessage = (message: string | null) =>
  107. (!!message && message.length > 0) ||
  108. "Le message ne peut pas être vide";
  109. const validateMessageLength = async (message: string | null) =>
  110. (!!message && message.length <= maxMessageLength) ||
  111. "Le message ne doit pas dépasser " + maxMessageLength + " caractères";
  112. /**
  113. * Soumet le formulaire de candidature (boite de dialogue)
  114. */
  115. const submit = async () => {
  116. const { valid } = await form.value.validate()
  117. if (!valid) {
  118. jobApplicationSent.value = false
  119. return
  120. }
  121. jobApplication.resume = (resumeUpload.value !== null && resumeUpload.value[0] !== null) ?
  122. {
  123. //@ts-ignore
  124. 'name': resumeUpload.value[0].name,
  125. 'content': await FileUtils.blobToBase64(resumeUpload.value[0])
  126. } : null
  127. jobApplication.motivationLetter = (motivationLetterUpload.value !== null && motivationLetterUpload.value[0] !== null) ?
  128. {
  129. //@ts-ignore
  130. 'name': motivationLetterUpload.value[0].name,
  131. 'content': await FileUtils.blobToBase64(motivationLetterUpload.value[0])
  132. } : null
  133. await em.persist(JobApplication, jobApplication)
  134. jobApplicationSent.value = true;
  135. emit('submit')
  136. };
  137. </script>
  138. <style scoped lang="scss">
  139. </style>