251201_01_ENH : datatable, autocomplete, user관리 완성

This commit is contained in:
2025-12-02 13:45:35 +09:00
parent 74de3d58f7
commit 310506d9ef
45 changed files with 5313 additions and 368 deletions

View File

@@ -0,0 +1,188 @@
'use client'
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Label } from '@/components/ui/label'
import { useState, useRef } from 'react'
import {Download, Upload, X} from 'lucide-react'
import { useLoading } from '@/hooks/useLoading'
import { useDialog } from '@/hooks/useDialog'
import type { DialogId } from '@/store/jotai/dialog'
import {apiError} from "@/lib/http-util";
export type UserUploadDialogProps = {
/** jotai로 관리할 dialog id (기본값: 'userUploadDialog') */
dialogId?: DialogId
/** 필요하면 부모에서 직접 제어도 가능 */
open?: boolean
onOpenChange?: (open: boolean) => void
onUpload: (file: File) => void | Promise<void>
onDownloadTemplate?: () => void | Promise<void>
}
const UserUploadDialog = ({
dialogId = 'userUploadDialog',
open,
onOpenChange,
onUpload,
onDownloadTemplate,
}: UserUploadDialogProps) => {
const { withLoadingAsync } = useLoading()
const { isDialogOpen, showDialog, hideDialog } = useDialog()
const [selectedFile, setSelectedFile] = useState<File | null>(null)
const fileInputRef = useRef<HTMLInputElement>(null)
//실제 Dialog에 넘길 open값 (props 우선, 없으면 jotai 사용)
const resolvedOpen = open ?? isDialogOpen(dialogId)
// open change 공통 처리 (부모 콜백 + jotai 동시 반영)
const handleOpenChange = (nextOpen: boolean) => {
// 1) 부모 콜백 먼저
onOpenChange?.(nextOpen)
// 2) jotai 상태 업데이트
if (nextOpen) {
showDialog(dialogId)
} else {
hideDialog(dialogId)
}
}
const handleFileSelect = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0]
if (file) {
const allowedTypes = [
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'text/csv',
]
if (
allowedTypes.includes(file.type) ||
file.name.endsWith('.xlsx') ||
file.name.endsWith('.xls') ||
file.name.endsWith('.csv')
) {
setSelectedFile(file)
} else {
alert('엑셀 파일만 업로드 가능합니다. (.xlsx, .xls, .csv)')
}
}
}
const handleRemoveFile = () => {
setSelectedFile(null)
if (fileInputRef.current) {
fileInputRef.current.value = ''
}
}
const handleUploadClick = () => {
fileInputRef.current?.click()
}
const handleSubmit = async () => {
if (!selectedFile) {
alert('파일을 선택해주세요.')
return
}
withLoadingAsync(async () => {
try {
await onUpload(selectedFile)
// 성공 시 초기화 + 닫기
handleRemoveFile()
handleOpenChange(false)
} catch (e) {
const err = apiError(e);
console.error(err.status, err.code, err.message);
alert('업로드에 실패했습니다.')
}
})
}
// ✅ Dialog에서 닫힐 때: 파일도 초기화 + 상태 닫기
const handleDialogOpenChange = (nextOpen: boolean) => {
if (!nextOpen) {
handleRemoveFile()
}
handleOpenChange(nextOpen)
}
return (
<Dialog open={resolvedOpen} onOpenChange={handleDialogOpenChange}>
<DialogContent className="sm:max-w-[600px]">
<DialogHeader>
<DialogTitle className=" content-text-2xl font-bold">엑셀 업로드</DialogTitle>
</DialogHeader>
<div className="space-y-6 py-4">
<div>
<div className="flex gap-2">
<Button
type="button"
variant="default"
className="content-text-lg bg-green-600 hover:bg-green-700"
onClick={onDownloadTemplate}
>
<Download className="w-4 h-4 mr-2" />
양식 다운로드
</Button>
<Button
type="button"
variant="outline"
className="flex-1 content-text-lg w-full"
onClick={handleUploadClick}
>
<Upload className="w-4 h-4 mr-2" />
{selectedFile ? selectedFile.name : '선택된 파일 없음'}
</Button>
<div className="w-9 h-8">
{selectedFile && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={handleRemoveFile}
>
<X className="w-4 h-4" />
</Button>
)}
</div>
</div>
<input
ref={fileInputRef}
type="file"
accept=".xlsx,.xls,.csv"
className="hidden"
onChange={handleFileSelect}
/>
</div>
</div>
<DialogFooter>
<div className="flex items-center justify-end w-full gap-2">
<Button
type="button"
onClick={handleSubmit}
disabled={!selectedFile}
>
확인
</Button>
</div>
</DialogFooter>
</DialogContent>
</Dialog>
)
}
export default UserUploadDialog