|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 4 | +import { useForm } from 'react-hook-form' |
| 5 | +import { z } from 'zod' |
| 6 | +import { |
| 7 | + Form, |
| 8 | + FormControl, |
| 9 | + FormField, |
| 10 | + FormItem, |
| 11 | + FormLabel, |
| 12 | + FormMessage, |
| 13 | +} from '../ui/form' |
| 14 | +import { toast } from 'sonner' |
| 15 | +import { Button } from '../ui/button' |
| 16 | +import { Input } from '../ui/input' |
| 17 | +import { useCallback, useTransition } from 'react' |
| 18 | +import { lostBorrowAction } from '@/lib/actions/lost-borrow' |
| 19 | +import { Textarea } from '../ui/textarea' |
| 20 | +import { Spinner } from '../ui/spinner' |
| 21 | + |
| 22 | +const FormSchema = z.object({ |
| 23 | + id: z.uuid(), |
| 24 | + reported_at: z.date(), |
| 25 | + note: z.string().nonempty(), |
| 26 | + fine: z.number(), |
| 27 | +}) |
| 28 | + |
| 29 | +export const FormLostBorrow: React.FC<{ |
| 30 | + id: string |
| 31 | +}> = ({ id }) => { |
| 32 | + const form = useForm<z.infer<typeof FormSchema>>({ |
| 33 | + resolver: zodResolver(FormSchema), |
| 34 | + defaultValues: { |
| 35 | + id, |
| 36 | + reported_at: new Date(), |
| 37 | + note: '', |
| 38 | + fine: 0, |
| 39 | + }, |
| 40 | + }) |
| 41 | + |
| 42 | + const [isPending, startTransition] = useTransition() |
| 43 | + |
| 44 | + function onSubmit(data: z.infer<typeof FormSchema>) { |
| 45 | + startTransition(async () => { |
| 46 | + const msg = await lostBorrowAction({ |
| 47 | + id: data.id, |
| 48 | + reported_at: new Date().toJSON(), |
| 49 | + note: data.note, |
| 50 | + fine: data.fine, |
| 51 | + }) |
| 52 | + if ('error' in msg) { |
| 53 | + toast.error(msg.error, { richColors: true }) |
| 54 | + } else { |
| 55 | + toast.success(msg.message) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + const onReset = useCallback(() => { |
| 61 | + form.reset() |
| 62 | + }, [form]) |
| 63 | + |
| 64 | + return ( |
| 65 | + <Form {...form}> |
| 66 | + <form onSubmit={form.handleSubmit(onSubmit)} className="grid gap-2"> |
| 67 | + <FormField |
| 68 | + control={form.control} |
| 69 | + name="note" |
| 70 | + render={({ field }) => ( |
| 71 | + <FormItem className="flex flex-col"> |
| 72 | + <FormLabel>Note</FormLabel> |
| 73 | + <FormControl> |
| 74 | + <Textarea |
| 75 | + placeholder="Remarks" |
| 76 | + {...field} |
| 77 | + onChange={field.onChange} |
| 78 | + rows={3} |
| 79 | + /> |
| 80 | + </FormControl> |
| 81 | + <FormMessage /> |
| 82 | + </FormItem> |
| 83 | + )} |
| 84 | + /> |
| 85 | + <FormField |
| 86 | + control={form.control} |
| 87 | + name="fine" |
| 88 | + render={({ field }) => ( |
| 89 | + <FormItem className="flex flex-col"> |
| 90 | + <FormLabel>Fine</FormLabel> |
| 91 | + <FormControl> |
| 92 | + <Input |
| 93 | + placeholder="Pts" |
| 94 | + type="number" |
| 95 | + {...field} |
| 96 | + onChange={(e) => field.onChange(e.target.valueAsNumber)} |
| 97 | + /> |
| 98 | + </FormControl> |
| 99 | + <FormMessage /> |
| 100 | + </FormItem> |
| 101 | + )} |
| 102 | + /> |
| 103 | + <Button type="reset" variant="ghost" onClick={onReset}> |
| 104 | + Reset |
| 105 | + </Button> |
| 106 | + <Button type="submit" disabled={!form.formState.isDirty || isPending}> |
| 107 | + {isPending && <Spinner />} |
| 108 | + Submit |
| 109 | + </Button> |
| 110 | + </form> |
| 111 | + </Form> |
| 112 | + ) |
| 113 | +} |
0 commit comments