From e9897d416b3e7327bbd4d4aef887eee37751ae82 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Fri, 27 Jun 2025 01:16:20 +0000 Subject: (대표님) 20250627 오전 10시 작업사항 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ship/import-from-dolce-button.tsx | 82 ++++++++++++++++++---- 1 file changed, 68 insertions(+), 14 deletions(-) (limited to 'lib/vendor-document-list/ship/import-from-dolce-button.tsx') diff --git a/lib/vendor-document-list/ship/import-from-dolce-button.tsx b/lib/vendor-document-list/ship/import-from-dolce-button.tsx index 23d80981..d4728d22 100644 --- a/lib/vendor-document-list/ship/import-from-dolce-button.tsx +++ b/lib/vendor-document-list/ship/import-from-dolce-button.tsx @@ -22,6 +22,8 @@ import { Progress } from "@/components/ui/progress" import { Separator } from "@/components/ui/separator" import { SimplifiedDocumentsView } from "@/db/schema" import { ImportStatus } from "../import-service" +import { useSession } from "next-auth/react" +import { getContractIdsByVendor } from "../service" // 서버 액션 import interface ImportFromDOLCEButtonProps { allDocuments: SimplifiedDocumentsView[] // contractId 대신 문서 배열 @@ -37,31 +39,71 @@ export function ImportFromDOLCEButton({ const [isImporting, setIsImporting] = React.useState(false) const [importStatusMap, setImportStatusMap] = React.useState>(new Map()) const [statusLoading, setStatusLoading] = React.useState(false) + const [vendorContractIds, setVendorContractIds] = React.useState([]) // 서버에서 가져온 contractIds + const [loadingVendorContracts, setLoadingVendorContracts] = React.useState(false) + const { data: session } = useSession() - // 문서들에서 contractId들 추출 - const contractIds = React.useMemo(() => { + const vendorId = session?.user.companyId; + + // allDocuments에서 추출한 contractIds + const documentsContractIds = React.useMemo(() => { const uniqueIds = [...new Set(allDocuments.map(doc => doc.contractId).filter(Boolean))] return uniqueIds.sort() }, [allDocuments]) + // 최종 사용할 contractIds (allDocuments가 있으면 문서에서, 없으면 vendor의 모든 contracts) + const contractIds = React.useMemo(() => { + if (documentsContractIds.length > 0) { + return documentsContractIds + } + return vendorContractIds + }, [documentsContractIds, vendorContractIds]) + console.log(contractIds, "contractIds") + // vendorId로 contracts 가져오기 + React.useEffect(() => { + const fetchVendorContracts = async () => { + // allDocuments가 비어있고 vendorId가 있을 때만 실행 + if (allDocuments.length === 0 && vendorId) { + setLoadingVendorContracts(true) + try { + const contractIds = await getContractIdsByVendor(vendorId) + setVendorContractIds(contractIds) + } catch (error) { + console.error('Failed to fetch vendor contracts:', error) + toast.error('계약 정보를 가져오는데 실패했습니다.') + } finally { + setLoadingVendorContracts(false) + } + } + } + + fetchVendorContracts() + }, [allDocuments.length, vendorId]) + // 주요 contractId (가장 많이 나타나는 것) const primaryContractId = React.useMemo(() => { if (contractIds.length === 1) return contractIds[0] - const counts = allDocuments.reduce((acc, doc) => { - const id = doc.contractId || 0 - acc[id] = (acc[id] || 0) + 1 - return acc - }, {} as Record) + if (allDocuments.length > 0) { + const counts = allDocuments.reduce((acc, doc) => { + const id = doc.contractId || 0 + acc[id] = (acc[id] || 0) + 1 + return acc + }, {} as Record) + + return Number(Object.entries(counts) + .sort(([,a], [,b]) => b - a)[0]?.[0] || contractIds[0] || 0) + } - return Number(Object.entries(counts) - .sort(([,a], [,b]) => b - a)[0]?.[0] || contractIds[0] || 0) + return contractIds[0] || 0 }, [contractIds, allDocuments]) // 모든 contractId에 대한 상태 조회 const fetchAllImportStatus = async () => { + if (contractIds.length === 0) return + setStatusLoading(true) const statusMap = new Map() @@ -217,6 +259,10 @@ export function ImportFromDOLCEButton({ } const getStatusBadge = () => { + if (loadingVendorContracts) { + return 계약 정보 로딩 중... + } + if (statusLoading) { return DOLCE 연결 확인 중... } @@ -231,7 +277,7 @@ export function ImportFromDOLCEButton({ if (totalStats.newDocuments > 0 || totalStats.updatedDocuments > 0) { return ( - + 업데이트 가능 ({contractIds.length}개 계약) @@ -249,8 +295,9 @@ export function ImportFromDOLCEButton({ const canImport = totalStats.importEnabled && (totalStats.newDocuments > 0 || totalStats.updatedDocuments > 0) - if (contractIds.length === 0) { - return null // 계약이 없으면 버튼을 표시하지 않음 + // 로딩 중이거나 contractIds가 없으면 버튼을 표시하지 않음 + if (loadingVendorContracts || contractIds.length === 0) { + return null } return ( @@ -272,8 +319,8 @@ export function ImportFromDOLCEButton({ DOLCE에서 가져오기 {totalStats.newDocuments + totalStats.updatedDocuments > 0 && ( {totalStats.newDocuments + totalStats.updatedDocuments} @@ -292,6 +339,13 @@ export function ImportFromDOLCEButton({ + {/* 계약 소스 표시 */} + {allDocuments.length === 0 && vendorContractIds.length > 0 && ( +
+ 문서가 없어서 전체 계약에서 가져오기를 진행합니다. +
+ )} + {/* 다중 계약 정보 표시 */} {contractIds.length > 1 && (
-- cgit v1.2.3