106 lines
3.2 KiB
TypeScript
106 lines
3.2 KiB
TypeScript
"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,
|
|
notice,
|
|
onDownloadTemplate,
|
|
onUpload,
|
|
isUploading,
|
|
}: Readonly<{
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
title: string;
|
|
notice?: 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">
|
|
{notice && <p className="text-xs text-red-500">* {notice}</p>}
|
|
<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>
|
|
);
|
|
}
|