From e0dfb55c5457aec489fc084c4567e791b4c65eb1 Mon Sep 17 00:00:00 2001 From: dujinkim 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 --- .../table/delete-docs-dialog.tsx | 231 +++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 lib/vendor-document-list/table/delete-docs-dialog.tsx (limited to 'lib/vendor-document-list/table/delete-docs-dialog.tsx') diff --git a/lib/vendor-document-list/table/delete-docs-dialog.tsx b/lib/vendor-document-list/table/delete-docs-dialog.tsx new file mode 100644 index 00000000..8813c742 --- /dev/null +++ b/lib/vendor-document-list/table/delete-docs-dialog.tsx @@ -0,0 +1,231 @@ +"use client" + +import * as React from "react" +import { type Row } from "@tanstack/react-table" +import { Loader, Trash, AlertCircle } 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 { Alert, AlertDescription } from "@/components/ui/alert" +import { DocumentStagesView } from "@/db/schema/vendorDocu" +import { removeDocuments } from "../service" + +interface DeleteDocumentsDialogProps + extends React.ComponentPropsWithoutRef { + documents: Row["original"][] + showTrigger?: boolean + onSuccess?: () => void +} + +export function DeleteDocumentsDialog({ + documents, + showTrigger = true, + onSuccess, + ...props +}: DeleteDocumentsDialogProps) { + const [isDeletePending, startDeleteTransition] = React.useTransition() + const isDesktop = useMediaQuery("(min-width: 640px)") + + // "pending" 상태인 문서만 필터링 + const pendingDocuments = documents.filter(doc => doc.status === "pending") + const nonPendingDocuments = documents.filter(doc => doc.status !== "pending") + + const hasMixedStatus = pendingDocuments.length > 0 && nonPendingDocuments.length > 0 + const hasNoPendingDocuments = pendingDocuments.length === 0 + + function onDelete() { + // 삭제할 문서가 없으면 경고 + if (pendingDocuments.length === 0) { + toast.error("No pending documents to delete") + props.onOpenChange?.(false) + return + } + + startDeleteTransition(async () => { + // "pending" 상태인 문서 ID만 전달 + const { success, error } = await removeDocuments({ + ids: pendingDocuments.map((document) => document.documentId) + }) + + if (!success) { + toast.error(error || "Failed to delete documents") + return + } + + props.onOpenChange?.(false) + + // 적절한 성공 메시지 표시 + if (hasMixedStatus) { + toast.success(`${pendingDocuments.length} pending document(s) deleted successfully. ${nonPendingDocuments.length} non-pending document(s) were not affected.`) + } else { + toast.success(`${pendingDocuments.length} document(s) deleted successfully`) + } + + onSuccess?.() + }) + } + + // 선택된 문서 상태에 대한 알림 메시지 렌더링 + const renderStatusAlert = () => { + if (hasNoPendingDocuments) { + return ( + + + + None of the selected documents are in "pending" status. Only pending documents can be deleted. + + + ) + } + + if (hasMixedStatus) { + return ( + + + + Only the {pendingDocuments.length} document(s) with "pending" status will be deleted. + {nonPendingDocuments.length} document(s) cannot be deleted because they are not in pending status. + + + ) + } + + return null + } + + if (isDesktop) { + return ( + + {showTrigger ? ( + + + + ) : null} + + + Are you absolutely sure? + + This action cannot be undone. Only documents with "pending" status can be deleted. + + + + {renderStatusAlert()} + +
+ {pendingDocuments.length > 0 && ( +

+ {pendingDocuments.length} pending document(s) will be deleted: +

+ )} + {pendingDocuments.length > 0 && ( +
    + {pendingDocuments.map(doc => ( +
  • {doc.docNumber} - {doc.title}
  • + ))} +
+ )} +
+ + + + + + + +
+
+ ) + } + + return ( + + {showTrigger ? ( + + + + ) : null} + + + Are you absolutely sure? + + This action cannot be undone. Only documents with "pending" status can be deleted. + + + + {renderStatusAlert()} + +
+ {pendingDocuments.length > 0 && ( +

+ {pendingDocuments.length} pending document(s) will be deleted: +

+ )} + {pendingDocuments.length > 0 && ( +
    + {pendingDocuments.map(doc => ( +
  • {doc.docNumber} - {doc.title}
  • + ))} +
+ )} +
+ + + + + + + +
+
+ ) +} \ No newline at end of file -- cgit v1.2.3