"use client"; import { useState } from "react"; import { Button, ButtonVariants } from "./Button"; import { TextInput } from "./Input"; import { useForm } from "react-hook-form"; import { useRouter } from "next/navigation"; import { Spinner } from "./Spinner"; import Alert from "./Alert"; type Inputs = { password: string; }; type Props = { loginName: string; }; export default function PasswordForm({ loginName }: Props) { const { register, handleSubmit, formState } = useForm({ mode: "onBlur", }); const [error, setError] = useState(""); const [loading, setLoading] = useState(false); const router = useRouter(); async function submitPassword(values: Inputs) { setError(""); setLoading(true); const res = await fetch("/session", { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ password: values.password, }), }); const response = await res.json(); if (!res.ok) { setLoading(false); console.log(response); setError(response.details); return Promise.reject(response.details); } else { setLoading(false); return response; } } function submitPasswordAndContinue(value: Inputs): Promise { return submitPassword(value).then((resp: any) => { return router.push(`/accounts`); }); } const { errors } = formState; return (
{loginName && ( )}
{error && (
{error}
)}
{/* */}
); }