2023-05-16 17:34:52 +02:00
|
|
|
"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";
|
|
|
|
|
|
|
|
|
|
type Inputs = {
|
|
|
|
|
loginName: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function UsernameForm() {
|
|
|
|
|
const { register, handleSubmit, formState } = useForm<Inputs>({
|
|
|
|
|
mode: "onBlur",
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-29 14:54:07 +02:00
|
|
|
const router = useRouter();
|
2023-05-16 17:34:52 +02:00
|
|
|
|
2023-06-29 14:54:07 +02:00
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
2023-05-16 17:34:52 +02:00
|
|
|
|
2023-06-29 14:54:07 +02:00
|
|
|
function resubmitWithUsername(values: Inputs) {
|
|
|
|
|
return router.push(
|
|
|
|
|
`/loginname?` + new URLSearchParams({ loginName: values.loginName })
|
2023-06-28 10:47:22 +02:00
|
|
|
);
|
2023-06-29 14:54:07 +02:00
|
|
|
// setLoading(true);
|
|
|
|
|
// const res = await fetch("/methods", {
|
|
|
|
|
// method: "POST",
|
|
|
|
|
// headers: {
|
|
|
|
|
// "Content-Type": "application/json",
|
|
|
|
|
// },
|
|
|
|
|
// body: JSON.stringify({
|
|
|
|
|
// loginName: values.loginName,
|
|
|
|
|
// }),
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// setLoading(false);
|
|
|
|
|
// if (!res.ok) {
|
|
|
|
|
// throw new Error("Failed to load authentication methods");
|
|
|
|
|
// }
|
|
|
|
|
// return res.json();
|
2023-05-16 17:34:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { errors } = formState;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className="w-full">
|
|
|
|
|
<div className="">
|
|
|
|
|
<TextInput
|
|
|
|
|
type="text"
|
|
|
|
|
autoComplete="username"
|
|
|
|
|
{...register("loginName", { required: "This field is required" })}
|
|
|
|
|
label="Loginname"
|
|
|
|
|
// error={errors.username?.message as string}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="mt-8 flex w-full flex-row items-center">
|
|
|
|
|
{/* <Button type="button" variant={ButtonVariants.Secondary}>
|
|
|
|
|
back
|
|
|
|
|
</Button> */}
|
|
|
|
|
<span className="flex-grow"></span>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="self-end"
|
|
|
|
|
variant={ButtonVariants.Primary}
|
|
|
|
|
disabled={loading || !formState.isValid}
|
2023-06-29 14:54:07 +02:00
|
|
|
onClick={handleSubmit(resubmitWithUsername)}
|
2023-05-16 17:34:52 +02:00
|
|
|
>
|
|
|
|
|
{loading && <Spinner className="h-5 w-5 mr-2" />}
|
|
|
|
|
continue
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|