diff --git a/src/components/Expenses/CreateExpense.jsx b/src/components/Expenses/CreateExpense.jsx deleted file mode 100644 index cddc79dd..00000000 --- a/src/components/Expenses/CreateExpense.jsx +++ /dev/null @@ -1,477 +0,0 @@ -import { zodResolver } from "@hookform/resolvers/zod"; -import React, { useEffect, useState } from "react"; -import { useForm } from "react-hook-form"; -import { ExpenseSchema } from "./ExpenseSchema"; -import { formatFileSize } from "../../utils/appUtils"; -import { useProjectName } from "../../hooks/useProjects"; -import { useDispatch, useSelector } from "react-redux"; -import { changeMaster } from "../../slices/localVariablesSlice"; -import useMaster, { - useExpenseStatus, - useExpenseType, - usePaymentMode, -} from "../../hooks/masterHook/useMaster"; -import { - useEmployeesAllOrByProjectId, - useEmployeesByProject, -} from "../../hooks/useEmployees"; -import Avatar from "../common/Avatar"; -import { useCreateExpnse } from "../../hooks/useExpense"; - -const CreateExpense = ({closeModal}) => { - const [ExpenseType, setExpenseType] = useState(); - const dispatch = useDispatch(); - const { - ExpenseTypes, - loading: ExpenseLoading, - error: ExpenseError, - } = useExpenseType(); - const schema = ExpenseSchema(ExpenseTypes); - const { - register, - handleSubmit, - watch, - setValue, - reset, - formState: { errors }, - } = useForm({ - resolver: zodResolver(schema), - defaultValues: { - projectId: "", - expensesTypeId: "", - paymentModeId: "", - paidById: "", - transactionDate: "", - transactionId: "", - description: "", - location: "", - supplerName: "", - amount: "", - noOfPersons: "", - billAttachments: [], - }, - }); - - const selectedproject = watch("projectId"); - const selectedProject = useSelector( - (store) => store.localVariables.projectId - ); - const { projectNames, loading: projectLoading, error } = useProjectName(); - - const { - PaymentModes, - loading: PaymentModeLoading, - error: PaymentModeError, - } = usePaymentMode(); - const { - ExpenseStatus, - loading: StatusLoadding, - error: stausError, - } = useExpenseStatus(); - const { - employees, - loading: EmpLoading, - error: EmpError, - } = useEmployeesByProject(selectedproject); - - const files = watch("billAttachments"); - const onFileChange = async (e) => { - const newFiles = Array.from(e.target.files); - if (newFiles.length === 0) return; - - const existingFiles = watch("billAttachments") || []; - - const parsedFiles = await Promise.all( - newFiles.map(async (file) => { - const base64Data = await toBase64(file); - return { - fileName: file.name, - base64Data, - contentType: file.type, - fileSize: file.size, - description: "", - }; - }) - ); - - const combinedFiles = [ - ...existingFiles, - ...parsedFiles.filter( - (newFile) => - !existingFiles.some( - (f) => - f.fileName === newFile.fileName && f.fileSize === newFile.fileSize - ) - ), - ]; - - setValue("billAttachments", combinedFiles, { - shouldDirty: true, - shouldValidate: true, - }); - }; - - const toBase64 = (file) => - new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.readAsDataURL(file); - reader.onload = () => resolve(reader.result.split(",")[1]); // base64 only, no prefix - reader.onerror = (error) => reject(error); - }); - const removeFile = (index) => { - const newFiles = files.filter((_, i) => i !== index); - setValue("billAttachments", newFiles, { shouldValidate: true }); - }; - - const {mutate:CreateExpense,isPending} = useCreateExpnse(()=>{ - handleClose() - }) - const onSubmit = (payload) => { - console.log("Form Data:", payload); - - CreateExpense(payload) - }; - const ExpenseTypeId = watch("expensesTypeId"); - - useEffect(() => { - setExpenseType(ExpenseTypes?.find((type) => type.id === ExpenseTypeId)); - }, [ExpenseTypeId]); - - const handleClose =()=>{ - reset() - closeModal() - } - return ( -