diff --git a/src/components/Expenses/CreateExpense.jsx b/src/components/Expenses/CreateExpense.jsx index 0ed1b119..cddc79dd 100644 --- a/src/components/Expenses/CreateExpense.jsx +++ b/src/components/Expenses/CreateExpense.jsx @@ -254,6 +254,11 @@ const CreateExpense = ({closeModal}) => { )) )} + {errors.paidById && ( + + {errors.paidById.message} + + )} diff --git a/src/components/Expenses/ExpenseList.jsx b/src/components/Expenses/ExpenseList.jsx index 4476dbad..2ac606e5 100644 --- a/src/components/Expenses/ExpenseList.jsx +++ b/src/components/Expenses/ExpenseList.jsx @@ -19,9 +19,10 @@ const ExpenseList = () => { startDate: null, endDate: null, }; + - const { data, isLoading, isError } = useExpenseList(ITEMS_PER_PAGE, currentPage, filter); - if (isLoading) return
Loading...
; + const { data, isLoading, isError,isInitialLoading } = useExpenseList(2, currentPage, filter); + if (isInitialLoading) return
Loading...
; const items = data.data ?? []; const totalPages = data?.totalPages ?? 1; const hasMore = currentPage < totalPages; @@ -37,12 +38,13 @@ const ExpenseList = () => {
@@ -131,7 +133,7 @@ const ExpenseList = () => { )} - {!isLoading && items.length === 0 && ( + {!isInitialLoading && items.length === 0 && ( )} - {!isLoading && + {!isInitialLoading && items.map((expense) => ( - ))}
No expenses found. @@ -139,12 +141,12 @@ const ExpenseList = () => {
- {formatUTCToLocalTime(expense.createdAt)} + {formatUTCToLocalTime(expense.transactionDate)}
@@ -183,22 +185,37 @@ const ExpenseList = () => { {expense.status?.name || "Unknown"} - +
+ setViewExpense({ expenseId: expense, view: true }) } > - + + + + + + + + +
- {!isLoading && items.length > 0 && ( + {!isInitialLoading && items.length > 0 && ( { - -return useQuery({ +export const useExpenseList = (pageSize, pageNumber, filter) => { + return useQuery({ queryKey: ["expenses", pageNumber, pageSize, filter], - queryFn: async() => - await ExpenseRepository.GetExpenseList( pageSize, pageNumber, filter ).then((res) => res.data), + queryFn: async () => + await ExpenseRepository.GetExpenseList(pageSize, pageNumber, filter).then( + (res) => res.data + ), keepPreviousData: true, }); -} - -export const useExpense =(ExpenseId)=>{ - return useQuery({ - queryKey:["Expense",ExpenseId], - queryFn:async()=>await ExpenseRepository.GetExpenseDetails(ExpenseId) - }) -} +}; +export const useExpense = (ExpenseId) => { + return useQuery({ + queryKey: ["Expense", ExpenseId], + queryFn: async () => await ExpenseRepository.GetExpenseDetails(ExpenseId), + }); +}; // ---------------------------Mutation--------------------------------------------- -export const useCreateExpnse =(onSuccessCallBack)=>{ - const queryClient = useQueryClient() - return useMutation({ - mutationFn: async(payload)=>{ - await ExpenseRepository.CreateExpense(payload) - }, - onSuccess:(_,variables)=>{ - showToast("Expense Created Successfully","success") - queryClient.invalidateQueries({queryKey:["expenses"]}) - if (onSuccessCallBack) onSuccessCallBack(); - }, - onError:(error)=>{ - showToast(error.message || "Something went wrong please try again !","error") - } - }) -} +export const useCreateExpnse = (onSuccessCallBack) => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: async (payload) => { + await ExpenseRepository.CreateExpense(payload); + }, + onSuccess: (_, variables) => { + showToast("Expense Created Successfully", "success"); + queryClient.invalidateQueries({ queryKey: ["expenses"] }); + if (onSuccessCallBack) onSuccessCallBack(); + }, + onError: (error) => { + showToast( + error.message || "Something went wrong please try again !", + "error" + ); + }, + }); +}; -export const useActionOnExpense=(onSuccessCallBack)=>{ - const queryClient = useQueryClient() - return useMutation({ - mutationFn: async(payload)=>{ - await ExpenseRepository.ActionOnExpense(payload) +export const useActionOnExpense = (onSuccessCallBack) => { + const queryClient = useQueryClient(); + + return useMutation({ + mutationFn: async (payload) => { + const response = await ExpenseRepository.ActionOnExpense(payload); + return response.data; + }, + onSuccess: (updatedExpense, variables) => { + showToast("Expense updated successfully", "success"); + + queryClient.setQueriesData( + { + queryKey: ["expenses"], + exact: false, }, - onSuccess:(data,variables)=>{ - console.log(data) - showToast("Expense Created Successfully","success") - if (onSuccessCallBack) onSuccessCallBack(); - }, - onError:(error)=>{ - showToast(error.message || "Something went wrong please try again !","error") + (oldData) => { + if (!oldData) return oldData; + return { + ...oldData, + data: oldData.data.map((item) => + item.id === updatedExpense.id + ? { + ...item, + nextStatus: updatedExpense.nextStatus, + status: updatedExpense.status, + } + : item + ), + }; } - }) -} + ); + + if (onSuccessCallBack) onSuccessCallBack(); + }, + onError: (error) => { + showToast( + error.message || "Something went wrong, please try again!", + "error" + ); + }, + }); +};