From 9367c6de283785017d0b1f3643d3098655263508 Mon Sep 17 00:00:00 2001 From: pramod mahajan Date: Tue, 22 Jul 2025 18:53:17 +0530 Subject: [PATCH] initiated forn edit and created with one component --- src/components/Expenses/CreateExpense.jsx | 477 ---------------------- src/components/Expenses/ExpenseSchema.js | 15 + 2 files changed, 15 insertions(+), 477 deletions(-) delete mode 100644 src/components/Expenses/CreateExpense.jsx 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 ( -
-
Create New Expense
-
-
-
- - - {errors.projectId && ( - {errors.projectId.message} - )} -
- -
- - - {errors.expensesTypeId && ( - - {errors.expensesTypeId.message} - - )} -
-
- -
-
- - - {errors.paymentModeId && ( - - {errors.paymentModeId.message} - - )} -
- -
- - - {errors.paidById && ( - - {errors.paidById.message} - - )} -
-
- -
-
- - - {errors.transactionDate && ( - - {errors.transactionDate.message} - - )} -
- -
- - - {errors.amount && ( - {errors.amount.message} - )} -
-
- -
-
- - - {errors.supplerName && ( - - {errors.supplerName.message} - - )} -
- -
- - - {errors.location && ( - {errors.location.message} - )} -
-
-
-
- - - {errors.transactionId && ( - - {errors.transactionId.message} - - )} -
- - {ExpenseType?.noOfPersonsRequired && ( -
- - - {errors.noOfPersons && ( - - {errors.noOfPersons.message} - - )} -
- )} -
- -
-
- - - {errors.description && ( - - {errors.description.message} - - )} -
-
- -
-
- - -
document.getElementById("billAttachments").click()} - > - - - Click to select or click here to browse - - (PDF, JPG, PNG, max 5MB) - - { - onFileChange(e); - e.target.value = ""; - }} - /> -
- {errors.billAttachments && ( - - {errors.billAttachments.message} - - )} - - {files.length > 0 && ( - - )} - {Array.isArray(errors.billAttachments) && - errors.billAttachments.map((fileError, index) => ( -
- {fileError?.fileSize?.message || - fileError?.contentType?.message} -
- ))} -
-
- -
- {" "} - - -
-
-
- ); -}; - -export default CreateExpense; - diff --git a/src/components/Expenses/ExpenseSchema.js b/src/components/Expenses/ExpenseSchema.js index 99b75633..a2e11ed7 100644 --- a/src/components/Expenses/ExpenseSchema.js +++ b/src/components/Expenses/ExpenseSchema.js @@ -68,6 +68,21 @@ export const ExpenseSchema = (expenseTypes) => { }); }; +export const defaultExpense = { + projectId: "", + expensesTypeId: "", + paymentModeId: "", + paidById: "", + transactionDate: "", + transactionId: "", + description: "", + location: "", + supplerName: "", + amount: "", + noOfPersons: "", + billAttachments: [], + } + export const ActionSchema = z.object({ comment : z.string().min(1,{message:"Please leave comment"}), selectedStatus: z.string().min(1, { message: "Please select a status" }),