Files
contact-front/src/components/dialog/UserUploadDialog.tsx

169 lines
4.9 KiB
TypeScript

'use client'
import { KriDialog } from '@/components/common/KriDialog'
import { Button } from '@/components/ui/button'
import { useState, useRef } from 'react'
import {Download, Upload, X} from 'lucide-react'
import { useDialog } from '@/hooks/useDialog'
import type { DialogId } from '@/store/jotai/dialog'
export type UserUploadDialogProps = {
/** jotai로 관리할 dialog id */
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 { isDialogOpen, showDialog, hideDialog } = useDialog();
const [submitting, setSubmitting] = useState(false);
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
// 실제 Dialog에 전달할 open 값
const resolvedOpen = open ?? isDialogOpen(dialogId);
// 부모 콜백과 jotai 상태를 함께 처리
const handleOpenChange = (nextOpen: boolean) => {
onOpenChange?.(nextOpen)
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;
}
setSubmitting(true);
try {
await onUpload(selectedFile);
handleRemoveFile();
handleOpenChange(false);
} catch (e) {
console.error(e);
} finally {
setSubmitting(false);
}
};
// Dialog가 닫힐 때 파일 선택 상태 초기화
const handleDialogOpenChange = (nextOpen: boolean) => {
if (!nextOpen) {
handleRemoveFile()
}
handleOpenChange(nextOpen)
}
return (
<KriDialog
open={resolvedOpen}
onOpenChange={handleDialogOpenChange}
title={<span className="text-2xl font-bold">User upload</span>}
contentClassName="sm:max-w-[600px]"
bodyClassName="space-y-6 py-4"
footer={
<div className="flex items-center justify-end w-full gap-2">
<Button type="button" onClick={handleSubmit} disabled={!selectedFile || submitting}>
확인
</Button>
</div>
}
>
<div>
<div className="flex gap-2">
<Button
type="button"
variant="default"
className="text-lg bg-excel"
onClick={onDownloadTemplate}
>
<Download className="w-4 h-4 mr-2" />
양식 다운로드
</Button>
<Button
type="button"
variant="outline"
className="flex-1 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>
</KriDialog>
)
}
export default UserUploadDialog