feat : refacto de la partie calendrier + ajout de validation sur les formulaires + ajout des jours fériés

This commit is contained in:
2026-02-09 14:25:18 +01:00
parent 03f5552dd4
commit c1025d6066
18 changed files with 1303 additions and 427 deletions

View File

@@ -0,0 +1,201 @@
<template>
<AppDrawer v-model="drawerOpen" title="Nouvelle absence">
<form class="space-y-4" @submit.prevent="handleSubmit">
<div>
<label class="text-md font-semibold text-neutral-700" for="employee">
Employé <span class="text-red-600">*</span>
</label>
<select
id="employee"
v-model="absenceForm.employeeId"
:class="employeeFieldClass"
>
<option value="" disabled>Choisir un employé</option>
<option v-for="employee in employees" :key="employee.id" :value="employee.id">
{{ employee.firstName }} {{ employee.lastName }}
</option>
</select>
<p v-if="showEmployeeError" class="mt-1 text-sm text-red-600">
L'employé est obligatoire.
</p>
</div>
<div>
<label class="text-md font-semibold text-neutral-700" for="type">
Type d'absence <span class="text-red-600">*</span>
</label>
<select
id="type"
v-model="absenceForm.typeId"
:class="typeFieldClass"
>
<option value="" disabled>Choisir un type</option>
<option v-for="type in absenceTypes" :key="type.id" :value="type.id">
{{ type.label }} ({{ type.code }})
</option>
</select>
<p v-if="showTypeError" class="mt-1 text-sm text-red-600">
Le type d'absence est obligatoire.
</p>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="text-md font-semibold text-neutral-700" for="start-date">Date de début</label>
<input
id="start-date"
v-model="absenceForm.startDate"
type="date"
class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-md text-neutral-900"
/>
</div>
<div>
<label class="text-md font-semibold text-neutral-700" for="end-date">Date de fin</label>
<input
id="end-date"
v-model="absenceForm.endDate"
type="date"
class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-md text-neutral-900"
/>
</div>
</div>
<div>
<label class="text-md font-semibold text-neutral-700" for="comment">Commentaire</label>
<textarea
id="comment"
v-model="absenceForm.comment"
rows="3"
class="mt-2 w-full rounded-md border border-neutral-300 px-3 py-2 text-md text-neutral-900"
/>
</div>
<div class="flex justify-end gap-3 pt-2">
<button
v-if="editingAbsence"
type="button"
class="rounded-lg border border-red-200 px-4 py-2 text-md font-semibold text-red-600 hover:bg-red-50"
@click="handleDelete"
>
Supprimer
</button>
<button
type="button"
class="rounded-lg border border-neutral-200 px-4 py-2 text-md font-semibold text-neutral-700 hover:bg-neutral-100"
@click="handleCancel"
>
Annuler
</button>
<button
type="submit"
class="rounded-lg bg-primary-500 px-4 py-2 text-md font-semibold text-white hover:bg-secondary-500"
:class="submitButtonClass"
>
Enregistrer
</button>
</div>
</form>
</AppDrawer>
</template>
<script setup lang="ts">
import { computed, reactive, toRef, watch } from 'vue'
import type { Employee } from '~/services/dto/employee'
import type { AbsenceType } from '~/services/dto/absence-type'
import type { Absence } from '~/services/dto/absence'
import AppDrawer from '~/components/AppDrawer.vue'
const props = defineProps<{
modelValue: boolean
employees: Employee[]
absenceTypes: AbsenceType[]
form: {
employeeId: number | ''
typeId: number | ''
startDate: string
endDate: string
comment: string
}
editingAbsence: Absence | null
isSubmitting: boolean
}>()
const emit = defineEmits<{
(event: 'update:modelValue', value: boolean): void
(event: 'submit'): void
(event: 'delete'): void
(event: 'cancel'): void
}>()
const drawerOpen = computed({
get: () => props.modelValue,
set: (value: boolean) => emit('update:modelValue', value)
})
const absenceForm = toRef(props, 'form')
const editingAbsence = toRef(props, 'editingAbsence')
const validationTouched = reactive({
employee: false,
type: false
})
const isEmployeeValid = computed(() => absenceForm.value.employeeId !== '')
const isTypeValid = computed(() => absenceForm.value.typeId !== '')
const isFormValid = computed(() => isEmployeeValid.value && isTypeValid.value)
const showEmployeeError = computed(
() => validationTouched.employee && !isEmployeeValid.value
)
const showTypeError = computed(
() => validationTouched.type && !isTypeValid.value
)
const submitButtonClass = computed(() => {
if (props.isSubmitting || !isFormValid.value) {
return 'opacity-50 cursor-not-allowed'
}
return ''
})
const baseSelectClass =
'mt-2 w-full rounded-md border bg-white px-3 py-2 text-md text-neutral-900'
const employeeFieldClass = computed(() => {
if (showEmployeeError.value) {
return `${baseSelectClass} border-red-500`
}
return `${baseSelectClass} border-neutral-300`
})
const typeFieldClass = computed(() => {
if (showTypeError.value) {
return `${baseSelectClass} border-red-500`
}
return `${baseSelectClass} border-neutral-300`
})
watch(
() => props.modelValue,
(isOpen) => {
if (!isOpen) {
validationTouched.employee = false
validationTouched.type = false
}
}
)
const handleSubmit = () => {
validationTouched.employee = true
validationTouched.type = true
if (!isEmployeeValid.value || !isTypeValid.value) return
emit('submit')
}
const handleDelete = () => {
emit('delete')
}
const handleCancel = () => {
emit('cancel')
emit('update:modelValue', false)
}
</script>

View File

@@ -0,0 +1,172 @@
<template>
<AppDrawer v-model="drawerOpen" title="Imprimer les absences">
<form class="space-y-4" @submit.prevent="handleSubmit">
<div class="grid grid-cols-2 gap-4">
<div>
<label class="text-md font-semibold text-neutral-700" for="print-from">
Date de début <span class="text-red-600">*</span>
</label>
<input
id="print-from"
v-model="printForm.from"
type="date"
:class="fromFieldClass"
/>
<p v-if="showFromError" class="mt-1 text-sm text-red-600">
La date de début est obligatoire.
</p>
</div>
<div>
<label class="text-md font-semibold text-neutral-700" for="print-to">
Date de fin <span class="text-red-600">*</span>
</label>
<input
id="print-to"
v-model="printForm.to"
type="date"
:class="toFieldClass"
/>
<p v-if="showToError" class="mt-1 text-sm text-red-600">
La date de fin est obligatoire.
</p>
</div>
</div>
<div class="space-y-2">
<p class="text-md font-semibold text-neutral-700">
Sites <span class="text-red-600">*</span>
</p>
<div class="flex flex-wrap gap-4 rounded-md border border-neutral-300 px-3 py-2">
<div v-for="site in sites" :key="site.id" class="flex items-center gap-2">
<div :style="{ backgroundColor: site.color }" class="h-4 w-4 rounded"></div>
<label class="text-md" :for="`print-site-${site.id}`">{{ site.name }}</label>
<input
:id="`print-site-${site.id}`"
v-model="printForm.siteIds"
:value="site.id"
type="checkbox"
class="h-4 w-4"
/>
</div>
</div>
<p v-if="showSitesError" class="text-sm text-red-600">
Sélectionne au moins un site.
</p>
</div>
<div class="flex justify-end gap-3 pt-2">
<button
type="button"
class="rounded-lg border border-neutral-200 px-4 py-2 text-md font-semibold text-neutral-700 hover:bg-neutral-100"
@click="handleCancel"
>
Annuler
</button>
<button
type="submit"
class="rounded-lg bg-primary-500 px-4 py-2 text-md font-semibold text-white hover:bg-secondary-500"
:class="submitButtonClass"
>
Imprimer
</button>
</div>
</form>
</AppDrawer>
</template>
<script setup lang="ts">
import { computed, reactive, toRef, watch } from 'vue'
import AppDrawer from '~/components/AppDrawer.vue'
type SiteOption = {
id: number
name: string
color: string
}
const props = defineProps<{
modelValue: boolean
sites: SiteOption[]
printForm: {
from: string
to: string
siteIds: number[]
}
}>()
const emit = defineEmits<{
(event: 'update:modelValue', value: boolean): void
(event: 'submit'): void
(event: 'cancel'): void
}>()
const drawerOpen = computed({
get: () => props.modelValue,
set: (value: boolean) => emit('update:modelValue', value)
})
const printForm = toRef(props, 'printForm')
const validationTouched = reactive({
from: false,
to: false,
sites: false
})
const isFromValid = computed(() => printForm.value.from.trim() !== '')
const isToValid = computed(() => printForm.value.to.trim() !== '')
const isSitesValid = computed(() => printForm.value.siteIds.length > 0)
const isFormValid = computed(
() => isFromValid.value && isToValid.value && isSitesValid.value
)
const showFromError = computed(() => validationTouched.from && !isFromValid.value)
const showToError = computed(() => validationTouched.to && !isToValid.value)
const showSitesError = computed(() => validationTouched.sites && !isSitesValid.value)
const baseInputClass =
'mt-2 w-full rounded-md border px-3 py-2 text-md text-neutral-900'
const fromFieldClass = computed(() => {
if (showFromError.value) {
return `${baseInputClass} border-red-500`
}
return `${baseInputClass} border-neutral-300`
})
const toFieldClass = computed(() => {
if (showToError.value) {
return `${baseInputClass} border-red-500`
}
return `${baseInputClass} border-neutral-300`
})
const submitButtonClass = computed(() => {
if (!isFormValid.value) {
return 'opacity-50 cursor-not-allowed'
}
return ''
})
const handleSubmit = () => {
validationTouched.from = true
validationTouched.to = true
validationTouched.sites = true
if (!isFormValid.value) return
emit('submit')
}
const handleCancel = () => {
emit('cancel')
emit('update:modelValue', false)
}
watch(
() => props.modelValue,
(isOpen) => {
if (!isOpen) {
validationTouched.from = false
validationTouched.to = false
validationTouched.sites = false
}
}
)
</script>

View File

@@ -0,0 +1,76 @@
<template>
<div class="max-h-[80vh] overflow-auto rounded-lg border border-neutral-200 bg-white">
<div class="min-w-[900px]">
<div class="grid" :style="gridStyle">
<div
class="sticky left-0 z-20 border-b border-neutral-200 bg-tertiary-500 px-4 py-3 text-md font-semibold text-neutral-700"
>
Employés
</div>
<div
v-for="day in daysInMonth"
:key="day.date"
class="border-b border-neutral-200 bg-tertiary-500 px-2 py-3 text-center text-xs font-semibold text-neutral-700"
>
<div>{{ day.label }}</div>
<div class="text-[10px] text-neutral-500">{{ day.weekday }}</div>
</div>
<template v-for="employee in visibleEmployees" :key="employee.id">
<div
class="sticky left-0 z-10 border-b border-neutral-100 px-4 py-3 text-md font-semibold text-black"
:style="{ backgroundColor: employee.site?.color ?? '#304998' }"
>
{{ formatEmployeeName(employee) }}
</div>
<div
v-for="day in daysInMonth"
:key="employee.id + '-' + day.date"
class="border-b border-neutral-100 px-2 py-2 text-center text-xs text-neutral-800"
>
<button
type="button"
class="flex h-8 w-full items-center justify-center rounded-md border border-neutral-200 text-[11px] font-semibold text-neutral-800 hover:border-primary-500/40"
:class="isHolidayDate(day.date) ? 'cursor-not-allowed opacity-80' : ''"
:style="getCellStyle(employee.id, day.date)"
:disabled="isHolidayDate(day.date)"
@click="handleCellClick(employee, day.date)"
>
<span v-if="getCellCode(employee.id, day.date)">
{{ getCellCode(employee.id, day.date) }}
</span>
</button>
</div>
</template>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import type { Employee } from '~/services/dto/employee'
type DayInfo = {
date: string
label: string
weekday: string
}
defineProps<{
daysInMonth: DayInfo[]
visibleEmployees: Employee[]
gridStyle: Record<string, string>
getCellStyle: (employeeId: number, date: string) => Record<string, string> | undefined
getCellCode: (employeeId: number, date: string) => string
formatEmployeeName: (employee: Employee) => string
isHolidayDate: (date: string) => boolean
}>()
const emit = defineEmits<{
(event: 'cell-click', employee: Employee, date: string): void
}>()
const handleCellClick = (employee: Employee, date: string) => {
emit('cell-click', employee, date)
}
</script>