import { Loader2 } from "lucide-react"
import { useState } from "react"
import { Navigate, useNavigate } from "react-router-dom"
import { toast } from "sonner"
import { resetPasswordWithOtp } 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 { getApiErrorMessage } from "./utils"
export function ForgotPasswordPasswordPage() {
const navigate = useNavigate()
const { t } = useTranslation()
const { state, resetFlow, setMobile } = useAuthFlow()
const [password, setPassword] = useState("")
const [confirmation, setConfirmation] = useState("")
const [loading, setLoading] = useState(false)
if (!state.forgotPassword.mobile) {
return
}
if (!state.forgotPassword.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
}
setLoading(true)
try {
await resetPasswordWithOtp(state.forgotPassword.mobile, state.forgotPassword.code, password, confirmation)
setMobile("login", state.forgotPassword.mobile)
resetFlow("forgotPassword")
toast.success(t.login.toasts.passwordResetSuccess)
navigate("/auth/login/password", { replace: true })
} catch (error) {
toast.error(getApiErrorMessage(error, t.login.toasts.passwordResetFailed))
} finally {
setLoading(false)
}
}
return (
)
}