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<typeof Sheet> {
+  item: Item | null
+}
+
+export function UpdateItemSheet({ item, ...props }: UpdateItemSheetProps) {
+  const [isUpdatePending, startUpdateTransition] = React.useTransition()
+
+  console.log(item)
+  const form = useForm<UpdateItemSchema>({
+    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 (
+    <Sheet {...props}>
+      <SheetContent className="flex flex-col gap-6 sm:max-w-md">
+        <SheetHeader className="text-left">
+          <SheetTitle>Update item</SheetTitle>
+          <SheetDescription>
+            Update the item details and save the changes
+          </SheetDescription>
+        </SheetHeader>
+        <Form {...form}>
+          <form
+            onSubmit={form.handleSubmit(onSubmit)}
+            className="flex flex-col gap-4"
+          >
+            <FormField
+              control={form.control}
+              name="itemCode"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>Item Code</FormLabel>
+                  <FormControl>
+                    <Input   placeholder="e.g." {...field} />
+
+                  </FormControl>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+            <FormField
+              control={form.control}
+              name="itemName"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>Item Name</FormLabel>
+                  <FormControl>
+                    <Input
+                      placeholder="e.g."
+                      {...field}
+                    />
+                  </FormControl>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+
+            <FormField
+              control={form.control}
+              name="description"
+              render={({ field }) => (
+                <FormItem>
+                  <FormLabel>Description</FormLabel>
+                  <FormControl>
+                    <Textarea
+                      placeholder="e.g."
+                      {...field}
+                    />
+                  </FormControl>
+                  <FormMessage />
+                </FormItem>
+              )}
+            />
+
+            <SheetFooter className="gap-2 pt-2 sm:space-x-0">
+              <SheetClose asChild>
+                <Button type="button" variant="outline">
+                  Cancel
+                </Button>
+              </SheetClose>
+              <Button disabled={isUpdatePending}>
+                {isUpdatePending && (
+                  <Loader
+                    className="mr-2 size-4 animate-spin"
+                    aria-hidden="true"
+                  />
+                )}
+                Save
+              </Button>
+            </SheetFooter>
+          </form>
+        </Form>
+      </SheetContent>
+    </Sheet>
+  )
+}
-- 
cgit v1.2.3

