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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
"use client"
import { useEffect, useTransition, useState, useRef } from "react"
import { useRouter, useParams } from "next/navigation"
import { z } from "zod"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { Search, X } from "lucide-react"
import { customAlphabet } from "nanoid"
import { parseAsStringEnum, useQueryState } from "nuqs"
import { Button } from "@/components/ui/button"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Badge } from "@/components/ui/badge"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { cn } from "@/lib/utils"
import { getFiltersStateParser } from "@/lib/parsers"
// nanoid 생성기
const generateId = customAlphabet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 6)
// RFQ 필터 스키마 정의
const rfqFilterSchema = z.object({
rfqCode: z.string().optional(),
projectCode: z.string().optional(),
picName: z.string().optional(),
packageNo: z.string().optional(),
packageName: z.string().optional(),
status: z.string().optional(),
})
// RFQ 상태 옵션 정의
const rfqStatusOptions = [
{ value: "DRAFT", label: "초안" },
{ value: "Doc. Received", label: "문서접수" },
{ value: "PIC Assigned", label: "담당자배정" },
{ value: "Doc. Confirmed", label: "문서확인" },
{ value: "Init. RFQ Sent", label: "초기RFQ발송" },
{ value: "Init. RFQ Answered", label: "초기RFQ회신" },
{ value: "TBE started", label: "TBE시작" },
{ value: "TBE finished", label: "TBE완료" },
{ value: "Final RFQ Sent", label: "최종RFQ발송" },
{ value: "Quotation Received", label: "견적접수" },
{ value: "Vendor Selected", label: "업체선정" },
]
type RFQFilterFormValues = z.infer<typeof rfqFilterSchema>
interface RFQFilterSheetProps {
isOpen: boolean;
onClose: () => void;
onSearch?: () => void;
isLoading?: boolean;
}
export function RFQFilterSheet({
isOpen,
onClose,
onSearch,
isLoading = false
}: RFQFilterSheetProps) {
const router = useRouter()
const params = useParams();
const lng = params ? (params.lng as string) : 'ko';
const [isPending, startTransition] = useTransition()
// 초기화 상태 추가 - 폼 초기화 중에는 상태 변경을 방지
const [isInitializing, setIsInitializing] = useState(false)
// 마지막으로 적용된 필터를 추적하기 위한 ref
const lastAppliedFilters = useRef<string>("")
// nuqs로 URL 상태 관리
const [filters, setFilters] = useQueryState(
"basicFilters",
getFiltersStateParser().withDefault([])
)
// joinOperator 설정
const [joinOperator, setJoinOperator] = useQueryState(
"basicJoinOperator",
parseAsStringEnum(["and", "or"]).withDefault("and")
)
// 현재 URL의 페이지 파라미터도 가져옴
const [page, setPage] = useQueryState("page", { defaultValue: "1" })
// 폼 상태 초기화
const form = useForm<RFQFilterFormValues>({
resolver: zodResolver(rfqFilterSchema),
defaultValues: {
rfqCode: "",
projectCode: "",
picName: "",
packageNo: "",
packageName: "",
status: "",
},
})
// URL 필터에서 초기 폼 상태 설정
useEffect(() => {
// 현재 필터를 문자열로 직렬화
const currentFiltersString = JSON.stringify(filters);
// 패널이 열렸고, 필터가 있고, 마지막에 적용된 필터와 다를 때만 업데이트
if (isOpen && filters && filters.length > 0 && currentFiltersString !== lastAppliedFilters.current) {
setIsInitializing(true);
const formValues = { ...form.getValues() };
let formUpdated = false;
filters.forEach(filter => {
if (filter.id in formValues) {
// @ts-ignore - 동적 필드 접근
formValues[filter.id] = filter.value;
formUpdated = true;
}
});
// 폼 값이 변경된 경우에만 reset으로 한 번에 업데이트
if (formUpdated) {
form.reset(formValues);
lastAppliedFilters.current = currentFiltersString;
}
setIsInitializing(false);
}
}, [filters, isOpen])
// 현재 적용된 필터 카운트
const getActiveFilterCount = () => {
return filters?.length || 0
}
// 폼 제출 핸들러
async function onSubmit(data: RFQFilterFormValues) {
// 초기화 중이면 제출 방지
if (isInitializing) return;
startTransition(async () => {
try {
// 필터 배열 생성
const newFilters = []
if (data.rfqCode?.trim()) {
newFilters.push({
id: "rfqCode",
value: data.rfqCode.trim(),
type: "text",
operator: "iLike",
rowId: generateId()
})
}
if (data.projectCode?.trim()) {
newFilters.push({
id: "projectCode",
value: data.projectCode.trim(),
type: "text",
operator: "iLike",
rowId: generateId()
})
}
if (data.picName?.trim()) {
newFilters.push({
id: "picName",
value: data.picName.trim(),
type: "text",
operator: "iLike",
rowId: generateId()
})
}
if (data.packageNo?.trim()) {
newFilters.push({
id: "packageNo",
value: data.packageNo.trim(),
type: "text",
operator: "iLike",
rowId: generateId()
})
}
if (data.packageName?.trim()) {
newFilters.push({
id: "packageName",
value: data.packageName.trim(),
type: "text",
operator: "iLike",
rowId: generateId()
})
}
if (data.status?.trim()) {
newFilters.push({
id: "status",
value: data.status.trim(),
type: "select",
operator: "eq",
rowId: generateId()
})
}
// 수동으로 URL 업데이트 (nuqs 대신)
const currentUrl = new URL(window.location.href);
const params = new URLSearchParams(currentUrl.search);
// 기존 필터 관련 파라미터 제거
params.delete('basicFilters');
params.delete('rfqBasicFilters');
params.delete('basicJoinOperator');
params.delete('rfqBasicJoinOperator');
params.delete('page');
// 새로운 필터 추가
if (newFilters.length > 0) {
params.set('basicFilters', JSON.stringify(newFilters));
params.set('basicJoinOperator', joinOperator);
}
// 페이지를 1로 설정
params.set('page', '1');
const newUrl = `${currentUrl.pathname}?${params.toString()}`;
console.log("New RFQ Filter URL:", newUrl);
// 페이지 완전 새로고침으로 서버 렌더링 강제
window.location.href = newUrl;
// 마지막 적용된 필터 업데이트
lastAppliedFilters.current = JSON.stringify(newFilters);
// 필터 업데이트 후 조회 핸들러 호출 (제공된 경우)
if (onSearch) {
console.log("Calling RFQ onSearch...");
onSearch();
}
console.log("=== RFQ Filter Submit Complete ===");
} catch (error) {
console.error("RFQ 필터 적용 오류:", error);
}
})
}
// 필터 초기화 핸들러
async function handleReset() {
try {
setIsInitializing(true);
form.reset({
rfqCode: "",
projectCode: "",
picName: "",
packageNo: "",
packageName: "",
status: "",
});
console.log("=== RFQ Filter Reset Debug ===");
console.log("Current URL before reset:", window.location.href);
// 수동으로 URL 초기화
const currentUrl = new URL(window.location.href);
const params = new URLSearchParams(currentUrl.search);
// 필터 관련 파라미터 제거
params.delete('basicFilters');
params.delete('rfqBasicFilters');
params.delete('basicJoinOperator');
params.delete('rfqBasicJoinOperator');
params.set('page', '1');
const newUrl = `${currentUrl.pathname}?${params.toString()}`;
console.log("Reset URL:", newUrl);
// 페이지 완전 새로고침
window.location.href = newUrl;
// 마지막 적용된 필터 초기화
lastAppliedFilters.current = "";
console.log("RFQ 필터 초기화 완료");
setIsInitializing(false);
} catch (error) {
console.error("RFQ 필터 초기화 오류:", error);
setIsInitializing(false);
}
}
// Don't render if not open (for side panel use)
if (!isOpen) {
return null;
}
return (
<div className="flex flex-col h-full max-h-full bg-[#F5F7FB] px-6 sm:px-8" style={{backgroundColor:"#F5F7FB", paddingLeft:"2rem", paddingRight:"2rem"}}>
{/* Filter Panel Header */}
<div className="flex items-center justify-between px-6 min-h-[60px] shrink-0">
<h3 className="text-lg font-semibold whitespace-nowrap">RFQ 검색 필터</h3>
<div className="flex items-center gap-2">
{getActiveFilterCount() > 0 && (
<Badge variant="secondary" className="px-2 py-1">
{getActiveFilterCount()}개 필터 적용됨
</Badge>
)}
</div>
</div>
{/* Join Operator Selection */}
<div className="px-6 shrink-0">
<label className="text-sm font-medium">조건 결합 방식</label>
<Select
value={joinOperator}
onValueChange={(value: "and" | "or") => setJoinOperator(value)}
disabled={isInitializing}
>
<SelectTrigger className="h-8 w-[180px] mt-2 bg-white">
<SelectValue placeholder="조건 결합 방식" />
</SelectTrigger>
<SelectContent>
<SelectItem value="and">모든 조건 충족 (AND)</SelectItem>
<SelectItem value="or">하나라도 충족 (OR)</SelectItem>
</SelectContent>
</Select>
</div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col h-full min-h-0">
{/* Scrollable content area */}
<div className="flex-1 min-h-0 overflow-y-auto px-6 pb-4">
<div className="space-y-4 pt-2">
{/* RFQ 코드 */}
<FormField
control={form.control}
name="rfqCode"
render={({ field }) => (
<FormItem>
<FormLabel>RFQ 코드</FormLabel>
<FormControl>
<div className="relative">
<Input
placeholder="RFQ 코드 입력"
{...field}
className={cn(field.value && "pr-8", "bg-white")}
disabled={isInitializing}
/>
{field.value && (
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-0 top-0 h-full px-2"
onClick={(e) => {
e.stopPropagation();
form.setValue("rfqCode", "");
}}
disabled={isInitializing}
>
<X className="size-3.5" />
<span className="sr-only">Clear</span>
</Button>
)}
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* 프로젝트 코드 */}
<FormField
control={form.control}
name="projectCode"
render={({ field }) => (
<FormItem>
<FormLabel>프로젝트 코드</FormLabel>
<FormControl>
<div className="relative">
<Input
placeholder="프로젝트 코드 입력"
{...field}
className={cn(field.value && "pr-8", "bg-white")}
disabled={isInitializing}
/>
{field.value && (
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-0 top-0 h-full px-2"
onClick={(e) => {
e.stopPropagation();
form.setValue("projectCode", "");
}}
disabled={isInitializing}
>
<X className="size-3.5" />
<span className="sr-only">Clear</span>
</Button>
)}
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* 담당자명 */}
<FormField
control={form.control}
name="picName"
render={({ field }) => (
<FormItem>
<FormLabel>담당자명</FormLabel>
<FormControl>
<div className="relative">
<Input
placeholder="담당자명 입력"
{...field}
className={cn(field.value && "pr-8", "bg-white")}
disabled={isInitializing}
/>
{field.value && (
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-0 top-0 h-full px-2"
onClick={(e) => {
e.stopPropagation();
form.setValue("picName", "");
}}
disabled={isInitializing}
>
<X className="size-3.5" />
<span className="sr-only">Clear</span>
</Button>
)}
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* 패키지 번호 */}
<FormField
control={form.control}
name="packageNo"
render={({ field }) => (
<FormItem>
<FormLabel>패키지 번호</FormLabel>
<FormControl>
<div className="relative">
<Input
placeholder="패키지 번호 입력"
{...field}
className={cn(field.value && "pr-8", "bg-white")}
disabled={isInitializing}
/>
{field.value && (
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-0 top-0 h-full px-2"
onClick={(e) => {
e.stopPropagation();
form.setValue("packageNo", "");
}}
disabled={isInitializing}
>
<X className="size-3.5" />
<span className="sr-only">Clear</span>
</Button>
)}
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* 패키지명 */}
<FormField
control={form.control}
name="packageName"
render={({ field }) => (
<FormItem>
<FormLabel>패키지명</FormLabel>
<FormControl>
<div className="relative">
<Input
placeholder="패키지명 입력"
{...field}
className={cn(field.value && "pr-8", "bg-white")}
disabled={isInitializing}
/>
{field.value && (
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-0 top-0 h-full px-2"
onClick={(e) => {
e.stopPropagation();
form.setValue("packageName", "");
}}
disabled={isInitializing}
>
<X className="size-3.5" />
<span className="sr-only">Clear</span>
</Button>
)}
</div>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{/* RFQ 상태 */}
<FormField
control={form.control}
name="status"
render={({ field }) => (
<FormItem>
<FormLabel>RFQ 상태</FormLabel>
<Select
value={field.value}
onValueChange={field.onChange}
disabled={isInitializing}
>
<FormControl>
<SelectTrigger className={cn(field.value && "pr-8", "bg-white")}>
<div className="flex justify-between w-full">
<SelectValue placeholder="RFQ 상태 선택" />
{field.value && (
<Button
type="button"
variant="ghost"
size="icon"
className="h-4 w-4 -mr-2"
onClick={(e) => {
e.stopPropagation();
form.setValue("status", "");
}}
disabled={isInitializing}
>
<X className="size-3" />
<span className="sr-only">Clear</span>
</Button>
)}
</div>
</SelectTrigger>
</FormControl>
<SelectContent>
{rfqStatusOptions.map(option => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
{/* Fixed buttons at bottom */}
<div className="p-4 shrink-0">
<div className="flex gap-2 justify-end">
<Button
type="button"
variant="outline"
onClick={handleReset}
disabled={isPending || getActiveFilterCount() === 0 || isInitializing}
className="px-4"
>
초기화
</Button>
<Button
type="submit"
variant="samsung"
disabled={isPending || isLoading || isInitializing}
className="px-4"
>
<Search className="size-4 mr-2" />
{isPending || isLoading ? "조회 중..." : "조회"}
</Button>
</div>
</div>
</form>
</Form>
</div>
)
}
|