Files
Fastchat/app/register/register-form.tsx
T

112 lines
4.0 KiB
TypeScript

"use client";
import Link from "next/link";
import { useActionState } from "react";
import { registerAction, type RegisterState } from "./actions";
const initialState: RegisterState = null;
export default function RegisterForm() {
const [state, action, pending] = useActionState(registerAction, initialState);
// Show success state if registration succeeded and email confirmation is needed
if (state && "success" in state && state.emailConfirmationNeeded) {
return (
<div className="w-full max-w-md space-y-6">
<div className="space-y-3 rounded-xl border border-emerald-200 bg-emerald-50 p-6">
<div className="flex items-center gap-3">
<div className="flex size-10 shrink-0 items-center justify-center rounded-full bg-emerald-100">
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
className="size-5 text-emerald-600"
>
<path
fillRule="evenodd"
d="M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm3.857-9.809a.75.75 0 0 0-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 1 0-1.06 1.061l2.5 2.5a.75.75 0 0 0 1.137-.089l4-5.5Z"
clipRule="evenodd"
/>
</svg>
</div>
<h2 className="text-lg font-semibold text-emerald-900">
Check your email
</h2>
</div>
<p className="text-sm text-emerald-700">
We&apos;ve sent a confirmation link to your email address. Please
click the link to verify your account and start chatting.
</p>
</div>
<p className="text-sm text-neutral-600">
Already confirmed?{" "}
<Link
className="font-medium text-neutral-900 hover:underline"
href="/login"
>
Log in
</Link>
</p>
</div>
);
}
return (
<div className="w-full max-w-md space-y-6">
<div className="space-y-1">
<h1 className="text-2xl font-semibold text-black">Create an account</h1>
<p className="text-sm text-muted-foreground text-black">
Sign up to start chatting with your friends.
</p>
</div>
<form action={action} className="space-y-4">
<div className="space-y-2">
<label htmlFor="email" className="text-sm font-medium text-black">
Email
</label>
<input
id="email"
name="email"
type="email"
autoComplete="email"
required
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm text-neutral-900 shadow-sm focus:border-neutral-900 focus:outline-none"
/>
</div>
<div className="space-y-2">
<label htmlFor="password" className="text-sm font-medium text-black">
Password
</label>
<input
id="password"
name="password"
type="password"
autoComplete="new-password"
required
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm text-neutral-900 shadow-sm focus:border-neutral-900 focus:outline-none"
/>
</div>
{state && "error" in state && state.error ? (
<p className="text-sm text-red-600">{state.error}</p>
) : null}
<button
type="submit"
disabled={pending}
className="w-full rounded-md bg-neutral-900 px-4 py-2 text-sm font-medium text-white shadow-sm transition disabled:cursor-not-allowed disabled:bg-neutral-400"
>
{pending ? "Creating account..." : "Register"}
</button>
</form>
<p className="text-sm text-neutral-600">
Already have an account?{" "}
<Link
className="font-medium text-neutral-900 hover:underline"
href="/login"
>
Log in
</Link>
</p>
</div>
);
}