"use client"; import { useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useToast } from "@/components/ui/use-toast"; import { Lock } from "lucide-react"; import type { AdminUserData } from "@/lib/services/admin.service"; interface ResetPasswordDialogProps { user: AdminUserData; open: boolean; onOpenChange: (open: boolean) => void; onSuccess: () => void; } export function ResetPasswordDialog({ user, open, onOpenChange, onSuccess, }: ResetPasswordDialogProps) { const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const { toast } = useToast(); const handleSubmit = async () => { if (!newPassword || !confirmPassword) { toast({ variant: "destructive", title: "Erreur", description: "Veuillez remplir tous les champs", }); return; } if (newPassword !== confirmPassword) { toast({ variant: "destructive", title: "Erreur", description: "Les mots de passe ne correspondent pas", }); return; } if (newPassword.length < 8) { toast({ variant: "destructive", title: "Erreur", description: "Le mot de passe doit contenir au moins 8 caractères", }); return; } setIsLoading(true); try { const response = await fetch(`/api/admin/users/${user.id}/password`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ newPassword }), }); if (!response.ok) { const data = await response.json(); throw new Error(data.error || "Erreur lors de la réinitialisation"); } toast({ title: "Succès", description: "Le mot de passe a été réinitialisé", }); setNewPassword(""); setConfirmPassword(""); onSuccess(); } catch (error) { toast({ variant: "destructive", title: "Erreur", description: error instanceof Error ? error.message : "Une erreur est survenue", }); } finally { setIsLoading(false); } }; const handleOpenChange = (open: boolean) => { if (!open) { setNewPassword(""); setConfirmPassword(""); } onOpenChange(open); }; return ( Réinitialiser le mot de passe Définir un nouveau mot de passe pour {user.email}
setNewPassword(e.target.value)} className="pl-9" placeholder="Minimum 8 caractères" disabled={isLoading} />
setConfirmPassword(e.target.value)} className="pl-9" placeholder="Confirmer le nouveau mot de passe" disabled={isLoading} />

Le mot de passe doit contenir au moins 8 caractères, une majuscule et un chiffre.

); }