summaryrefslogtreecommitdiff
path: root/lib/swp/table/swp-table-toolbar.tsx
diff options
context:
space:
mode:
authorjoonhoekim <26rote@gmail.com>2025-10-24 19:44:04 +0900
committerjoonhoekim <26rote@gmail.com>2025-10-24 19:44:04 +0900
commit231c4eb86771a44b24248ca403fcbb8c44fff74b (patch)
tree90725c5c216058223bf2ccd9a9d710a8003a037e /lib/swp/table/swp-table-toolbar.tsx
parent39fc95095ac4b99186294f21fe6d8ac0cfab1f6e (diff)
(김준회) SWP 파일 업로드 처리, 다운로드는 임시 처리(네트워크경로에서 다운로드받도록)
Diffstat (limited to 'lib/swp/table/swp-table-toolbar.tsx')
-rw-r--r--lib/swp/table/swp-table-toolbar.tsx139
1 files changed, 127 insertions, 12 deletions
diff --git a/lib/swp/table/swp-table-toolbar.tsx b/lib/swp/table/swp-table-toolbar.tsx
index 6858f42e..03082b26 100644
--- a/lib/swp/table/swp-table-toolbar.tsx
+++ b/lib/swp/table/swp-table-toolbar.tsx
@@ -16,11 +16,13 @@ import {
PopoverTrigger,
} from "@/components/ui/popover";
import { Label } from "@/components/ui/label";
-import { RefreshCw, Download, Search, X, Check, ChevronsUpDown } from "lucide-react";
-import { syncSwpProjectAction, type SwpTableFilters } from "../actions";
+import { RefreshCw, Search, X, Check, ChevronsUpDown, Upload } from "lucide-react";
+import { syncSwpProjectAction, uploadSwpFilesAction, type SwpTableFilters } from "../actions";
import { useToast } from "@/hooks/use-toast";
import { useRouter } from "next/navigation";
import { cn } from "@/lib/utils";
+import { useRef } from "react";
+import { SwpUploadHelpDialog } from "./swp-help-dialog";
interface SwpTableToolbarProps {
filters: SwpTableFilters;
@@ -34,11 +36,13 @@ export function SwpTableToolbar({
projects = [],
}: SwpTableToolbarProps) {
const [isSyncing, startSync] = useTransition();
+ const [isUploading, startUpload] = useTransition();
const [localFilters, setLocalFilters] = useState<SwpTableFilters>(filters);
const { toast } = useToast();
const router = useRouter();
const [projectSearchOpen, setProjectSearchOpen] = useState(false);
const [projectSearch, setProjectSearch] = useState("");
+ const fileInputRef = useRef<HTMLInputElement>(null);
// 동기화 핸들러
const handleSync = () => {
@@ -86,17 +90,112 @@ export function SwpTableToolbar({
/**
* 파일 업로드 핸들러
- * 1) 네트워크 드라이브에 정해진 규칙대로, 파일이름 기반으로 파일 업로드하기 (단, cpyCd는 어떻게 해결할지 고민해봐야 함...)
+ * 1) 네트워크 드라이브에 정해진 규칙대로, 파일이름 기반으로 파일 업로드하기
* 2) 1~N개 파일 받아서, 파일 이름 기준으로 파싱해서 SaveInBoxList API를 통해 업로드 처리
- *
- * 개발중인 동안은 토스트 반환하도록 처리
*/
const handleUploadFiles = () => {
- toast({
- title: "파일 업로드",
- description: "현재 개발중입니다.",
+ // 프로젝트와 벤더 코드 체크
+ const projectNo = localFilters.projNo;
+ const vndrCd = localFilters.vndrCd;
+
+ if (!projectNo) {
+ toast({
+ variant: "destructive",
+ title: "프로젝트 선택 필요",
+ description: "파일을 업로드할 프로젝트를 먼저 선택해주세요.",
+ });
+ return;
+ }
+
+ if (!vndrCd) {
+ toast({
+ variant: "destructive",
+ title: "업체 코드 입력 필요",
+ description: "파일을 업로드할 업체 코드를 입력해주세요.",
+ });
+ return;
+ }
+
+ // 파일 선택 다이얼로그 열기
+ fileInputRef.current?.click();
+ };
+
+ /**
+ * 파일 선택 핸들러
+ */
+ const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
+ const selectedFiles = event.target.files;
+ if (!selectedFiles || selectedFiles.length === 0) {
+ return;
+ }
+
+ const projectNo = localFilters.projNo!;
+ const vndrCd = localFilters.vndrCd!;
+
+ startUpload(async () => {
+ try {
+ toast({
+ title: "파일 업로드 시작",
+ description: `${selectedFiles.length}개 파일을 업로드합니다...`,
+ });
+
+ // 파일을 Buffer로 변환
+ const fileInfos = await Promise.all(
+ Array.from(selectedFiles).map(async (file) => {
+ const arrayBuffer = await file.arrayBuffer();
+ return {
+ fileName: file.name,
+ fileBuffer: Buffer.from(arrayBuffer),
+ };
+ })
+ );
+
+ // 서버 액션 호출
+ const result = await uploadSwpFilesAction(projectNo, vndrCd, fileInfos);
+
+ if (result.success) {
+ toast({
+ title: "업로드 완료",
+ description: result.message,
+ });
+
+ // 페이지 새로고침
+ router.refresh();
+ } else {
+ toast({
+ variant: "destructive",
+ title: "업로드 실패",
+ description: result.message,
+ });
+ }
+
+ // 실패한 파일이 있으면 상세 정보 표시
+ const failedFiles = result.details.filter((d) => !d.success);
+ if (failedFiles.length > 0) {
+ console.error("실패한 파일:", failedFiles);
+ failedFiles.forEach((f) => {
+ toast({
+ variant: "destructive",
+ title: `${f.fileName} 업로드 실패`,
+ description: f.error || "알 수 없는 오류",
+ });
+ });
+ }
+ } catch (error) {
+ console.error("파일 업로드 실패:", error);
+ toast({
+ variant: "destructive",
+ title: "업로드 실패",
+ description: error instanceof Error ? error.message : "알 수 없는 오류",
+ });
+ } finally {
+ // 파일 입력 초기화
+ if (fileInputRef.current) {
+ fileInputRef.current.value = "";
+ }
+ }
});
- }
+ };
// 검색 적용
const handleSearch = () => {
@@ -141,10 +240,26 @@ export function SwpTableToolbar({
<div className="text-sm text-muted-foreground">
SWP 문서 관리 시스템
</div>
- <div>
- <Button variant="outline" size="sm" onClick={handleUploadFiles}>
- 파일 업로드하기
+ <div className="flex items-center gap-2">
+ <input
+ ref={fileInputRef}
+ type="file"
+ multiple
+ className="hidden"
+ onChange={handleFileChange}
+ accept="*/*"
+ />
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handleUploadFiles}
+ disabled={isUploading || !localFilters.projNo || !localFilters.vndrCd}
+ >
+ <Upload className={`h-4 w-4 mr-2 ${isUploading ? "animate-pulse" : ""}`} />
+ {isUploading ? "업로드 중..." : "파일 업로드"}
</Button>
+
+ <SwpUploadHelpDialog />
</div>
</div>