|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useActionState, useOptimistic, useState, type FC } from 'react' |
| 4 | + |
| 5 | +import { formSchema } from '@/constants/validation-schema' |
| 6 | +import { formFields } from '@/constants/form-fields' |
| 7 | +import { FormSchema } from '@/types/form-types' |
| 8 | + |
| 9 | +import { FieldWrapper } from '../FieldWrapper/FieldWrapper' |
| 10 | +import { Form } from '../Form/Form' |
| 11 | +import { Input } from '../Input/Input' |
| 12 | +import { Label } from '../Label/Label' |
| 13 | +import { Button } from '../Button/Button' |
| 14 | +import { slowFormRequest } from '@/utils/slow-form-request' |
| 15 | + |
| 16 | +type ErrorsState = Partial<{ |
| 17 | + [key in keyof FormSchema]: string[] |
| 18 | +}> |
| 19 | + |
| 20 | +export const ReactForm: FC = () => { |
| 21 | + const [errors, setErrors] = useState<ErrorsState>() |
| 22 | + |
| 23 | + const [optimisticState, addOptimisticState] = useOptimistic<string[]>([]) |
| 24 | + |
| 25 | + const handleFormSubmit = async (previousState: FormSchema, formData: FormData) => { |
| 26 | + try { |
| 27 | + addOptimisticState(['Submitting form...']) |
| 28 | + |
| 29 | + const formDataEntries = Object.fromEntries(formData.entries()) |
| 30 | + const result = formSchema.safeParse(formDataEntries) |
| 31 | + |
| 32 | + if (result.error) { |
| 33 | + addOptimisticState(['Form submission failed!']) |
| 34 | + |
| 35 | + setErrors(result.error.flatten().fieldErrors) |
| 36 | + |
| 37 | + // Keep the optimistic state for 1 second |
| 38 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 39 | + |
| 40 | + return { |
| 41 | + firstName: '', |
| 42 | + lastName: '', |
| 43 | + note: '', |
| 44 | + inviteCode: '', |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const submitResult = await slowFormRequest(result.data) |
| 49 | + |
| 50 | + addOptimisticState(['Form submitted successfully!']) |
| 51 | + // Keep the optimistic state for 1 second |
| 52 | + await new Promise((resolve) => setTimeout(resolve, 1000)) |
| 53 | + |
| 54 | + return { |
| 55 | + firstName: submitResult.firstName, |
| 56 | + lastName: submitResult.lastName, |
| 57 | + note: submitResult.note, |
| 58 | + inviteCode: submitResult.inviteCode, |
| 59 | + } |
| 60 | + } catch (error) { |
| 61 | + addOptimisticState([`Form submission failed! ${JSON.stringify(error)}`]) |
| 62 | + |
| 63 | + return { |
| 64 | + firstName: '', |
| 65 | + lastName: '', |
| 66 | + note: '', |
| 67 | + inviteCode: '', |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + const [state, formAction, isPending] = useActionState(handleFormSubmit, { |
| 73 | + firstName: '', |
| 74 | + lastName: '', |
| 75 | + note: '', |
| 76 | + inviteCode: '', |
| 77 | + }) |
| 78 | + |
| 79 | + return ( |
| 80 | + <div className="min-w-96"> |
| 81 | + <h1 className="text-2xl font-bold text-center mb-4">React Form</h1> |
| 82 | + |
| 83 | + <Form action={formAction}> |
| 84 | + {formFields.map((field) => ( |
| 85 | + <FieldWrapper key={field.name} errorMessage={errors?.[field.name]?.[0]}> |
| 86 | + <Label htmlFor={field.name}>{field.label}</Label> |
| 87 | + <Input name={field.name} /> |
| 88 | + </FieldWrapper> |
| 89 | + ))} |
| 90 | + |
| 91 | + {optimisticState.map((message) => ( |
| 92 | + <div key={message}>{message}</div> |
| 93 | + ))} |
| 94 | + |
| 95 | + <Button disabled={isPending}>Submit</Button> |
| 96 | + </Form> |
| 97 | + </div> |
| 98 | + ) |
| 99 | +} |
0 commit comments