"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { createClient } from "@/lib/supabase/client"; import Button from "@/components/ui/Button"; import Input from "@/components/ui/Input"; import Card from "@/components/ui/Card"; import type { Profile } from "@/types/database.types"; export default function ProfilPage() { const router = useRouter(); const [profile, setProfile] = useState(null); const [fullName, setFullName] = useState(""); const [loading, setLoading] = useState(false); const [saving, setSaving] = useState(false); const [message, setMessage] = useState<{ type: "success" | "error"; text: string; } | null>(null); // Changement de mot de passe const [newPassword, setNewPassword] = useState(""); const [confirmNewPassword, setConfirmNewPassword] = useState(""); const [passwordSaving, setPasswordSaving] = useState(false); useEffect(() => { const loadProfile = async () => { setLoading(true); const supabase = createClient(); const { data: { user }, } = await supabase.auth.getUser(); if (!user) return; const { data } = await supabase .from("profiles") .select("*") .eq("id", user.id) .single() as { data: Profile | null }; if (data) { setProfile(data); setFullName(data.full_name || ""); } setLoading(false); }; loadProfile(); }, []); const handleSaveProfile = async (e: React.FormEvent) => { e.preventDefault(); setSaving(true); setMessage(null); try { const supabase = createClient(); const { error } = await supabase .from("profiles") .update({ full_name: fullName } as never) .eq("id", profile!.id); if (error) throw error; setMessage({ type: "success", text: "Profil mis a jour !" }); router.refresh(); } catch { setMessage({ type: "error", text: "Erreur lors de la mise a jour du profil.", }); } finally { setSaving(false); } }; const handleChangePassword = async (e: React.FormEvent) => { e.preventDefault(); setPasswordSaving(true); setMessage(null); if (newPassword !== confirmNewPassword) { setMessage({ type: "error", text: "Les mots de passe ne correspondent pas.", }); setPasswordSaving(false); return; } if (newPassword.length < 8) { setMessage({ type: "error", text: "Le mot de passe doit contenir au moins 8 caracteres.", }); setPasswordSaving(false); return; } try { const supabase = createClient(); const { error } = await supabase.auth.updateUser({ password: newPassword, }); if (error) throw error; setMessage({ type: "success", text: "Mot de passe mis a jour !" }); setNewPassword(""); setConfirmNewPassword(""); } catch { setMessage({ type: "error", text: "Erreur lors du changement de mot de passe.", }); } finally { setPasswordSaving(false); } }; if (loading) { return (
); } return (

Mon profil

Gere tes informations personnelles et ton abonnement.

{/* Message */} {message && (

{message.text}

)} {/* Informations profil */}

Informations personnelles

setFullName(e.target.value)} />
{/* Changement de mot de passe */}

Changer le mot de passe

setNewPassword(e.target.value)} /> setConfirmNewPassword(e.target.value)} />
{/* Abonnement */}

Abonnement

Statut {profile?.subscription_status === "active" ? "Actif" : "Inactif"}
{profile?.subscription_end_date && (
Valide jusqu'au {new Date(profile.subscription_end_date).toLocaleDateString( "fr-FR", { day: "numeric", month: "long", year: "numeric" } )}
)}
Plan HookLab - Programme 8 semaines
); }