From e868d27d5ffc9486d056a6657bb639ddecb661eb Mon Sep 17 00:00:00 2001 From: Kartik Sharma Date: Tue, 4 Nov 2025 18:34:00 +0530 Subject: [PATCH] Implementing Get and Update API in Recurring Expense. --- .../ManageRecurringExpense.jsx | 50 +++++++------ ...penseList.jsx => RecurringExpenseList.jsx} | 74 +++++++++++-------- src/hooks/masterHook/useMaster.js | 8 ++ src/hooks/useExpense.js | 37 ++++++++++ .../RecurringExpense/RecurringExpensePage.jsx | 15 ++-- src/repositories/ExpsenseRepository.jsx | 9 ++- src/repositories/MastersRepository.jsx | 2 + 7 files changed, 130 insertions(+), 65 deletions(-) rename src/components/RecurringExpense/{RecurringRexpenseList.jsx => RecurringExpenseList.jsx} (90%) diff --git a/src/components/RecurringExpense/ManageRecurringExpense.jsx b/src/components/RecurringExpense/ManageRecurringExpense.jsx index a9f3c913..4afe79c1 100644 --- a/src/components/RecurringExpense/ManageRecurringExpense.jsx +++ b/src/components/RecurringExpense/ManageRecurringExpense.jsx @@ -1,13 +1,13 @@ import React, { useEffect, useState } from 'react' import Label from '../common/Label'; import { useForm } from 'react-hook-form'; -import { useExpenseCategory } from '../../hooks/masterHook/useMaster'; +import { useExpenseCategory, useRecurringStatus } from '../../hooks/masterHook/useMaster'; import DatePicker from '../common/DatePicker'; import { zodResolver } from '@hookform/resolvers/zod'; import { defaultRecurringExpense, PaymentRecurringExpense } from './RecurringExpenseSchema'; import { INR_CURRENCY_CODE } from '../../utils/constants'; import { useCurrencies, useProjectName } from '../../hooks/useProjects'; -import { useCreateRecurringExpense } from '../../hooks/useExpense'; +import { useCreateRecurringExpense, useUpdateRecurringExpense } from '../../hooks/useExpense'; function ManageRecurringExpense({ closeModal, requestToEdit = null }) { @@ -17,7 +17,9 @@ function ManageRecurringExpense({ closeModal, requestToEdit = null }) { const { data: currencyData, isLoading: currencyLoading, isError: currencyError } = useCurrencies(); + const { data: statusData, isLoading: statusLoading, isError: statusError } = useRecurringStatus(); + console.log("Tanish", statusData) const { ExpenseCategories, loading: ExpenseLoading, @@ -40,9 +42,9 @@ function ManageRecurringExpense({ closeModal, requestToEdit = null }) { handleClose(); } ); - // const { mutate: PaymentRequestUpdate, isPending } = useUpdatePaymentRequest(() => - // handleClose() - // ); + const { mutate: RecurringExpenseUpdate, isPending } = useUpdateRecurringExpense(() => + handleClose() + ); useEffect(() => { if (requestToEdit && data) { @@ -66,22 +68,30 @@ function ManageRecurringExpense({ closeModal, requestToEdit = null }) { } }, [data, reset]); - // console.log("Veer",data) + + useEffect(() => { + if (!requestToEdit && currencyData && currencyData.length > 0) { + const inrCurrency = currencyData.find( + (c) => c.id === INR_CURRENCY_CODE + ); + if (inrCurrency) { + setValue("currencyId", INR_CURRENCY_CODE, { shouldValidate: true }); + } + } + }, [currencyData, requestToEdit, setValue]); const onSubmit = (fromdata) => { let payload = { ...fromdata, - // strikeDate: localToUtc(fromdata.strikeDate), strikeDate: fromdata.strikeDate ? new Date(fromdata.strikeDate).toISOString() : null, }; if (requestToEdit) { - const editPayload = { ...payload, id: data.id}; - PaymentRequestUpdate({ id: data.id, payload: editPayload }); + const editPayload = { ...payload, id: data.id }; + RecurringExpenseUpdate({ id: data.id, payload: editPayload }); } else { CreateRecurringExpense(payload); } - console.log("Kartik", payload) }; return ( @@ -294,10 +304,8 @@ function ManageRecurringExpense({ closeModal, requestToEdit = null }) { {errors.notifyTo.message} )} - -
diff --git a/src/hooks/masterHook/useMaster.js b/src/hooks/masterHook/useMaster.js index 991719e9..a64dd7d8 100644 --- a/src/hooks/masterHook/useMaster.js +++ b/src/hooks/masterHook/useMaster.js @@ -57,6 +57,14 @@ export const useMasterMenu = () => { }); }; +export const useRecurringStatus = () => { + return useQuery({ + queryKey: ["recurringExpense"], + queryFn: async () => + await MasterRespository.getRecurringStatus(), + }); +}; + export const useActivitiesMaster = () => { const { data: activities = [], diff --git a/src/hooks/useExpense.js b/src/hooks/useExpense.js index 4f38de56..fa6f4418 100644 --- a/src/hooks/useExpense.js +++ b/src/hooks/useExpense.js @@ -407,4 +407,41 @@ export const useCreateRecurringExpense = (onSuccessCallBack) => { ); }, }); +}; + +export const useUpdateRecurringExpense = (onSuccessCallBack) => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: async ({ id, payload }) => { + const response = await ExpenseRepository.UpdateRecurringExpense(id, payload); + return response.data; + }, + onSuccess: (updatedExpense, variables) => { + queryClient.removeQueries({ queryKey: ["RecurringExpense", variables.id] }); + queryClient.invalidateQueries({ queryKey: ["RecurringExpenseList"] }); + showToast("Recurring Expense updated Successfully", "success"); + + if (onSuccessCallBack) onSuccessCallBack(); + }, + onError: (error) => { + showToast("Something went wrong.Please try again later.", "error"); + }, + }); +}; + +export const useRecurringExpenseList = ( + pageSize, + pageNumber, + filter, + isActive, + searchString = "", +) => { + return useQuery({ + queryKey: ["paymentRequestList",pageSize,pageNumber,filter,isActive,searchString], + queryFn: async()=>{ + const resp = await ExpenseRepository.GetRecurringExpenseList(pageSize,pageNumber,filter,isActive,searchString); + return resp.data; + }, + keepPreviousData: true, + }); }; \ No newline at end of file diff --git a/src/pages/RecurringExpense/RecurringExpensePage.jsx b/src/pages/RecurringExpense/RecurringExpensePage.jsx index 60da6be8..e541a376 100644 --- a/src/pages/RecurringExpense/RecurringExpensePage.jsx +++ b/src/pages/RecurringExpense/RecurringExpensePage.jsx @@ -2,9 +2,8 @@ import React, { createContext, useState, useEffect, useContext } from "react"; import Breadcrumb from "../../components/common/Breadcrumb"; import GlobalModel from "../../components/common/GlobalModel"; import { useFab } from "../../Context/FabContext"; -// import { defaultPaymentRequestFilter,SearchPaymentRequestSchema } from "../../components/PaymentRequest/PaymentRequestSchema"; import ManageRecurringExpense from "../../components/RecurringExpense/ManageRecurringExpense"; -import RecurringExpenseList from "../../components/RecurringExpense/RecurringRexpenseList"; +import RecurringExpenseList from "../../components/RecurringExpense/RecurringExpenseList"; export const RecurringExpenseContext = createContext(); export const useRecurringExpenseContext = () => { @@ -17,7 +16,7 @@ export const useRecurringExpenseContext = () => { const RecurringExpensePage = () => { const [ManageRequest, setManageRequest] = useState({ IsOpen: null, - RequestId: null, + RecurringId: null, }); const [ViewRequest, setVieRequest] = useState({ view: false, requestId: null }) const { setOffcanvasContent, setShowTrigger } = useFab(); @@ -49,7 +48,7 @@ const RecurringExpensePage = () => {
{/* Breadcrumb */} { search={search} filters={filters} /> */} - + {/* Add/Edit Modal */} {ManageRequest.IsOpen && ( @@ -105,10 +104,10 @@ const RecurringExpensePage = () => { } > - setManageRequest({ IsOpen: null, RequestId: null }) + setManageRequest({ IsOpen: null, RecurringId: null }) } /> diff --git a/src/repositories/ExpsenseRepository.jsx b/src/repositories/ExpsenseRepository.jsx index 2bcc5596..b7ede978 100644 --- a/src/repositories/ExpsenseRepository.jsx +++ b/src/repositories/ExpsenseRepository.jsx @@ -29,9 +29,14 @@ const ExpenseRepository = { //#endregion //#region Recurring Expense - + + GetRecurringExpenseList: (pageSize, pageNumber, filter, isActive, searchString) => { + const payloadJsonString = JSON.stringify(filter); + return api.get(`/api/Expense/get/recurring-payment/list?isActive=${isActive}&pageSize=${pageSize}&pageNumber=${pageNumber}&filter=${payloadJsonString}&searchString=${searchString}`); + }, CreateRecurringExpense: (data) => api.post("/api/Expense/recurring-payment/create", data), - + UpdateRecurringExpense: (id, data) => api.put(`/api/Expense/recurring-payment/edit/${id}`, data), + //#endregion diff --git a/src/repositories/MastersRepository.jsx b/src/repositories/MastersRepository.jsx index 792cb376..7413e5f5 100644 --- a/src/repositories/MastersRepository.jsx +++ b/src/repositories/MastersRepository.jsx @@ -141,4 +141,6 @@ export const MasterRespository = { api.post(`/api/Master/payment-adjustment-head`, data), updatePaymentAjustmentHead: (id, data) => api.put(`/api/Master/payment-adjustment-head/edit/${id}`, data), + + getRecurringStatus: () => api.get(`/api/master/recurring-status/list`), };