From ef4c533ebacc2cdc97e518f30e9a9350004fcdfb Mon Sep 17 00:00:00 2001
From: dujinkim <dujin.kim@dtsolution.co.kr>
Date: Mon, 28 Apr 2025 02:13:30 +0000
Subject: ~20250428 작업사항
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lib/tbe/table/tbe-table.tsx | 81 +++++++++++++++++++++++++++++++++------------
 1 file changed, 60 insertions(+), 21 deletions(-)

(limited to 'lib/tbe/table/tbe-table.tsx')

diff --git a/lib/tbe/table/tbe-table.tsx b/lib/tbe/table/tbe-table.tsx
index e67b1d3d..83d601b3 100644
--- a/lib/tbe/table/tbe-table.tsx
+++ b/lib/tbe/table/tbe-table.tsx
@@ -15,12 +15,14 @@ import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-adv
 import { useFeatureFlags } from "./feature-flags-provider"
 import { getColumns } from "./tbe-table-columns"
 import { Vendor, vendors } from "@/db/schema/vendors"
-import { InviteVendorsDialog } from "./invite-vendors-dialog"
 import { CommentSheet, TbeComment } from "./comments-sheet"
 import { VendorWithTbeFields } from "@/config/vendorTbeColumnsConfig"
 import { TBEFileDialog } from "./file-dialog"
 import { fetchRfqAttachmentsbyCommentId, getAllTBE } from "@/lib/rfqs/service"
 import { VendorsTableToolbarActions } from "./tbe-table-toolbar-actions"
+import { TbeResultDialog } from "./tbe-result-dialog"
+import { toast } from "sonner"
+import { VendorContactsDialog } from "./vendor-contact-dialog"
 
 interface VendorsTableProps {
   promises: Promise<[
@@ -39,6 +41,8 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
 
   // 댓글 시트 관련 state
   const [initialComments, setInitialComments] = React.useState<TbeComment[]>([])
+  const [isLoadingComments, setIsLoadingComments] = React.useState(false)
+
   const [commentSheetOpen, setCommentSheetOpen] = React.useState(false)
   const [selectedVendorIdForComments, setSelectedVendorIdForComments] = React.useState<number | null>(null)
   const [selectedRfqIdForComments, setSelectedRfqIdForComments] = React.useState<number | null>(null)
@@ -49,6 +53,10 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
   const [selectedTbeIdForFiles, setSelectedTbeIdForFiles] = React.useState<number | null>(null)
   const [selectedRfqIdForFiles, setSelectedRfqIdForFiles] = React.useState<number | null>(null)
 
+  const [isContactDialogOpen, setIsContactDialogOpen] = React.useState(false)
+  const [selectedVendor, setSelectedVendor] = React.useState<VendorWithTbeFields | null>(null)
+  const [selectedVendorId, setSelectedVendorId] = React.useState<number | null>(null)
+
   // 테이블 리프레시용
   const handleRefresh = React.useCallback(() => {
     router.refresh();
@@ -81,25 +89,33 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
   // -----------------------------------------------------------
   async function openCommentSheet(vendorId: number, rfqId: number) {
     setInitialComments([])
-
+    setIsLoadingComments(true)
     const comments = rowAction?.row.original.comments
-    if (comments && comments.length > 0) {
-      const commentWithAttachments: TbeComment[] = await Promise.all(
-        comments.map(async (c) => {
-          const attachments = await fetchRfqAttachmentsbyCommentId(c.id)
-          return {
-            ...c,
-            commentedBy: 1, // DB나 API 응답에 있다고 가정
-            attachments,
-          }
-        })
-      )
-      setInitialComments(commentWithAttachments)
+    try {
+      if (comments && comments.length > 0) {
+        const commentWithAttachments: TbeComment[] = await Promise.all(
+          comments.map(async (c) => {
+            const attachments = await fetchRfqAttachmentsbyCommentId(c.id)
+            return {
+              ...c,
+              commentedBy: 1, // DB나 API 응답에 있다고 가정
+              attachments,
+            }
+          })
+        )
+        setInitialComments(commentWithAttachments)
+      }
+
+      setSelectedVendorIdForComments(vendorId)
+      setSelectedRfqIdForComments(rfqId)
+      setCommentSheetOpen(true)
+    } catch (error) {
+      console.error("Error loading comments:", error)
+      toast.error("Failed to load comments")
+    } finally {
+      // End loading regardless of success/failure
+      setIsLoadingComments(false)
     }
-
-    setSelectedVendorIdForComments(vendorId)
-    setSelectedRfqIdForComments(rfqId)
-    setCommentSheetOpen(true)
   }
 
   // -----------------------------------------------------------
@@ -112,6 +128,13 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
     setIsFileDialogOpen(true)
   }
 
+  const openVendorContactsDialog = (vendorId: number, vendor: VendorWithTbeFields) => {
+    setSelectedVendorId(vendorId)
+    setSelectedVendor(vendor)
+    setIsContactDialogOpen(true)
+  }
+
+
   // -----------------------------------------------------------
   // 테이블 컬럼
   // -----------------------------------------------------------
@@ -122,6 +145,7 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
         router,
         openCommentSheet,  // 필요하면 직접 호출 가능
         openFilesDialog,
+        openVendorContactsDialog,
       }),
     [setRowAction, router]
   )
@@ -161,7 +185,7 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
     enableAdvancedFilter: true,
     initialState: {
       sorting: [{ id: "rfqVendorUpdated", desc: true }],
-      columnPinning: { right: ["actions"] },
+      columnPinning: { right: ["files", "comments"] },
     },
     getRowId: (originalRow) => (`${originalRow.id}${originalRow.rfqId}`),
     shallow: false,
@@ -176,7 +200,7 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
           filterFields={advancedFilterFields}
           shallow={false}
         >
-          <VendorsTableToolbarActions table={table}  rfqId={selectedRfqIdForFiles ?? 0} />
+          <VendorsTableToolbarActions table={table} rfqId={selectedRfqIdForFiles ?? 0} />
         </DataTableAdvancedToolbar>
       </DataTable>
 
@@ -186,7 +210,8 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
         open={commentSheetOpen}
         onOpenChange={setCommentSheetOpen}
         vendorId={selectedVendorIdForComments ?? 0}
-        rfqId={selectedRfqIdForComments ?? 0}  // ← 여기!
+        rfqId={selectedRfqIdForComments ?? 0}
+        isLoading={isLoadingComments}
         initialComments={initialComments}
       />
 
@@ -199,6 +224,20 @@ export function AllTbeTable({ promises }: VendorsTableProps) {
         rfqId={selectedRfqIdForFiles ?? 0}     // ← 여기!
         onRefresh={handleRefresh}
       />
+
+      <TbeResultDialog
+        open={rowAction?.type === "tbeResult"}
+        onOpenChange={() => setRowAction(null)}
+        tbe={rowAction?.row.original ?? null}
+      />
+
+      <VendorContactsDialog
+        isOpen={isContactDialogOpen}
+        onOpenChange={setIsContactDialogOpen}
+        vendorId={selectedVendorId}
+        vendor={selectedVendor}
+      />
+
     </>
   )
 }
\ No newline at end of file
-- 
cgit v1.2.3

