From e0dfb55c5457aec489fc084c4567e791b4c65eb1 Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Wed, 26 Mar 2025 00:37:41 +0000
Subject: 3/25 까지의 대표님 작업사항
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/vendors/table/send-vendor-dialog.tsx | 150 +++++++++++++++++++++++++++++++
 1 file changed, 150 insertions(+)
 create mode 100644 lib/vendors/table/send-vendor-dialog.tsx

(limited to 'lib/vendors/table/send-vendor-dialog.tsx')

diff --git a/lib/vendors/table/send-vendor-dialog.tsx b/lib/vendors/table/send-vendor-dialog.tsx
new file mode 100644
index 00000000..a34abb77
--- /dev/null
+++ b/lib/vendors/table/send-vendor-dialog.tsx
@@ -0,0 +1,150 @@
+"use client"
+
+import * as React from "react"
+import { type Row } from "@tanstack/react-table"
+import { Loader, Check, Send } from "lucide-react"
+import { toast } from "sonner"
+
+import { useMediaQuery } from "@/hooks/use-media-query"
+import { Button } from "@/components/ui/button"
+import {
+  Dialog,
+  DialogClose,
+  DialogContent,
+  DialogDescription,
+  DialogFooter,
+  DialogHeader,
+  DialogTitle,
+  DialogTrigger,
+} from "@/components/ui/dialog"
+import {
+  Drawer,
+  DrawerClose,
+  DrawerContent,
+  DrawerDescription,
+  DrawerFooter,
+  DrawerHeader,
+  DrawerTitle,
+  DrawerTrigger,
+} from "@/components/ui/drawer"
+import { Vendor } from "@/db/schema/vendors"
+import { requestPQVendors, sendVendors } from "../service"
+
+interface ApprovalVendorDialogProps
+  extends React.ComponentPropsWithoutRef<typeof Dialog> {
+  vendors: Row<Vendor>["original"][]
+  showTrigger?: boolean
+  onSuccess?: () => void
+}
+
+export function SendVendorsDialog({
+  vendors,
+  showTrigger = true,
+  onSuccess,
+  ...props
+}: ApprovalVendorDialogProps) {
+  const [isApprovePending, startApproveTransition] = React.useTransition()
+  const isDesktop = useMediaQuery("(min-width: 640px)")
+
+  function onApprove() {
+    startApproveTransition(async () => {
+      const { error } = await sendVendors({
+        ids: vendors.map((vendor) => vendor.id),
+      })
+
+      if (error) {
+        toast.error(error)
+        return
+      }
+
+      props.onOpenChange?.(false)
+      toast.success("PQ successfully sent to vendors")
+      onSuccess?.()
+    })
+  }
+
+  if (isDesktop) {
+    return (
+      <Dialog {...props}>
+        {showTrigger ? (
+          <DialogTrigger asChild>
+            <Button variant="outline" size="sm" className="gap-2">
+              <Send className="size-4" aria-hidden="true" />
+              Send ({vendors.length})
+            </Button>
+          </DialogTrigger>
+        ) : null}
+        <DialogContent>
+          <DialogHeader>
+            <DialogTitle>Confirm to send Vendor Information</DialogTitle>
+            <DialogDescription>
+              Are you sure you want to send{" "}
+              <span className="font-medium">{vendors.length}</span>
+              {vendors.length === 1 ? " vendor" : " vendors"}?
+              After vendor information is sent, vendor code will be generated.
+            </DialogDescription>
+          </DialogHeader>
+          <DialogFooter className="gap-2 sm:space-x-0">
+            <DialogClose asChild>
+              <Button variant="outline">Cancel</Button>
+            </DialogClose>
+            <Button
+              aria-label="Send selected vendors"
+              variant="default"
+              onClick={onApprove}
+              disabled={isApprovePending}
+            >
+              {isApprovePending && (
+                <Loader
+                  className="mr-2 size-4 animate-spin"
+                  aria-hidden="true"
+                />
+              )}
+              Send
+            </Button>
+          </DialogFooter>
+        </DialogContent>
+      </Dialog>
+    )
+  }
+
+  return (
+    <Drawer {...props}>
+      {showTrigger ? (
+        <DrawerTrigger asChild>
+          <Button variant="outline" size="sm" className="gap-2">
+            <Send className="size-4" aria-hidden="true" />
+            Send ({vendors.length})
+          </Button>
+        </DrawerTrigger>
+      ) : null}
+      <DrawerContent>
+        <DrawerHeader>
+          <DrawerTitle>Confirm to send Vendor Information</DrawerTitle>
+          <DrawerDescription>
+            Are you sure you want to send{" "}
+            <span className="font-medium">{vendors.length}</span>
+            {vendors.length === 1 ? " vendor" : " vendors"}?
+            After vendor information is sent, vendor code will be generated.
+            </DrawerDescription>
+        </DrawerHeader>
+        <DrawerFooter className="gap-2 sm:space-x-0">
+          <DrawerClose asChild>
+            <Button variant="outline">Cancel</Button>
+          </DrawerClose>
+          <Button
+            aria-label="Send selected vendors"
+            variant="default"
+            onClick={onApprove}
+            disabled={isApprovePending}
+          >
+            {isApprovePending && (
+              <Loader className="mr-2 size-4 animate-spin" aria-hidden="true" />
+            )}
+            Send
+          </Button>
+        </DrawerFooter>
+      </DrawerContent>
+    </Drawer>
+  )
+}
\ No newline at end of file
-- 
cgit v1.2.3

