214 lines
6.7 KiB
TypeScript
214 lines
6.7 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { ProfileForm } from "@/components/profile-form";
|
|
import { authClient } from "@/clients";
|
|
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";
|
|
|
|
interface LoginFormWrapperProps {
|
|
teams: Team[];
|
|
initialUser?: UserProfile | null;
|
|
}
|
|
|
|
export function LoginFormWrapper({
|
|
teams,
|
|
initialUser,
|
|
}: LoginFormWrapperProps) {
|
|
const [authenticating, setAuthenticating] = useState(false);
|
|
const [currentUser, setCurrentUser] = useState<UserProfile | null>(
|
|
initialUser || null
|
|
);
|
|
const [isEditing, setIsEditing] = useState(false);
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (initialUser) {
|
|
setCurrentUser(initialUser);
|
|
}
|
|
}, [initialUser]);
|
|
|
|
const handleSubmit = async (profile: UserProfile) => {
|
|
setAuthenticating(true);
|
|
try {
|
|
await authClient.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 authClient.logout();
|
|
setCurrentUser(null);
|
|
setIsEditing(false);
|
|
} catch (error) {
|
|
console.error("Logout failed:", error);
|
|
}
|
|
};
|
|
|
|
const handleEdit = () => {
|
|
setIsEditing(true);
|
|
};
|
|
|
|
const handleCancelEdit = () => {
|
|
setIsEditing(false);
|
|
};
|
|
|
|
// Si l'utilisateur est connecté et qu'on ne modifie pas
|
|
if (currentUser && !isEditing) {
|
|
const currentTeam = teams.find((t) => t.id === currentUser.teamId);
|
|
|
|
return (
|
|
<div className="text-center mb-12">
|
|
<div className="inline-flex items-center gap-2 px-4 py-2 rounded-full bg-white/5 border border-white/10 backdrop-blur-sm mb-6">
|
|
<Code2 className="h-4 w-4 text-blue-400" />
|
|
<span className="text-sm font-medium text-slate-200">PeakSkills</span>
|
|
</div>
|
|
|
|
<h1 className="text-4xl font-bold mb-4 text-white">
|
|
Vous êtes connecté
|
|
</h1>
|
|
<p className="text-lg text-slate-400 mb-8">
|
|
Gérez votre profil ou retournez à l'application
|
|
</p>
|
|
|
|
<div className="max-w-2xl mx-auto space-y-6">
|
|
{/* Informations utilisateur */}
|
|
<Card className="bg-white/5 border-white/10 backdrop-blur-sm">
|
|
<CardHeader>
|
|
<CardTitle className="text-white">Vos informations</CardTitle>
|
|
<CardDescription className="text-slate-400">
|
|
Profil actuellement connecté
|
|
</CardDescription>
|
|
</CardHeader>
|
|
<CardContent className="space-y-4">
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label className="text-sm font-medium text-slate-300">
|
|
Prénom
|
|
</label>
|
|
<p className="text-white">{currentUser.firstName}</p>
|
|
</div>
|
|
<div>
|
|
<label className="text-sm font-medium text-slate-300">
|
|
Nom
|
|
</label>
|
|
<p className="text-white">{currentUser.lastName}</p>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label className="text-sm font-medium text-slate-300">
|
|
Équipe
|
|
</label>
|
|
<p className="text-white">
|
|
{currentTeam?.name || "Équipe non trouvée"}
|
|
</p>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Actions */}
|
|
<div className="flex gap-4 justify-center">
|
|
<Button
|
|
onClick={() => router.push("/")}
|
|
className="bg-blue-500 hover:bg-blue-600 text-white"
|
|
>
|
|
<Home className="w-4 h-4 mr-2" />
|
|
Retour à l'accueil
|
|
</Button>
|
|
<Button
|
|
onClick={handleEdit}
|
|
variant="outline"
|
|
className="border-white/20 text-white hover:bg-white/10"
|
|
>
|
|
<Edit className="w-4 h-4 mr-2" />
|
|
Modifier le profil
|
|
</Button>
|
|
<Button
|
|
onClick={handleLogout}
|
|
variant="outline"
|
|
className="border-red-500/50 text-red-400 hover:bg-red-500/10"
|
|
>
|
|
<LogOut className="w-4 h-4 mr-2" />
|
|
Se déconnecter
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Sinon, afficher le formulaire (nouvel utilisateur ou modification)
|
|
return (
|
|
<div className="text-center mb-12">
|
|
<div className="inline-flex items-center gap-2 px-4 py-2 rounded-full bg-white/5 border border-white/10 backdrop-blur-sm mb-6">
|
|
<Code2 className="h-4 w-4 text-blue-400" />
|
|
<span className="text-sm font-medium text-slate-200">PeakSkills</span>
|
|
</div>
|
|
|
|
<h1 className="text-4xl font-bold mb-4 text-white">
|
|
{isEditing ? "Modifier vos informations" : "Bienvenue sur PeakSkills"}
|
|
</h1>
|
|
<p className="text-lg text-slate-400 mb-8">
|
|
{isEditing
|
|
? "Mettez à jour vos informations personnelles"
|
|
: "Évaluez vos compétences techniques et suivez votre progression"}
|
|
</p>
|
|
|
|
{isEditing && (
|
|
<Button
|
|
onClick={handleCancelEdit}
|
|
variant="outline"
|
|
className="mb-8 border-white/20 text-white hover:bg-white/10"
|
|
>
|
|
<X className="w-4 h-4 mr-2" />
|
|
Annuler
|
|
</Button>
|
|
)}
|
|
|
|
<div className="max-w-2xl mx-auto">
|
|
<div className="relative">
|
|
{authenticating && (
|
|
<div className="absolute inset-0 bg-black/20 backdrop-blur-sm z-10 flex items-center justify-center rounded-lg">
|
|
<div className="text-center">
|
|
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-400 mx-auto mb-2"></div>
|
|
<p className="text-white text-sm">
|
|
{isEditing
|
|
? "Mise à jour en cours..."
|
|
: "Connexion en cours..."}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)}
|
|
<ProfileForm
|
|
teams={teams}
|
|
onSubmit={handleSubmit}
|
|
initialProfile={isEditing && currentUser ? currentUser : undefined}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|