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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
"use client"
import * as React from "react"
import { type Table } from "@tanstack/react-table"
import {
Send, Download, FileSpreadsheet, Trash
} from "lucide-react"
import { toast } from "sonner"
import { useSession } from "next-auth/react"
import { exportBiddingsToExcel } from "./export-biddings-to-excel"
import { Button } from "@/components/ui/button"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { BiddingListItem } from "@/db/schema"
import { TransmissionDialog } from "./biddings-transmission-dialog"
import { BiddingCreateDialog } from "@/components/bidding/create/bidding-create-dialog"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { createBiddingSchema } from "@/lib/bidding/validation"
import { deleteBidding } from "@/lib/bidding/delete-action"
import { BiddingDeleteDialog } from "./biddings-delete-dialog"
interface BiddingsTableToolbarActionsProps {
table: Table<BiddingListItem>
}
export function BiddingsTableToolbarActions({ table }: BiddingsTableToolbarActionsProps) {
const { data: session } = useSession()
const [isExporting, setIsExporting] = React.useState(false)
const [isTransmissionDialogOpen, setIsTransmissionDialogOpen] = React.useState(false)
const [isDeleteDialogOpen, setIsDeleteDialogOpen] = React.useState(false)
const userId = session?.user?.id ? Number(session.user.id) : 1
// 입찰 생성 폼
const form = useForm({
resolver: zodResolver(createBiddingSchema),
defaultValues: {
revision: 0,
title: '',
description: '',
content: '',
noticeType: 'standard' as const,
contractType: 'general' as const,
biddingType: 'equipment' as const,
awardCount: 'single' as const,
currency: 'KRW',
status: 'bidding_generated' as const,
bidPicName: '',
bidPicCode: '',
supplyPicName: '',
supplyPicCode: '',
requesterName: '',
attachments: [],
vendorAttachments: [],
hasSpecificationMeeting: false,
hasPrDocument: false,
isPublic: false,
isUrgent: false,
purchasingOrganization: '',
biddingConditions: {
paymentTerms: '',
taxConditions: 'V1',
incoterms: 'DAP',
incotermsOption: '',
contractDeliveryDate: '',
shippingPort: '',
destinationPort: '',
isPriceAdjustmentApplicable: false,
sparePartOptions: '',
},
},
})
// 선택된 입찰들
const selectedBiddings = React.useMemo(() => {
return table
.getFilteredSelectedRowModel()
.rows
.map(row => row.original)
}, [table.getFilteredSelectedRowModel().rows])
// 업체선정이 완료된 입찰만 전송 가능
const canTransmit = true
// 삭제 가능 여부: 선택된 항목이 정확히 1개이고, '입찰생성' 상태여야 함
const canDelete = React.useMemo(() => {
return selectedBiddings.length === 1 && selectedBiddings[0].status === 'bidding_generated'
}, [selectedBiddings])
// Excel 내보내기 핸들러
const handleExport = React.useCallback(async () => {
try {
setIsExporting(true)
await exportBiddingsToExcel(table, {
filename: "입찰목록",
onlySelected: false,
})
toast.success("Excel 파일이 다운로드되었습니다.")
} catch (error) {
console.error("Excel export error:", error)
toast.error("Excel 내보내기 중 오류가 발생했습니다.")
} finally {
setIsExporting(false)
}
}, [table])
return (
<>
<div className="flex items-center gap-2">
{/* 신규입찰 생성 버튼 */}
<BiddingCreateDialog form={form} onSuccess={() => {
// 성공 시 테이블 새로고침 등 추가 작업
// window.location.reload()
}} />
{/* Excel 내보내기 버튼 */}
<Button
variant="outline"
size="sm"
onClick={handleExport}
disabled={isExporting}
className="gap-2"
>
<FileSpreadsheet className="size-4" aria-hidden="true" />
<span className="hidden sm:inline">{isExporting ? "내보내는 중..." : "Excel 내보내기"}</span>
</Button>
{/* 전송하기 (업체선정 완료된 입찰만) */}
<Button
variant="default"
size="sm"
onClick={() => setIsTransmissionDialogOpen(true)}
disabled={!canTransmit}
className="gap-2"
>
<Send className="size-4" aria-hidden="true" />
<span className="hidden sm:inline">전송하기</span>
</Button>
{/* 삭제 버튼 */}
<Button
variant="destructive"
size="sm"
onClick={() => setIsDeleteDialogOpen(true)}
disabled={!canDelete}
className="gap-2"
>
<Trash className="size-4" aria-hidden="true" />
<span className="hidden sm:inline">삭제</span>
</Button>
</div>
{/* 전송 다이얼로그 */}
<TransmissionDialog
open={isTransmissionDialogOpen}
onOpenChange={setIsTransmissionDialogOpen}
bidding={selectedBiddings[0]}
userId={userId}
/>
{/* 삭제 다이얼로그 */}
<BiddingDeleteDialog
open={isDeleteDialogOpen}
onOpenChange={setIsDeleteDialogOpen}
bidding={selectedBiddings[0]}
onSuccess={() => table.resetRowSelection()}
/>
</>
)
}
|