"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { ProfileForm } from "@/components/profile-form"; import { AuthService } from "@/lib/auth-utils"; import { UserProfile, Team } from "@/lib/types"; import { Code2, LogOut, Edit, X, Home } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import Link from "next/link"; interface LoginPageProps {} export default function LoginPage({}: LoginPageProps) { const [teams, setTeams] = useState([]); const [loading, setLoading] = useState(true); const [authenticating, setAuthenticating] = useState(false); const [currentUser, setCurrentUser] = useState(null); const [isEditing, setIsEditing] = useState(false); const router = useRouter(); useEffect(() => { async function initialize() { try { // Vérifier si l'utilisateur est déjà connecté const user = await AuthService.getCurrentUser(); setCurrentUser(user); // Charger les équipes const teamsResponse = await fetch("/api/teams"); if (teamsResponse.ok) { const teamsData = await teamsResponse.json(); setTeams(teamsData); } } catch (error) { console.error("Error initializing login page:", error); } finally { setLoading(false); } } initialize(); }, []); const handleSubmit = async (profile: UserProfile) => { setAuthenticating(true); try { await AuthService.login(profile); if (isEditing) { // Si on modifie le profil existant, mettre à jour l'état setCurrentUser(profile); setIsEditing(false); } else { // Si c'est un nouveau login, rediriger router.push("/"); } } catch (error) { console.error("Login failed:", error); // Vous pouvez ajouter une notification d'erreur ici } finally { setAuthenticating(false); } }; const handleLogout = async () => { try { await AuthService.logout(); setCurrentUser(null); setIsEditing(false); } catch (error) { console.error("Logout failed:", error); } }; const handleEdit = () => { setIsEditing(true); }; const handleCancelEdit = () => { setIsEditing(false); }; if (loading) { return (

Chargement...

); } // Si l'utilisateur est connecté et qu'on ne modifie pas if (currentUser && !isEditing) { const currentTeam = teams.find((t) => t.id === currentUser.teamId); return (
PeakSkills

Vous êtes connecté

Gérez votre profil ou retournez à l'application

{/* Informations utilisateur */} Vos informations Profil actuellement connecté

{currentUser.firstName}

{currentUser.lastName}

{currentTeam?.name || "Équipe non trouvée"}

{/* Actions */}
); } // Sinon, afficher le formulaire (nouvel utilisateur ou modification) return (
PeakSkills

{isEditing ? "Modifier vos informations" : "Bienvenue sur PeakSkills"}

{isEditing ? "Mettez à jour vos informations personnelles" : "Évaluez vos compétences techniques et suivez votre progression"}

{isEditing && ( )}
{authenticating && (

{isEditing ? "Mise à jour en cours..." : "Connexion en cours..."}

)}
); }