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

@ -0,0 +1,66 @@
'use client'
import React, { useState, useEffect } from 'react';
import { fetchProfileRoles, updateProfileRoles, deleteProfileRoles } from '@/app/actions/authAction';
import logger from '@/utils/logger';
import { useEstablishment } from '@/context/EstablishmentContext';
import DjangoCSRFToken from '@/components/DjangoCSRFToken';
import { useCsrfToken } from '@/context/CsrfContext';
import ProfileDirectory from '@/components/ProfileDirectory';
import { BE_AUTH_PROFILES_ROLES_URL } from '@/utils/Url';
export default function Page() {
const [profileRoles, setProfileRoles] = useState([]);
const csrfToken = useCsrfToken();
const { selectedEstablishmentId } = useEstablishment();
useEffect(() => {
if (selectedEstablishmentId) {
// Fetch data for profileRoles
handleProfiles();
}
}, [selectedEstablishmentId]);
const handleProfiles = () => {
fetchProfileRoles(selectedEstablishmentId)
.then(data => {
setProfileRoles(data);
})
.catch(error => logger.error('Error fetching profileRoles:', error));
};
const handleEdit = (profileRole) => {
const updatedData = { ...profileRole, is_active: !profileRole.is_active };
return updateProfileRoles(profileRole.id, updatedData, csrfToken)
.then(data => {
setProfileRoles(prevState => prevState.map(item => item.id === profileRole.id ? data : item));
return data;
})
.catch(error => {
logger.error('Error editing data:', error);
throw error;
});
};
const handleDelete = (id) => {
return deleteProfileRoles(id, csrfToken)
.then(() => {
setProfileRoles(prevState => prevState.filter(item => item.id !== id));
logger.debug("Profile deleted successfully:", id);
})
.catch(error => {
logger.error('Error deleting profile:', error);
throw error;
});
};
return (
<div className='p-8'>
<DjangoCSRFToken csrfToken={csrfToken} />
<div className="w-full p-4">
<ProfileDirectory profileRoles={profileRoles} handleActivateProfile={handleEdit} handleDeleteProfile={handleDelete} />
</div>
</div>
);
}