'use client' import { useEffect, useMemo, useState } from 'react' import { Plus, X } from 'lucide-react' import { KriDialog } from '@/components/common/KriDialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { useDialog } from '@/hooks/useDialog' import type { DialogId } from '@/store/jotai/dialog' export type BatchEditOption = { id: number label: string } export type BatchEditRow = { id: string headerId: number value: string } export type ListBatchEditDialogProps = { dialogId?: DialogId open?: boolean onOpenChange?: (open: boolean) => void appliedFilters?: Record headerOptions: BatchEditOption[] initialRows?: BatchEditRow[] onConfirm: (rows: BatchEditRow[]) => void | Promise } const createRow = (headerId: number): BatchEditRow => ({ id: crypto.randomUUID(), headerId, value: '', }) const ListBatchEditDialog = ({ dialogId = 'listBatchEditDialog', open, onOpenChange, appliedFilters = {}, headerOptions, initialRows = [], onConfirm, }: ListBatchEditDialogProps) => { const { isDialogOpen, showDialog, hideDialog } = useDialog() const [rows, setRows] = useState([]) const [submitting, setSubmitting] = useState(false) const resolvedOpen = open ?? isDialogOpen(dialogId) const defaultHeaderId = headerOptions[0]?.id ?? 0 useEffect(() => { if (!resolvedOpen) return if (initialRows.length > 0) { setRows(initialRows.map((row) => ({ ...row }))) return } if (defaultHeaderId > 0) { setRows([createRow(defaultHeaderId)]) } }, [resolvedOpen, initialRows, defaultHeaderId]) const filterSummary = useMemo(() => { return Object.entries(appliedFilters) .map(([headerId, values]) => { const headerLabel = headerOptions.find((opt) => opt.id === Number(headerId))?.label ?? headerId return `${headerLabel}: ${values.join(', ')}` }) .join(' / ') }, [appliedFilters, headerOptions]) const handleOpenChange = (nextOpen: boolean) => { onOpenChange?.(nextOpen) if (nextOpen) { showDialog(dialogId) return } hideDialog(dialogId) } const handleAddRow = () => { if (defaultHeaderId <= 0) return setRows((prev) => [...prev, createRow(defaultHeaderId)]) } const handleRemoveRow = (id: string) => { setRows((prev) => prev.filter((row) => row.id !== id)) } const handleChangeHeader = (id: string, headerId: number) => { setRows((prev) => prev.map((row) => (row.id === id ? { ...row, headerId } : row))) } const handleChangeValue = (id: string, value: string) => { setRows((prev) => prev.map((row) => (row.id === id ? { ...row, value } : row))) } const handleConfirm = async () => { const normalized = rows.filter((row) => row.headerId > 0 && row.value.trim().length > 0) if (normalized.length === 0) { alert('일괄 수정할 항목을 입력해주세요.') return } setSubmitting(true) try { await onConfirm(normalized) handleOpenChange(false) } finally { setSubmitting(false) } } return ( 일괄 수정} contentClassName="sm:max-w-[900px]" bodyClassName="space-y-4" footer={ <> } >

적용된 필터

{filterSummary.length > 0 ? filterSummary : '적용된 필터가 없습니다.'}

헤더 수정 값
{rows.map((row) => (
handleChangeValue(row.id, e.target.value)} placeholder="수정할 값을 입력해주세요" />
))}
) } export default ListBatchEditDialog