/** * 재이용실적등록> 하수재이용 실적등록 파일등록 modal */ import { excelToPbnPrfData } from "@/app/store/prfmncReg/pbnstPrfmncReg/excel"; import { pbnPrfRegExcelZod } from "@/app/store/prfmncReg/pbnstPrfmncReg/headList"; import type { PbnstPrfRegDtlType, PrfType, } from "@/app/store/prfmncReg/pbnstPrfmncReg/type"; import { yearOptions } from "@/components/lib/func/common"; import { FormField, FormUi, SelectBox, Form, Title, filePicked, } from "@common/business_components/ui"; import { Button, Input } from "@common/components/ui"; import { zodResolver } from "@hookform/resolvers/zod"; import { useEffect, useRef, useState } from "react"; import { useForm } from "react-hook-form"; // import toast from "react-hot-toast"; import type { z } from "zod"; import { useAlertStore } from "@/src/shared/lib"; export default function PbnPrfExcelModal({ data, setIsExcelOPen, setModalExcelData, }: { data: PbnstPrfRegDtlType; setIsExcelOPen: (open: boolean) => void; setModalExcelData: (excelData: PrfType[]) => void; }) { const alert = useAlertStore((state) => state.setMessage); const [file, setFile] = useState(null); const [excelData, setExcelData] = useState(); const fileInputRef = useRef(null); const form = useForm>({ resolver: zodResolver(pbnPrfRegExcelZod), defaultValues: { ym: String(new Date().getFullYear()), }, }); const handleFileChange = (e: React.ChangeEvent) => { setExcelData(undefined); const selectedFile = e.target.files?.[0]; if (selectedFile) { setFile(selectedFile); } }; const onSubmit = (values: z.infer) => { if (!file) { alert("엑셀 파일을 선택해 주세요."); return; } if (!excelData || excelData.length === 0) { alert("엑셀 파일을 먼저 확인해 주세요."); return; } ////TODO: 궅이 서브밋을 써야하는 이유가 있는지 ? console.log("values ", values); setModalExcelData(excelData); setIsExcelOPen(false); }; useEffect(() => { form.setValue("pbnstRuseFcltId", data.pbnstRuseFcltId || ""); form.setValue("bscFctNm", data.swageBscFctNm || ""); form.setValue("pbnstRuseFcltNm", data.ruseUtztnFcltNm || ""); }, [data]); useEffect(() => { if (file) { const isFile = file; if (form.getValues("ym").length < 1) { // toast.error("파일업로드 이전에 등록년도를 작성해주세요 "); alert("파일업로드 이전에 등록년도를 작성해주세요 "); setFile(null); return; } if (!form.getValues("pbnstRuseFcltId")) { alert("재이용시설을 다시 선택한 뒤 파일등록을 진행해 주세요."); setFile(null); if (fileInputRef.current) { fileInputRef.current.value = ""; } return; } if (isFile) { filePicked(isFile, 0).then((res) => { if (res === undefined) { alert("엑셀 파일을 읽지 못했습니다."); return; } const parsedData = excelToPbnPrfData( res as unknown as JSON[], form.getValues("ym"), form.getValues("pbnstRuseFcltId") ) as PrfType[]; if (!parsedData.length) { alert("업로드 가능한 실적 데이터가 없습니다. 양식을 확인해 주세요."); if (fileInputRef.current) { fileInputRef.current.value = ""; } setFile(null); return; } setExcelData(parsedData); }); } } }, [alert, file, form]); return ( <>

{"점검정보 입력"}

( field.onChange(val)} /> )} />
( )} />
( )} />
( {/* { if (files) setFiles(files); }} fileTypes=".xlsx" maxFiles={1} > */}
{ console.log(" field. valus", field.value); handleFileChange(event); }} accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
)} />
); }