import { Loader2 } from "lucide-react" import { useState } from "react" import { Navigate, useNavigate } from "react-router-dom" import { toast } from "sonner" import { registerWithOtp } from "../../api/users" import { Button } from "../../components/ui/button" import { useAuthFlow } from "../../context/AuthFlowContext" import { useTranslation } from "../../hooks/useTranslation" import { AuthPanel } from "./AuthPanel" import { AuthPasswordField } from "./AuthPasswordField" import { completeAuthentication, getApiErrorMessage, getPasswordValidationMessage } from "./utils" export function SignupPasswordPage() { const navigate = useNavigate() const { t } = useTranslation() const { state, resetFlow } = useAuthFlow() const [password, setPassword] = useState("") const [confirmation, setConfirmation] = useState("") const [loading, setLoading] = useState(false) if (!state.signup.mobile) { return } if (!state.signup.code) { return } const handleSubmit = async (event: React.FormEvent) => { event.preventDefault() if (!password || !confirmation) { toast.error(t.login.toasts.fillAll) return } if (password !== confirmation) { toast.error(t.login.passwordMismatch) return } const passwordValidationMessage = getPasswordValidationMessage(password, t.login) if (passwordValidationMessage) { toast.error(passwordValidationMessage) return } setLoading(true) try { const data = await registerWithOtp(state.signup.mobile, state.signup.code, password, confirmation) resetFlow("signup") completeAuthentication({ access: data.access, refresh: data.refresh, successMessage: t.login.toasts.accountCreated, redirectTo: "/timesheet", navigate, }) } catch (error) { toast.error(getApiErrorMessage(error, t.login.toasts.failedSignup)) } finally { setLoading(false) } } return (
) }