"use client" import * as React from "react" import { useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Plus, Trash2 } from "lucide-react" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { toast } from "sonner" import { codeGroups } from "@/db/schema/codeGroups" import { createComboBoxOption } from "../service" interface ComboBoxOptionsDialogProps { open: boolean onOpenChange: (open: boolean) => void codeGroup: typeof codeGroups.$inferSelect | null onSuccess: () => void } interface OptionRow { id: string description: string remark: string } export function ComboBoxOptionsDialog({ open, onOpenChange, codeGroup, onSuccess }: ComboBoxOptionsDialogProps) { const [optionRows, setOptionRows] = useState([]) const [loading, setLoading] = useState(false) // 다이얼로그가 열릴 때 초기 행 생성 React.useEffect(() => { if (open && optionRows.length === 0) { addRow() } }, [open]) // 새 행 추가 const addRow = () => { const newRow: OptionRow = { id: `row-${Date.now()}-${Math.random()}`, description: "", remark: "", } setOptionRows(prev => [...prev, newRow]) } // 행 삭제 const removeRow = (id: string) => { setOptionRows(prev => prev.filter(row => row.id !== id)) } // 행 업데이트 const updateRow = (id: string, field: keyof OptionRow, value: string) => { setOptionRows(prev => prev.map(row => row.id === id ? { ...row, [field]: value } : row ) ) } // 일괄 저장 const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!codeGroup) return // 유효한 행들만 필터링 (Description이 있는 것만) const validRows = optionRows.filter(row => row.description.trim()) if (validRows.length === 0) { toast.error("최소 하나의 Description을 입력해주세요.") return } setLoading(true) try { let successCount = 0 let errorCount = 0 // 각 행을 순차적으로 저장 for (const row of validRows) { try { const result = await createComboBoxOption({ codeGroupId: codeGroup.id, code: "", // 서비스에서 자동 생성 description: row.description.trim(), remark: row.remark.trim() || undefined, }) if (result.success) { successCount++ } else { errorCount++ } } catch (error) { console.error("옵션 추가 실패:", error) errorCount++ } } if (successCount > 0) { toast.success(`${successCount}개의 옵션이 추가되었습니다.${errorCount > 0 ? ` (${errorCount}개 실패)` : ''}`) // 폼 초기화 setOptionRows([]) onSuccess() } else { toast.error("모든 옵션 추가에 실패했습니다.") } } catch (error) { console.error("옵션 추가 실패:", error) toast.error("옵션 추가에 실패했습니다.") } finally { setLoading(false) } } const handleCancel = () => { // 폼 초기화 setOptionRows([]) onOpenChange(false) } return ( Combo Box 옵션 추가 {codeGroup?.description}에 새로운 옵션들을 추가합니다. Code는 자동으로 생성됩니다.
{/* 테이블 헤더와 추가 버튼 */}

옵션 목록

{/* 옵션 테이블 - 항상 표시 */}
Description * Remark {optionRows.map((row) => ( updateRow(row.id, "description", e.target.value)} className="border-0 focus-visible:ring-1 bg-transparent h-8" /> updateRow(row.id, "remark", e.target.value)} className="border-0 focus-visible:ring-1 bg-transparent h-8" /> ))}
) }