refactor: SSR on page teams and split getAdminData
This commit is contained in:
@@ -27,36 +27,12 @@ export function useSkillsManagement(
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const { toast } = useToast();
|
||||
|
||||
// Charger les skills depuis l'API si pas de skills initiales
|
||||
useEffect(() => {
|
||||
if (!initialSkills) {
|
||||
const fetchSkills = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const skillsData = await adminClient.getSkills();
|
||||
setSkills(skillsData);
|
||||
} catch (error) {
|
||||
console.error("Error fetching skills:", error);
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: "Impossible de charger les skills",
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
fetchSkills();
|
||||
}
|
||||
}, [initialSkills]);
|
||||
|
||||
const resetForm = () => {
|
||||
setSkillFormData({ name: "", categoryId: "", description: "", icon: "" });
|
||||
setEditingSkill(null);
|
||||
};
|
||||
|
||||
const handleCreateSkill = async () => {
|
||||
console.log("skillFormData", skillFormData);
|
||||
if (!skillFormData.name || !skillFormData.categoryId) {
|
||||
toast({
|
||||
title: "Erreur",
|
||||
|
||||
@@ -23,27 +23,6 @@ export function useTeamsManagement(
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const { toast } = useToast();
|
||||
|
||||
// Charger les teams depuis l'API
|
||||
const fetchTeams = async () => {
|
||||
try {
|
||||
const teamsData = await adminClient.getTeams();
|
||||
// Note: on garde les teams existantes pour la compatibilité
|
||||
// Les nouvelles teams créées via l'API seront visibles après rafraîchissement
|
||||
} catch (error) {
|
||||
console.error("Error fetching teams:", error);
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: "Impossible de charger les teams",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Charger les teams au montage du composant
|
||||
useEffect(() => {
|
||||
fetchTeams();
|
||||
}, []);
|
||||
|
||||
const resetForm = () => {
|
||||
setTeamFormData({ name: "", direction: "" });
|
||||
setEditingTeam(null);
|
||||
|
||||
@@ -4,9 +4,9 @@ import { Team } from "@/lib/types";
|
||||
import { adminClient } from "@/clients";
|
||||
import { User, UserFormData } from "@/clients/domains/admin-client";
|
||||
|
||||
export function useUsersManagement(teams: Team[]) {
|
||||
const [users, setUsers] = useState<User[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
export function useUsersManagement(teams: Team[], initialUsers?: any[]) {
|
||||
const [users, setUsers] = useState<User[]>(initialUsers || []);
|
||||
const [isLoading, setIsLoading] = useState(!initialUsers);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deletingUserId, setDeletingUserId] = useState<string | null>(null);
|
||||
const [userFormData, setUserFormData] = useState<UserFormData>({
|
||||
@@ -17,25 +17,6 @@ export function useUsersManagement(teams: Team[]) {
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const { toast } = useToast();
|
||||
|
||||
// Charger les utilisateurs depuis l'API
|
||||
const fetchUsers = async () => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const usersData = await adminClient.getUsers();
|
||||
setUsers(usersData);
|
||||
} catch (err: any) {
|
||||
setError(err.message || "Erreur lors du chargement des utilisateurs");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Charger les utilisateurs au montage du composant
|
||||
useEffect(() => {
|
||||
fetchUsers();
|
||||
}, []);
|
||||
|
||||
const resetForm = () => {
|
||||
setUserFormData({ firstName: "", lastName: "", teamId: "" });
|
||||
};
|
||||
@@ -52,15 +33,15 @@ export function useUsersManagement(teams: Team[]) {
|
||||
|
||||
try {
|
||||
setIsSubmitting(true);
|
||||
await adminClient.createUser(userFormData);
|
||||
const newUser = await adminClient.createUser(userFormData);
|
||||
toast({
|
||||
title: "Succès",
|
||||
description: "Utilisateur créé avec succès",
|
||||
});
|
||||
|
||||
// Ajouter le nouvel utilisateur à la liste locale
|
||||
setUsers([...users, newUser]);
|
||||
resetForm();
|
||||
// Rafraîchir la liste
|
||||
await fetchUsers();
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
toast({
|
||||
@@ -128,6 +109,5 @@ export function useUsersManagement(teams: Team[]) {
|
||||
resetForm,
|
||||
handleCreateUser,
|
||||
handleDeleteUser,
|
||||
fetchUsers,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user