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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
"use client"
import * as React from "react"
import { Check, ChevronsUpDown, Loader } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem } from "@/components/ui/command"
import { Popover, PopoverTrigger, PopoverContent } from "@/components/ui/popover"
import { cn } from "@/lib/utils"
import { useDebounce } from "@/hooks/use-debounce"
import { getBudgetaryRfqs, type BudgetaryRfq } from "../service"
interface BudgetaryRfqSelectorProps {
selectedRfqId?: number;
onRfqSelect: (rfq: BudgetaryRfq | null) => void;
placeholder?: string;
}
export function BudgetaryRfqSelector({
selectedRfqId,
onRfqSelect,
placeholder = "Budgetary RFQ 선택..."
}: BudgetaryRfqSelectorProps) {
const [searchTerm, setSearchTerm] = React.useState("");
const debouncedSearchTerm = useDebounce(searchTerm, 300);
const [open, setOpen] = React.useState(false);
const [loading, setLoading] = React.useState(false);
const [budgetaryRfqs, setBudgetaryRfqs] = React.useState<BudgetaryRfq[]>([]);
const [selectedRfq, setSelectedRfq] = React.useState<BudgetaryRfq | null>(null);
const [page, setPage] = React.useState(1);
const [hasMore, setHasMore] = React.useState(true);
const [totalCount, setTotalCount] = React.useState(0);
const listRef = React.useRef<HTMLDivElement>(null);
// 초기 선택된 RFQ가 있을 경우 로드
React.useEffect(() => {
if (selectedRfqId && open) {
const loadSelectedRfq = async () => {
try {
const result = await getBudgetaryRfqs({
limit: 1,
// null을 undefined로 변환하여 타입 오류 해결
projectId: selectedRfq?.projectId ?? undefined
});
if ('rfqs' in result && result.rfqs) {
// 옵셔널 체이닝 또는 조건부 검사로 undefined 체크
const foundRfq = result.rfqs.find(rfq => rfq.id === selectedRfqId);
if (foundRfq) {
setSelectedRfq(foundRfq);
}
}
} catch (error) {
console.error("선택된 RFQ 로드 오류:", error);
}
};
if (!selectedRfq || selectedRfq.id !== selectedRfqId) {
loadSelectedRfq();
}
}
}, [selectedRfqId, open, selectedRfq]);
// 검색어 변경 시 데이터 리셋 및 재로드
React.useEffect(() => {
if (open) {
setPage(1);
setHasMore(true);
setBudgetaryRfqs([]);
loadBudgetaryRfqs(1, true);
}
}, [debouncedSearchTerm, open]);
// 데이터 로드 함수
const loadBudgetaryRfqs = async (pageToLoad: number, reset = false) => {
if (!open) return;
setLoading(true);
try {
const limit = 20; // 한 번에 로드할 항목 수
const result = await getBudgetaryRfqs({
search: debouncedSearchTerm,
limit,
offset: (pageToLoad - 1) * limit,
});
if ('rfqs' in result && result.rfqs) {
if (reset) {
setBudgetaryRfqs(result.rfqs);
} else {
setBudgetaryRfqs(prev => [...prev, ...result.rfqs]);
}
setTotalCount(result.totalCount);
setHasMore(result.rfqs.length === limit && (pageToLoad * limit) < result.totalCount);
setPage(pageToLoad);
}
} catch (error) {
console.error("Budgetary RFQs 로드 오류:", error);
} finally {
setLoading(false);
}
};
// 무한 스크롤 처리
const handleScroll = () => {
if (listRef.current) {
const { scrollTop, scrollHeight, clientHeight } = listRef.current;
// 스크롤이 90% 이상 내려갔을 때 다음 페이지 로드
if (scrollTop + clientHeight >= scrollHeight * 0.9 && !loading && hasMore) {
loadBudgetaryRfqs(page + 1);
}
}
};
// RFQ를 프로젝트별로 그룹화하는 함수
const groupRfqsByProject = (rfqs: BudgetaryRfq[]) => {
const groups: Record<string, {
projectId: number | null;
projectCode: string | null;
projectName: string | null;
rfqs: BudgetaryRfq[];
}> = {};
// 'No Project' 그룹 기본 생성
groups['no-project'] = {
projectId: null,
projectCode: null,
projectName: null,
rfqs: []
};
// 프로젝트별로 RFQ 그룹화
rfqs.forEach(rfq => {
const key = rfq.projectId ? `project-${rfq.projectId}` : 'no-project';
if (!groups[key] && rfq.projectId) {
groups[key] = {
projectId: rfq.projectId,
projectCode: rfq.projectCode,
projectName: rfq.projectName,
rfqs: []
};
}
groups[key].rfqs.push(rfq);
});
// 필터링된 결과가 있는 그룹만 남기기
return Object.values(groups).filter(group => group.rfqs.length > 0);
};
// 그룹화된 RFQ 목록
const groupedRfqs = React.useMemo(() => {
return groupRfqsByProject(budgetaryRfqs);
}, [budgetaryRfqs]);
// RFQ 선택 처리
const handleRfqSelect = (rfq: BudgetaryRfq | null) => {
setSelectedRfq(rfq);
onRfqSelect(rfq);
setOpen(false);
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-full justify-between"
>
{selectedRfq
? `${selectedRfq.rfqCode || ""} - ${selectedRfq.description || ""}`
: placeholder}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[400px] p-0">
<Command>
<CommandInput
placeholder="Budgetary RFQ 코드/설명/프로젝트 검색..."
value={searchTerm}
onValueChange={setSearchTerm}
/>
<CommandList
className="max-h-[300px]"
ref={listRef}
onScroll={handleScroll}
>
<CommandEmpty>검색 결과가 없습니다</CommandEmpty>
<CommandGroup>
<CommandItem
value="none"
onSelect={() => handleRfqSelect(null)}
>
<Check
className={cn(
"mr-2 h-4 w-4",
!selectedRfq
? "opacity-100"
: "opacity-0"
)}
/>
<span className="font-medium">선택 안함</span>
</CommandItem>
</CommandGroup>
{groupedRfqs.map((group, index) => (
<CommandGroup
key={`group-${group.projectId || index}`}
heading={
group.projectId
? `${group.projectCode || ""} - ${group.projectName || ""}`
: "프로젝트 없음"
}
>
{group.rfqs.map((rfq) => (
<CommandItem
key={rfq.id}
value={`${rfq.rfqCode || ""} ${rfq.description || ""}`}
onSelect={() => handleRfqSelect(rfq)}
>
<Check
className={cn(
"mr-2 h-4 w-4",
selectedRfq?.id === rfq.id
? "opacity-100"
: "opacity-0"
)}
/>
<span className="font-medium">{rfq.rfqCode || ""}</span>
<span className="ml-2 text-gray-500 truncate">
- {rfq.description || ""}
</span>
</CommandItem>
))}
</CommandGroup>
))}
{loading && (
<div className="py-2 text-center">
<Loader className="h-4 w-4 animate-spin mx-auto" />
</div>
)}
{!loading && !hasMore && budgetaryRfqs.length > 0 && (
<div className="py-2 text-center text-sm text-muted-foreground">
총 {totalCount}개 중 {budgetaryRfqs.length}개 표시됨
</div>
)}
</CommandList>
</Command>
</PopoverContent>
</Popover>
);
}
|