MovieCommentForm.vue
4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<script lang="ts" setup>
import type { Comment } from "~/type/commentForm";
// #region --Import--.
import { useVuelidate } from "@vuelidate/core";
import { helpers, maxLength, maxValue, minLength, minValue, required } from "@vuelidate/validators";
// #endregion
// #region --Props--.
withDefaults(defineProps<{
isSubmitting?: boolean;
}>(), {
isSubmitting: false,
});
// #endregion
// #region --Emit--.
const emit = defineEmits<{
eventSubmit: [formData: any];
}>();
// #endregion
// #region --Data/ref--.
const initialState: Comment = {
username: "",
message: "",
rating: 5,
};
// Validation rules
const rules = {
username: {
required: helpers.withMessage("Le nom d'utilisateur est requis", required),
minLength: helpers.withMessage("Le nom d'utilisateur doit contenir au moins 3 caractères", minLength(3)),
maxLength: helpers.withMessage("Le nom d'utilisateur ne peut pas dépasser 50 caractères", maxLength(50)),
alpha: helpers.withMessage(
"Le nom d'utilisateur ne peut contenir que des lettres",
// eslint-disable-next-line regexp/no-obscure-range
helpers.regex(/^[a-zA-ZÀ-ÿ\s]+$/),
),
},
message: {
required: helpers.withMessage("Le message est requis", required),
minLength: helpers.withMessage("Le message doit contenir au moins 3 caractères", minLength(3)),
maxLength: helpers.withMessage("Le message ne peut pas dépasser 500 caractères", maxLength(500)),
},
rating: {
required: helpers.withMessage("La notation est requise", required),
minValue: helpers.withMessage("Le message ne être inférieure à 0", minValue(0)),
maxValue: helpers.withMessage("Le message ne être suppérieur à 10", maxValue(10)),
},
};
const formData = reactive({
...initialState,
});
const v$ = useVuelidate(rules, formData);
// #endregion
// const errormessages = computed(() => {
// return v$.value.message.$errors.map((e) => e.$message);
// });
// #region --Function--.
async function submitComment() {
emit("eventSubmit", formData);
}
function clear() {
v$.value.$reset();
formData.username = initialState.username;
formData.message = initialState.message;
formData.rating = initialState.rating;
}
function handleMessageEvent(event: string) {
formData.message = event;
// todo : revoir ici la validation manquante (dû au retour de TinyMCE).
v$.value.message.$touch();
// console.log(formData.message.replace(/<[^>]*>/g, ''));
// console.log(formData.message.replace(/(<([^>]+)>)/ig, ''));
}
// #endregion
</script>
<template>
<section>
<VForm>
<v-text-field
v-model="formData.username"
:error-messages="v$.username.$errors.map((e) => e.$message) as readonly string[]"
label="nom d'utilisateur"
placeholder="nom d'utilisateur"
required
@blur="v$.username.$touch()"
@input="v$.username.$touch()"
/>
<v-text-field
v-model="formData.rating"
:error-messages="v$.rating.$errors.map((e) => e.$message) as readonly string[]"
label="Note (1-10)"
placeholder=""
required
type="number"
@blur="v$.rating.$touch"
@input="v$.rating.$touch"
/>
<!-- <pre>{{ errormessages }}</pre> -->
<ui-components-tiny-mce-field-editor
:error-message="v$?.message?.$errors[0]?.$message ? (v$.message.$errors[0].$message as string) : ''"
:model-value="formData.message"
@update-model-value="handleMessageEvent"
/>
<v-btn
class="mt-6 mr-4"
color="primary"
@click="
async () => {
const validForm = await v$.$validate();
if (validForm) submitComment();
}
"
>
<span
v-if="isSubmitting"
class="flex items-center justify-center"
>
<span class="w-5 h-5 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" />
Envoi en cours...
</span>
<span v-else>Publier le commentaire</span>
</v-btn>
<v-btn
class="mt-6 mr-4"
color="primary"
@click="clear"
>
effacer
</v-btn>
</VForm>
</section>
</template>
<style scoped></style>