From 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 Mon Sep 17 00:00:00 2001 From: joonhoekim <26rote@gmail.com> Date: Tue, 25 Mar 2025 15:55:45 +0900 Subject: initial commit --- lib/items/table/update-item-sheet.tsx | 178 ++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 lib/items/table/update-item-sheet.tsx (limited to 'lib/items/table/update-item-sheet.tsx') diff --git a/lib/items/table/update-item-sheet.tsx b/lib/items/table/update-item-sheet.tsx new file mode 100644 index 00000000..4bcdbfcb --- /dev/null +++ b/lib/items/table/update-item-sheet.tsx @@ -0,0 +1,178 @@ +"use client" + +import * as React from "react" +import { tasks, type Task } from "@/db/schema/tasks" +import { zodResolver } from "@hookform/resolvers/zod" +import { Loader } from "lucide-react" +import { useForm } from "react-hook-form" +import { toast } from "sonner" + +import { Button } from "@/components/ui/button" +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form" +import { + Select, + SelectContent, + SelectGroup, + SelectItem, + SelectTrigger, + SelectValue, +} from "@/components/ui/select" +import { + Sheet, + SheetClose, + SheetContent, + SheetDescription, + SheetFooter, + SheetHeader, + SheetTitle, +} from "@/components/ui/sheet" +import { Textarea } from "@/components/ui/textarea" + +import { modifiTask } from "@/lib//tasks/service" +import { updateTaskSchema, type UpdateTaskSchema } from "@/lib/tasks/validations" +import { Item } from "@/db/schema/items" +import { updateItemSchema, UpdateItemSchema } from "../validations" +import { modifyItem } from "../service" +import { Input } from "@/components/ui/input" + +interface UpdateItemSheetProps + extends React.ComponentPropsWithRef { + item: Item | null +} + +export function UpdateItemSheet({ item, ...props }: UpdateItemSheetProps) { + const [isUpdatePending, startUpdateTransition] = React.useTransition() + + console.log(item) + const form = useForm({ + resolver: zodResolver(updateItemSchema), + defaultValues: { + itemCode: item?.itemCode ?? "", + itemName: item?.itemName ?? "", + description: item?.description ?? "", + + }, + }) + + + React.useEffect(() => { + if (item) { + form.reset({ + itemCode: item.itemCode ?? "", + itemName: item.itemName ?? "", + description: item.description ?? "", + }); + } + }, [item, form]); + + function onSubmit(input: UpdateItemSchema) { + startUpdateTransition(async () => { + if (!item) return + + const { error } = await modifyItem({ + id: item.id, + ...input, + }) + + if (error) { + toast.error(error) + return + } + + form.reset() + props.onOpenChange?.(false) + toast.success("Item updated") + }) + } + + return ( + + + + Update item + + Update the item details and save the changes + + +
+ + ( + + Item Code + + + + + + + )} + /> + ( + + Item Name + + + + + + )} + /> + + ( + + Description + +