|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Borrow } from '@/lib/types/borrow' |
| 4 | +import { zodResolver } from '@hookform/resolvers/zod' |
| 5 | +import { useForm } from 'react-hook-form' |
| 6 | +import { z } from 'zod' |
| 7 | +import { |
| 8 | + Form, |
| 9 | + FormControl, |
| 10 | + FormDescription, |
| 11 | + FormField, |
| 12 | + FormItem, |
| 13 | + FormLabel, |
| 14 | + FormMessage, |
| 15 | +} from '../ui/form' |
| 16 | +import { useToast } from '../hooks/use-toast' |
| 17 | +import { Popover, PopoverContent, PopoverTrigger } from '../ui/popover' |
| 18 | +import { Button } from '../ui/button' |
| 19 | +import { cn, formatDate } from '@/lib/utils' |
| 20 | +import { CalendarIcon } from 'lucide-react' |
| 21 | +import { Calendar } from '../ui/calendar' |
| 22 | + |
| 23 | +const FormSchema = z.object({ |
| 24 | + id: z.string({ |
| 25 | + required_error: 'Please select a borrow.', |
| 26 | + }), |
| 27 | + borrowed_at: z.string(), |
| 28 | + // due_at: z.coerce.string().datetime(), |
| 29 | + // staff_id: z.string({ |
| 30 | + // required_error: 'Please select a staff.', |
| 31 | + // }), |
| 32 | +}) |
| 33 | + |
| 34 | +export const FormEditBorrow: React.FC<{ borrow: Borrow }> = ({ borrow }) => { |
| 35 | + const form = useForm<z.infer<typeof FormSchema>>({ |
| 36 | + resolver: zodResolver(FormSchema), |
| 37 | + defaultValues: borrow, |
| 38 | + }) |
| 39 | + const { toast } = useToast() |
| 40 | + |
| 41 | + function onSubmit(data: z.infer<typeof FormSchema>) { |
| 42 | + toast({ |
| 43 | + title: 'You submitted the following values:', |
| 44 | + description: ( |
| 45 | + <pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4"> |
| 46 | + <code className="text-white">{JSON.stringify(data, null, 2)}</code> |
| 47 | + </pre> |
| 48 | + ), |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <Form {...form}> |
| 54 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> |
| 55 | + <FormField |
| 56 | + control={form.control} |
| 57 | + name="borrowed_at" |
| 58 | + render={({ field }) => ( |
| 59 | + <FormItem className="flex flex-col"> |
| 60 | + <FormLabel>Borrowed At</FormLabel> |
| 61 | + <Popover> |
| 62 | + <PopoverTrigger asChild> |
| 63 | + <FormControl> |
| 64 | + <Button |
| 65 | + variant={'outline'} |
| 66 | + className={cn( |
| 67 | + 'w-[240px] pl-3 text-left font-normal', |
| 68 | + !field.value && 'text-muted-foreground' |
| 69 | + )} |
| 70 | + > |
| 71 | + {field.value ? ( |
| 72 | + formatDate(field.value) |
| 73 | + ) : ( |
| 74 | + <span>Pick a date</span> |
| 75 | + )} |
| 76 | + <CalendarIcon className="ml-auto h-4 w-4 opacity-50" /> |
| 77 | + </Button> |
| 78 | + </FormControl> |
| 79 | + </PopoverTrigger> |
| 80 | + <PopoverContent className="w-auto p-0" align="start"> |
| 81 | + <Calendar |
| 82 | + mode="single" |
| 83 | + selected={new Date(field.value)} |
| 84 | + onSelect={(v) => field.onChange(v?.toISOString())} |
| 85 | + disabled={(date) => |
| 86 | + date > new Date() || date < new Date('1900-01-01') |
| 87 | + } |
| 88 | + initialFocus |
| 89 | + /> |
| 90 | + </PopoverContent> |
| 91 | + </Popover> |
| 92 | + <FormDescription> |
| 93 | + Change the date when the book was borrowed. |
| 94 | + </FormDescription> |
| 95 | + <FormMessage /> |
| 96 | + </FormItem> |
| 97 | + )} |
| 98 | + /> |
| 99 | + <Button type="submit">Submit</Button> |
| 100 | + </form> |
| 101 | + </Form> |
| 102 | + ) |
| 103 | +} |
0 commit comments