import { useState } from 'react' import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query' import { Plus, Edit, Trash2, X } from 'lucide-react' import { getProducts, createProduct, updateProduct, deleteProduct } from '../api/client' export default function ProductList() { const qc = useQueryClient() const [keyword, setKeyword] = useState('') const [modal, setModal] = useState(null) const { data: products = [], isLoading } = useQuery({ queryKey: ['products', keyword], queryFn: () => getProducts({ keyword: keyword || undefined }), }) const saveMut = useMutation({ mutationFn: (d: any) => d.id ? updateProduct(d.id, d) : createProduct(d), onSuccess: () => { qc.invalidateQueries({ queryKey: ['products'] }); setModal(null) }, }) const deleteMut = useMutation({ mutationFn: deleteProduct, onSuccess: () => qc.invalidateQueries({ queryKey: ['products'] }), }) if (isLoading) return
로딩 중...
return (

상품/가격 관리

setKeyword(e.target.value)} placeholder="상품명·코드 검색" className="bg-panel border border-edge rounded px-3 py-1.5 text-sm text-gray-300 w-48" />
{['테넌트','매장','상품코드','상품명','카테고리','정가','판매가','화폐','상태','액션'].map(h => ( ))} {(products as any[]).map((p: any) => ( ))} {(products as any[]).length === 0 && ( )}
{h}
{p.tenantCode} {p.storeName || '-'} {p.productCode} {p.productName} {p.category} {p.price?.toLocaleString()} {p.salePrice?.toLocaleString()} {p.currency} {p.active ? '활성' : '비활성'}
상품이 없습니다
{modal && (

{modal.id ? '상품 수정 / 가격 변경' : '상품 추가'}

saveMut.mutate(d)} />
)}
) } function ProductForm({ initial, onSave }: { initial: any; onSave: (d: any) => void }) { const [f, setF] = useState({ tenantCode: initial.tenantCode || 'EMART', storeId: initial.storeId ?? '', productCode: initial.productCode || '', productName: initial.productName || '', category: initial.category || '', price: initial.price ?? '', salePrice: initial.salePrice ?? '', currency: initial.currency || 'KRW', active: initial.active !== false, id: initial.id, }) const set = (k: string) => (e: React.ChangeEvent) => setF(p => ({ ...p, [k]: e.target.value })) const submit = (e: React.FormEvent) => { e.preventDefault() onSave({ ...f, storeId: f.storeId === '' ? null : Number(f.storeId), price: f.price === '' ? null : Number(f.price), salePrice: f.salePrice === '' ? null : Number(f.salePrice), active: f.active === true || (f as any).active === 'true', }) } return (
{[ { label: '매장 ID', key: 'storeId', type: 'number', ph: '예: 1' }, { label: '상품코드', key: 'productCode' }, { label: '상품명', key: 'productName' }, { label: '카테고리', key: 'category' }, { label: '정가', key: 'price', type: 'number' }, { label: '판매가', key: 'salePrice', type: 'number' }, ].map(({ label, key, type, ph }) => (
))}
) }