feat: Création d'un annuaire / mise à jour du subscribe

This commit is contained in:
N3WT DE COMPET
2025-03-14 19:51:35 +01:00
parent cd9c10a88a
commit 6bd5704983
22 changed files with 585 additions and 185 deletions

View File

@ -1,5 +1,5 @@
import React, { useState, useEffect } from 'react';
import { Plus, Edit3, Trash2, GraduationCap, Check, X, Hand } from 'lucide-react';
import { Plus, Edit3, Trash2, GraduationCap, Check, X, Hand, Search } from 'lucide-react';
import Table from '@/components/Table';
import Popup from '@/components/Popup';
import ToggleSwitch from '@/components/ToggleSwitch';
@ -11,6 +11,8 @@ import InputText from '@/components/InputText';
import SpecialityItem from '@/components/Structure/Configuration/SpecialityItem';
import TeacherItem from './TeacherItem';
import logger from '@/utils/logger';
import { fetchProfiles } from '@/app/actions/authAction';
import { useEstablishment } from '@/context/EstablishmentContext';
const ItemTypes = {
SPECIALITY: 'speciality',
@ -103,14 +105,40 @@ const TeachersSection = ({ teachers, setTeachers, specialities, handleCreate, ha
const [removePopupMessage, setRemovePopupMessage] = useState("");
const [removePopupOnConfirm, setRemovePopupOnConfirm] = useState(() => {});
const [confirmPopupVisible, setConfirmPopupVisible] = useState(false);
const [confirmPopupMessage, setConfirmPopupMessage] = useState("");
const [confirmPopupOnConfirm, setConfirmPopupOnConfirm] = useState(() => {});
const { selectedEstablishmentId } = useEstablishment();
const handleSelectProfile = (profile) => {
setNewTeacher((prevData) => ({
...prevData,
selectedProfile: profile,
}));
setFormData((prevData) => ({
...prevData,
selectedProfile: profile
}));
setConfirmPopupMessage(`Vous êtes sur le point de rattacher l'enseignant ${newTeacher?.first_name} ${newTeacher?.last_name} au profil ${profile.email} ID = ${profile.id}.`);
setConfirmPopupOnConfirm(() => () => {
setConfirmPopupVisible(false);
});
setConfirmPopupVisible(true);
};
const handleCancelConfirmation = () => {
setConfirmPopupVisible(false);
};
// Récupération des messages d'erreur
const getError = (field) => {
return localErrors?.[field]?.[0];
};
const handleAddTeacher = () => {
setNewTeacher({ id: Date.now(), last_name: '', first_name: '', email: '', specialities: [], droit: 0 });
setFormData({ last_name: '', first_name: '', email: '', specialities: [], droit: 0 });
setNewTeacher({ id: Date.now(), last_name: '', first_name: '', selectedProfile: null, specialities: [], droit: 0 });
setFormData({ last_name: '', first_name: '', selectedProfile: null, specialities: [], droit: 0});
};
const handleRemoveTeacher = (id) => {
@ -124,43 +152,32 @@ const TeachersSection = ({ teachers, setTeachers, specialities, handleCreate, ha
};
const handleSaveNewTeacher = () => {
if (formData.last_name && formData.first_name && formData.email) {
if (formData.last_name && formData.first_name && formData.selectedProfile) {
const data = {
email: formData.email,
password: 'Provisoire01!',
username: formData.email,
is_active: 1,
droit: formData.droit,
last_name: formData.last_name,
first_name: formData.first_name,
profile_role_data: {
role_type: formData.droit,
establishment: selectedEstablishmentId,
is_active: true,
profile: formData.selectedProfile.id
},
specialities: formData.specialities
};
createProfile(data, csrfToken)
.then(response => {
logger.debug('Success:', response);
if (response.id) {
let idProfil = response.id;
newTeacher.associated_profile = idProfil;
handleCreate(newTeacher)
.then((createdTeacher) => {
setTeachers([createdTeacher, ...teachers]);
setNewTeacher(null);
setLocalErrors({});
})
.catch((error) => {
logger.error('Error:', error.message);
if (error.details) {
logger.error('Form errors:', error.details);
setLocalErrors(error.details);
}
});
}
setLocalErrors({});
})
.catch((error) => {
logger.error('Error:', error.message);
if (error.details) {
handleCreate(data)
.then((createdTeacher) => {
setTeachers([createdTeacher, ...teachers]);
setNewTeacher(null);
setLocalErrors({});
})
.catch((error) => {
logger.error('Error:', error.message);
if (error.details) {
logger.error('Form errors:', error.details);
setLocalErrors(error.details);
}
});
}
});
} else {
setPopupMessage("Tous les champs doivent être remplis et valides");
setPopupVisible(true);
@ -260,7 +277,6 @@ const TeachersSection = ({ teachers, setTeachers, specialities, handleCreate, ha
<div className="flex justify-center space-x-2">
<InputText
name="last_name"
label="nom"
value={currentData.last_name}
onChange={handleChange}
placeholder="Nom de l'enseignant"
@ -268,7 +284,6 @@ const TeachersSection = ({ teachers, setTeachers, specialities, handleCreate, ha
/>
<InputText
name="first_name"
label="prénom"
value={currentData.first_name}
onChange={handleChange}
placeholder="Prénom de l'enseignant"
@ -276,16 +291,26 @@ const TeachersSection = ({ teachers, setTeachers, specialities, handleCreate, ha
/>
</div>
);
case 'EMAIL':
return (
<InputText
name="email"
value={currentData.email}
onChange={handleChange}
placeholder="Email de l'enseignant"
errorMsg={getError('email')}
/>
);
case 'EMAIL':
return (
<div className="flex items-center space-x-2">
{currentData.selectedProfile ? (
<span className="text-gray-900">
{currentData.selectedProfile.email}
</span>
) : (
<span className="text-gray-500 italic">
Rechercher un profil existant
</span>
)}
<button
type="button"
onClick={() => setDirectoryPopupVisible(true)}
>
<Search className="w-5 h-5 text-emerald-500 hover:text-emerald-700"/>
</button>
</div>
);
case 'SPECIALITES':
return (
<SpecialitiesDropZone teacher={currentData} handleSpecialitiesChange={handleSpecialitiesChange} specialities={specialities} isEditing={isEditing || isCreating} />
@ -339,9 +364,9 @@ const TeachersSection = ({ teachers, setTeachers, specialities, handleCreate, ha
</div>
);
case 'ADMINISTRATEUR':
if (teacher.associated_profile) {
const badgeClass = teacher.droit === 1 ? 'bg-red-100 text-red-600' : 'bg-blue-100 text-blue-600';
const label = teacher.droit === 1 ? 'OUI' : 'NON';
if (teacher.associated_profile_email) {
const badgeClass = teacher.role_type.role_type === 1 ? 'bg-red-100 text-red-600' : 'bg-blue-100 text-blue-600';
const label = teacher.role_type.role_type === 1 ? 'OUI' : 'NON';
return (
<div key={teacher.id} className="flex justify-center items-center space-x-2">
<span className={`px-3 py-1 rounded-full font-bold ${badgeClass}`}>