260617_01_ENH: 배정 테스트를 위해 개발 중도 커밋
This commit is contained in:
102
src/components/dialog/ExcelRegisterDialog.tsx
Normal file
102
src/components/dialog/ExcelRegisterDialog.tsx
Normal file
@@ -0,0 +1,102 @@
|
||||
"use client";
|
||||
|
||||
import {useEffect, useState} from "react";
|
||||
import {Download, FileUp} from "lucide-react";
|
||||
import {Button} from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import {ExcelUploadResult} from "@/types/common";
|
||||
import {downloadBase64File} from "@/lib/utils";
|
||||
|
||||
export function ExcelRegisterDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
title,
|
||||
onDownloadTemplate,
|
||||
onUpload,
|
||||
isUploading,
|
||||
}: Readonly<{
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
title: string;
|
||||
onDownloadTemplate: () => void;
|
||||
onUpload: (file: File) => Promise<ExcelUploadResult>;
|
||||
isUploading?: boolean;
|
||||
}>) {
|
||||
const [file, setFile] = useState<File | null>(null);
|
||||
const [uploadResult, setUploadResult] = useState<ExcelUploadResult | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
setFile(null);
|
||||
setUploadResult(null);
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const upload = async () => {
|
||||
if (!file) return;
|
||||
const result = await onUpload(file);
|
||||
setUploadResult(result);
|
||||
};
|
||||
|
||||
const downloadErrorFile = () => {
|
||||
if (!uploadResult?.errorFileBase64) return;
|
||||
downloadBase64File(uploadResult.errorFileBase64, uploadResult.errorFileName || "excel_upload_error.xlsx");
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{title}</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="space-y-3">
|
||||
<Button type="button" variant="outline" className="w-full" onClick={onDownloadTemplate}>
|
||||
<Download className="h-4 w-4" />
|
||||
양식 다운로드
|
||||
</Button>
|
||||
<label className="flex h-11 cursor-pointer items-center justify-center gap-2 rounded-md border bg-muted text-sm font-medium">
|
||||
<FileUp className="h-4 w-4" />
|
||||
{file ? file.name : "파일 업로드"}
|
||||
<input
|
||||
type="file"
|
||||
accept=".xlsx,.xls"
|
||||
className="hidden"
|
||||
onChange={(event) => {
|
||||
setFile(event.target.files?.[0] ?? null);
|
||||
setUploadResult(null);
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
{uploadResult && (
|
||||
<div className="rounded-md border bg-muted/40 p-3 text-sm">
|
||||
<div>전체: {uploadResult.totalCount}</div>
|
||||
<div>성공: {uploadResult.successCount}</div>
|
||||
<div>실패: {uploadResult.failCount}</div>
|
||||
{uploadResult.hasErrorFile && (
|
||||
<Button type="button" variant="outline" size="sm" className="mt-2 w-full" onClick={downloadErrorFile}>
|
||||
<Download className="h-4 w-4" />
|
||||
오류 파일 다운로드
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={!file || isUploading}
|
||||
onClick={upload}
|
||||
>
|
||||
등록
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user