"use client"; import { updateEmailAction } from "@/app/(dashboard)/ajustes/actions"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { RiCheckLine, RiCloseLine, RiEyeLine, RiEyeOffLine } from "@remixicon/react"; import { useState, useTransition, useMemo } from "react"; import { toast } from "sonner"; type UpdateEmailFormProps = { currentEmail: string; authProvider?: string; // 'google' | 'credential' | undefined }; export function UpdateEmailForm({ currentEmail, authProvider }: UpdateEmailFormProps) { const [isPending, startTransition] = useTransition(); const [password, setPassword] = useState(""); const [newEmail, setNewEmail] = useState(""); const [confirmEmail, setConfirmEmail] = useState(""); const [showPassword, setShowPassword] = useState(false); // Verificar se o usuário usa login via Google (não precisa de senha) const isGoogleAuth = authProvider === "google"; // Validação em tempo real: e-mails coincidem const emailsMatch = useMemo(() => { if (!confirmEmail) return null; // Não mostrar erro se campo vazio return newEmail.toLowerCase() === confirmEmail.toLowerCase(); }, [newEmail, confirmEmail]); // Validação: novo e-mail é diferente do atual const isEmailDifferent = useMemo(() => { if (!newEmail) return true; return newEmail.toLowerCase() !== currentEmail.toLowerCase(); }, [newEmail, currentEmail]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // Validação frontend antes de enviar if (newEmail.toLowerCase() !== confirmEmail.toLowerCase()) { toast.error("Os e-mails não coincidem"); return; } if (newEmail.toLowerCase() === currentEmail.toLowerCase()) { toast.error("O novo e-mail deve ser diferente do atual"); return; } startTransition(async () => { const result = await updateEmailAction({ password: isGoogleAuth ? undefined : password, newEmail, confirmEmail, }); if (result.success) { toast.success(result.message); setPassword(""); setNewEmail(""); setConfirmEmail(""); } else { toast.error(result.error); } }); }; return (
{/* E-mail atual (apenas informativo) */}

Este é seu e-mail atual cadastrado

{/* Senha de confirmação (apenas para usuários com login por e-mail/senha) */} {!isGoogleAuth && (
setPassword(e.target.value)} disabled={isPending} placeholder="Digite sua senha para confirmar" required aria-required="true" aria-describedby="password-help" />

Por segurança, confirme sua senha antes de alterar seu e-mail

)} {/* Novo e-mail */}
setNewEmail(e.target.value)} disabled={isPending} placeholder="Digite o novo e-mail" required aria-required="true" aria-describedby="new-email-help" aria-invalid={!isEmailDifferent} className={!isEmailDifferent ? "border-red-500 focus-visible:ring-red-500" : ""} /> {!isEmailDifferent && newEmail && (

O novo e-mail deve ser diferente do atual

)} {!newEmail && (

Digite o novo endereço de e-mail para sua conta

)}
{/* Confirmar novo e-mail */}
setConfirmEmail(e.target.value)} disabled={isPending} placeholder="Repita o novo e-mail" required aria-required="true" aria-describedby="confirm-email-help" aria-invalid={emailsMatch === false} className={ emailsMatch === false ? "border-red-500 focus-visible:ring-red-500 pr-10" : emailsMatch === true ? "border-green-500 focus-visible:ring-green-500 pr-10" : "" } /> {/* Indicador visual de match */} {emailsMatch !== null && (
{emailsMatch ? ( ) : ( )}
)}
{/* Mensagem de erro em tempo real */} {emailsMatch === false && ( )} {emailsMatch === true && (

Os e-mails coincidem

)}
); }