Files
frontend/apps/wr/components/ui/modal/chkgdExcel/chkgdExcel.tsx
T

127 lines
3.8 KiB
TypeScript

import { useWrStore } from "@/app/store";
import { rainChkZod, rainRegExcel, type rainChkZodType } from "@/app/store/prfmncReg/chkgdnReg/rainChkgdn";
import { excelToRainChkData } from "@/app/store/prfmncReg/chkgdnReg/rainChkgdn/excel";
import { selectClass } from "@/app/store/ruseFcltMng/fcltReg/rainReg";
import {
DatePicker,
filePicked,
FileUpload,
Form,
FormField,
FormUi,
Title,
} from "@common/business_components/ui";
import { Button, Input } from "@common/components/ui";
import { zodResolver } from "@hookform/resolvers/zod";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import toast, { Toaster } from "react-hot-toast";
import type { z } from "zod";
import { ToastFormUi } from "../../toast/toastFromUi";
export default function ChkgdExcel({ type }: { type: string }) {
const { rainChkExcelDataUPload } = useWrStore((state) => {
return state;
});
const [files, setFiles] = useState<File[] | null>(null);
const [excelData , setExcelData] = useState<rainChkZodType[]>()
const defaultValues: z.infer<typeof rainRegExcel> = rainRegExcel.parse({});
const form = useForm<z.infer<typeof rainRegExcel>>({
resolver: zodResolver(rainRegExcel),
defaultValues: defaultValues,
});
useEffect(() => {
if (files) {
const file = files[0];
if(form.getValues("chckYr").length<1 ){
ToastFormUi({body:"파일업로드 이전에 등록년도를 작성해주세요 " ,title:'지도점검 등록'})
setFiles(null)
return
}
if (file)
filePicked(file, 0).then((res) => {
if (res !== undefined)
setExcelData(excelToRainChkData(res, form.getValues("chckYr")))
});
}
}, [files]);
const onSubmit =(values: z.infer<typeof rainRegExcel>)=> {
if(values.chckYr ===''){
ToastFormUi({body:"등록년도를 작성해주세요" ,title:'지도점검 등록'})
return
}else if(excelData ===undefined || excelData?.length === 0 ){
ToastFormUi({body:"빈 파일 및 파일 오류 입니다." ,title:'지도점검 등록'})
return
}
if (excelData) {
rainChkExcelDataUPload(excelData);
}
}
return (
<>
<div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<div>
<Title
title={
type === "RNB"
? "빗물 이용시설"
: "중수도 이용시설" + "점검 결과등록"
}
></Title>
</div>
<div className="flex justify-between">
<h3 className="text-2xl font-bold text-gray-900">
{"○점검정보 입력"}
</h3>
<Button className="" type="submit">
저장
</Button>
</div>
<div>
<FormField
control={form.control}
name="chckYr"
render={({ field }) => (
<FormUi label="등록년도">
<Input
className={`text-3xl`}
type="text"
value={field.value}
onChange={field.onChange}
/>
</FormUi>
)}
/>
<FormField
control={form.control}
name="file"
render={({ field }) => (
<FormUi label="점검결과 파일">
<FileUpload
files={files || []}
setFiles={(files: File[] | null ) => {
if(files) setFiles(files)
} }
fileTypes="xlsx"
></FileUpload>
</FormUi>
)}
/>
</div>
</form>
</Form>
</div>
</>
);
}