wr: 하수재이용실적 등록1
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import { excelToPbnPrfData } from "@/app/store/prfmncReg/pbnstPrfmncReg/excel";
|
||||
import { pbnPrfRegExcelZod } from "@/app/store/prfmncReg/pbnstPrfmncReg/headList";
|
||||
import type {
|
||||
FcPrfType,
|
||||
PbnstPrfRegDtlType,
|
||||
PrfType,
|
||||
} from "@/app/store/prfmncReg/pbnstPrfmncReg/type";
|
||||
import { yearOptions } from "@/components/lib/func/common";
|
||||
@@ -18,7 +18,7 @@ import {
|
||||
} from "@common/business_components/ui";
|
||||
import { Button, Input } from "@common/components/ui";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
// import toast from "react-hot-toast";
|
||||
import type { z } from "zod";
|
||||
@@ -29,13 +29,14 @@ export default function PbnPrfExcelModal({
|
||||
setIsExcelOPen,
|
||||
setModalExcelData,
|
||||
}: {
|
||||
data: FcPrfType;
|
||||
data: PbnstPrfRegDtlType;
|
||||
setIsExcelOPen: (open: boolean) => void;
|
||||
setModalExcelData: (excelData: PrfType) => void;
|
||||
setModalExcelData: (excelData: PrfType[]) => void;
|
||||
}) {
|
||||
const alert = useAlertStore((state) => state.setMessage);
|
||||
const [file, setFile] = useState<File | null>(null);
|
||||
const [excelData, setExcelData] = useState<PrfType[]>();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const form = useForm<z.infer<typeof pbnPrfRegExcelZod>>({
|
||||
resolver: zodResolver(pbnPrfRegExcelZod),
|
||||
defaultValues: {
|
||||
@@ -44,26 +45,32 @@ export default function PbnPrfExcelModal({
|
||||
});
|
||||
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
setExcelData(undefined);
|
||||
const selectedFile = e.target.files?.[0];
|
||||
if (selectedFile) {
|
||||
setFile(selectedFile);
|
||||
}
|
||||
};
|
||||
const onSubmit = (values: z.infer<typeof pbnPrfRegExcelZod>) => {
|
||||
if (excelData) {
|
||||
////TODO: 궅이 서브밋을 써야하는 이유가 있는지 ?
|
||||
console.log("values ", values);
|
||||
// prfmnRegData(1,'41', excelData as unknown as PrfType); 활성화시 스토어 활성화 칠요
|
||||
|
||||
setModalExcelData(excelData as unknown as PrfType);
|
||||
setIsExcelOPen(false);
|
||||
if (!file) {
|
||||
alert("엑셀 파일을 선택해 주세요.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!excelData || excelData.length === 0) {
|
||||
alert("엑셀 파일을 먼저 확인해 주세요.");
|
||||
return;
|
||||
}
|
||||
|
||||
////TODO: 궅이 서브밋을 써야하는 이유가 있는지 ?
|
||||
console.log("values ", values);
|
||||
setModalExcelData(excelData);
|
||||
setIsExcelOPen(false);
|
||||
};
|
||||
useEffect(() => {
|
||||
if (data && data.bscFctNm)
|
||||
form.setValue("pbnstRuseFcltId", data.pbnstRuseFcltId);
|
||||
form.setValue("bscFctNm", data.bscFctNm || "");
|
||||
form.setValue("pbnstRuseFcltNm", data.ruseUtztnFcltNm);
|
||||
form.setValue("pbnstRuseFcltId", data.pbnstRuseFcltId || "");
|
||||
form.setValue("bscFctNm", data.swageBscFctNm || "");
|
||||
form.setValue("pbnstRuseFcltNm", data.ruseUtztnFcltNm || "");
|
||||
}, [data]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -76,20 +83,42 @@ export default function PbnPrfExcelModal({
|
||||
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)
|
||||
setExcelData(
|
||||
excelToPbnPrfData(
|
||||
res as unknown as JSON[],
|
||||
form.getValues("ym"),
|
||||
form.getValues("pbnstRuseFcltId")
|
||||
) as PrfType[]
|
||||
);
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [file]);
|
||||
}, [alert, file, form]);
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -177,10 +206,11 @@ export default function PbnPrfExcelModal({
|
||||
|
||||
<div className="space-y-4 p-4">
|
||||
<Input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
onChange={(file) => {
|
||||
onChange={(event) => {
|
||||
console.log(" field. valus", field.value);
|
||||
if (file) handleFileChange(file);
|
||||
handleFileChange(event);
|
||||
}}
|
||||
accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user