summaryrefslogtreecommitdiff
path: root/lib/vendors/table/vendors-table-toolbar-actions.tsx
blob: 3cb2c55285060bd4345c5415e8d00de14d0b3a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use client"

import * as React from "react"
import { type Table } from "@tanstack/react-table"
import { Download, Upload, Check, BuildingIcon } from "lucide-react"
import { toast } from "sonner"

import { exportTableToExcel } from "@/lib/export"
import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { Vendor } from "@/db/schema/vendors"
import { ApproveVendorsDialog } from "./approve-vendor-dialog"
import { RequestPQVendorsDialog } from "./request-vendor-pg-dialog"
import { RequestProjectPQDialog } from "./request-project-pq-dialog"
import { SendVendorsDialog } from "./send-vendor-dialog"
import { RequestVendorsInvestigateDialog } from "./request-vendor-investigate-dialog"
import { RequestInfoDialog } from "./request-additional-Info-dialog"

interface VendorsTableToolbarActionsProps {
  table: Table<Vendor>
}

export function VendorsTableToolbarActions({ table }: VendorsTableToolbarActionsProps) {
  // 파일 input을 숨기고, 버튼 클릭 시 참조해 클릭하는 방식
  const fileInputRef = React.useRef<HTMLInputElement>(null)
  
  // 선택된 벤더 중 PENDING_REVIEW 상태인 벤더만 필터링
  const pendingReviewVendors = React.useMemo(() => {
    return table
      .getFilteredSelectedRowModel()
      .rows
      .map(row => row.original)
      .filter(vendor => vendor.status === "PENDING_REVIEW");
  }, [table.getFilteredSelectedRowModel().rows]);
  
  // 선택된 벤더 중 IN_REVIEW 상태인 벤더만 필터링
  const inReviewVendors = React.useMemo(() => {
    return table
      .getFilteredSelectedRowModel()
      .rows
      .map(row => row.original)
      .filter(vendor => vendor.status === "IN_REVIEW");
  }, [table.getFilteredSelectedRowModel().rows]);
  
  const approvedVendors = React.useMemo(() => {
    return table
      .getFilteredSelectedRowModel()
      .rows
      .map(row => row.original)
      .filter(vendor => vendor.status === "APPROVED");
  }, [table.getFilteredSelectedRowModel().rows]);
  
  const sendVendors = React.useMemo(() => {
    return table
      .getFilteredSelectedRowModel()
      .rows
      .map(row => row.original)
      .filter(vendor => vendor.status === "READY_TO_SEND");
  }, [table.getFilteredSelectedRowModel().rows]);
  
  const pqApprovedVendors = React.useMemo(() => {
    return table
      .getFilteredSelectedRowModel()
      .rows
      .map(row => row.original)
      .filter(vendor => vendor.status === "PQ_APPROVED");
  }, [table.getFilteredSelectedRowModel().rows]);
  
  // 프로젝트 PQ를 보낼 수 있는 벤더 상태 필터링
  const projectPQEligibleVendors = React.useMemo(() => {
    return table
      .getFilteredSelectedRowModel()
      .rows
      .map(row => row.original)
      .filter(vendor => 
        ["PENDING_REVIEW", "IN_REVIEW", "IN_PQ", "PQ_APPROVED", "APPROVED", "READY_TO_SEND", "ACTIVE"].includes(vendor.status)
      );
  }, [table.getFilteredSelectedRowModel().rows]);
  
  return (
    <div className="flex items-center gap-2">
      {/* 승인 다이얼로그: PENDING_REVIEW 상태인 벤더가 있을 때만 표시 */}
      {pendingReviewVendors.length > 0 && (
        <ApproveVendorsDialog
          vendors={pendingReviewVendors}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}
      
      {/* 일반 PQ 요청: IN_REVIEW 상태인 벤더가 있을 때만 표시 */}
      {inReviewVendors.length > 0 && (
        <RequestPQVendorsDialog
          vendors={inReviewVendors}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}
      
      {/* 프로젝트 PQ 요청: 적격 상태의 벤더가 있을 때만 표시 */}
      {projectPQEligibleVendors.length > 0 && (
        <RequestProjectPQDialog
          vendors={projectPQEligibleVendors}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}
      
      {approvedVendors.length > 0 && (
        <RequestInfoDialog
          vendors={approvedVendors}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}
      
      {sendVendors.length > 0 && (
        <RequestInfoDialog
          vendors={sendVendors}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}
      
      {pqApprovedVendors.length > 0 && (
        <RequestVendorsInvestigateDialog
          vendors={pqApprovedVendors}
          onSuccess={() => table.toggleAllRowsSelected(false)}
        />
      )}
      
      {/** 4) Export 버튼 */}
      <Button
        variant="outline"
        size="sm"
        onClick={() =>
          exportTableToExcel(table, {
            filename: "vendors",
            excludeColumns: ["select", "actions"],
          })
        }
        className="gap-2"
      >
        <Download className="size-4" aria-hidden="true" />
        <span className="hidden sm:inline">Export</span>
      </Button>
    </div>
  )
}