|
1 |
| -import { lusitana } from '@/app/ui/fonts'; |
| 1 | +'use client'; |
| 2 | + |
| 3 | +import {lusitana} from '@/app/ui/fonts'; |
2 | 4 | import {
|
3 |
| - AtSymbolIcon, |
4 |
| - KeyIcon, |
5 |
| - ExclamationCircleIcon, |
| 5 | + AtSymbolIcon, |
| 6 | + KeyIcon, |
| 7 | + ExclamationCircleIcon, |
6 | 8 | } from '@heroicons/react/24/outline';
|
7 |
| -import { ArrowRightIcon } from '@heroicons/react/20/solid'; |
8 |
| -import { Button } from './button'; |
| 9 | +import {ArrowRightIcon} from '@heroicons/react/20/solid'; |
| 10 | +import {Button} from './button'; |
| 11 | +import {useFormState, useFormStatus} from "react-dom"; |
| 12 | +import { authenticate } from '@/app/lib/actions' |
9 | 13 |
|
10 | 14 | export default function LoginForm() {
|
11 |
| - return ( |
12 |
| - <form className="space-y-3"> |
13 |
| - <div className="flex-1 rounded-lg bg-gray-50 px-6 pb-4 pt-8"> |
14 |
| - <h1 className={`${lusitana.className} mb-3 text-2xl`}> |
15 |
| - Please log in to continue. |
16 |
| - </h1> |
17 |
| - <div className="w-full"> |
18 |
| - <div> |
19 |
| - <label |
20 |
| - className="mb-3 mt-5 block text-xs font-medium text-gray-900" |
21 |
| - htmlFor="email" |
22 |
| - > |
23 |
| - Email |
24 |
| - </label> |
25 |
| - <div className="relative"> |
26 |
| - <input |
27 |
| - className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500" |
28 |
| - id="email" |
29 |
| - type="email" |
30 |
| - name="email" |
31 |
| - placeholder="Enter your email address" |
32 |
| - required |
33 |
| - /> |
34 |
| - <AtSymbolIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" /> |
35 |
| - </div> |
36 |
| - </div> |
37 |
| - <div className="mt-4"> |
38 |
| - <label |
39 |
| - className="mb-3 mt-5 block text-xs font-medium text-gray-900" |
40 |
| - htmlFor="password" |
41 |
| - > |
42 |
| - Password |
43 |
| - </label> |
44 |
| - <div className="relative"> |
45 |
| - <input |
46 |
| - className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500" |
47 |
| - id="password" |
48 |
| - type="password" |
49 |
| - name="password" |
50 |
| - placeholder="Enter password" |
51 |
| - required |
52 |
| - minLength={6} |
53 |
| - /> |
54 |
| - <KeyIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" /> |
| 15 | + const [errorMessage, dispatch] = useFormState(authenticate, undefined) |
| 16 | + |
| 17 | + return ( |
| 18 | + <form action={dispatch} className="space-y-3"> |
| 19 | + <div className="flex-1 rounded-lg bg-gray-50 px-6 pb-4 pt-8"> |
| 20 | + <h1 className={`${lusitana.className} mb-3 text-2xl`}> |
| 21 | + Please log in to continue. |
| 22 | + </h1> |
| 23 | + <div className="w-full"> |
| 24 | + <div> |
| 25 | + <label |
| 26 | + className="mb-3 mt-5 block text-xs font-medium text-gray-900" |
| 27 | + htmlFor="email" |
| 28 | + > |
| 29 | + Email |
| 30 | + </label> |
| 31 | + <div className="relative"> |
| 32 | + <input |
| 33 | + className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500" |
| 34 | + id="email" |
| 35 | + type="email" |
| 36 | + name="email" |
| 37 | + placeholder="Enter your email address" |
| 38 | + required |
| 39 | + /> |
| 40 | + <AtSymbolIcon |
| 41 | + className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900"/> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + <div className="mt-4"> |
| 45 | + <label |
| 46 | + className="mb-3 mt-5 block text-xs font-medium text-gray-900" |
| 47 | + htmlFor="password" |
| 48 | + > |
| 49 | + Password |
| 50 | + </label> |
| 51 | + <div className="relative"> |
| 52 | + <input |
| 53 | + className="peer block w-full rounded-md border border-gray-200 py-[9px] pl-10 text-sm outline-2 placeholder:text-gray-500" |
| 54 | + id="password" |
| 55 | + type="password" |
| 56 | + name="password" |
| 57 | + placeholder="Enter password" |
| 58 | + required |
| 59 | + minLength={6} |
| 60 | + /> |
| 61 | + <KeyIcon |
| 62 | + className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900"/> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + </div> |
| 66 | + <LoginButton/> |
| 67 | + <div className="flex h-8 items-end space-x-1" |
| 68 | + aria-live="polite" |
| 69 | + aria-atomic="true" |
| 70 | + > |
| 71 | + {errorMessage && ( |
| 72 | + <> |
| 73 | + <ExclamationCircleIcon className="h-5 w-5 text-red-500"/> |
| 74 | + <span className="text-sm text-red-500">{errorMessage}</span> |
| 75 | + </> |
| 76 | + |
| 77 | + )} |
| 78 | + </div> |
55 | 79 | </div>
|
56 |
| - </div> |
57 |
| - </div> |
58 |
| - <LoginButton /> |
59 |
| - <div className="flex h-8 items-end space-x-1"> |
60 |
| - {/* Add form errors here */} |
61 |
| - </div> |
62 |
| - </div> |
63 |
| - </form> |
64 |
| - ); |
| 80 | + </form> |
| 81 | + ) |
| 82 | + ; |
65 | 83 | }
|
66 | 84 |
|
67 | 85 | function LoginButton() {
|
68 |
| - return ( |
69 |
| - <Button className="mt-4 w-full"> |
70 |
| - Log in <ArrowRightIcon className="ml-auto h-5 w-5 text-gray-50" /> |
71 |
| - </Button> |
72 |
| - ); |
| 86 | + const { pending } = useFormStatus(); |
| 87 | + |
| 88 | + return ( |
| 89 | + <Button className="mt-4 w-full" aria-disabled={pending}> |
| 90 | + Log in <ArrowRightIcon className="ml-auto h-5 w-5 text-gray-50"/> |
| 91 | + </Button> |
| 92 | + ); |
73 | 93 | }
|
0 commit comments