"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { authService } from "@/lib/services/auth.service"; import { AppErrorType } from "@/types/global"; import { ERROR_CODES } from "@/constants/errorCodes"; import { ErrorMessage } from "@/components/ui/ErrorMessage"; import { useTranslate } from "@/hooks/useTranslate"; import { getErrorMessage } from "@/utils/errors"; interface RegisterFormProps { from?: string; } export function RegisterForm({ from }: RegisterFormProps) { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const { t } = useTranslate(); const handleSubmit = async (event: React.FormEvent) => { event.preventDefault(); setIsLoading(true); setError(null); const formData = new FormData(event.currentTarget); const email = formData.get("email") as string; const password = formData.get("password") as string; const confirmPassword = formData.get("confirmPassword") as string; if (password !== confirmPassword) { setError({ code: ERROR_CODES.AUTH.PASSWORD_MISMATCH, name: "Password mismatch", message: getErrorMessage(ERROR_CODES.AUTH.PASSWORD_MISMATCH), }); setIsLoading(false); return; } try { await authService.register(email, password); router.push(from || "/"); router.refresh(); } catch (error) { setError(error as AppErrorType); } finally { setIsLoading(false); } }; return (
{error && } ); }