69 lines
2.1 KiB
JavaScript
69 lines
2.1 KiB
JavaScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
|
|
import ExpenseRepository from "../repositories/ExpsenseRepository"
|
|
import showToast from "../services/toastService"
|
|
import { queryClient } from "../layouts/AuthLayout";
|
|
|
|
|
|
|
|
// -------------------Query------------------------------------------------------
|
|
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),
|
|
keepPreviousData: true,
|
|
});
|
|
}
|
|
|
|
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 !","success")
|
|
}
|
|
})
|
|
}
|
|
const demoExpense = {
|
|
projectId: "proj_123",
|
|
expensesTypeId: "1", // corresponds to Travel
|
|
paymentModeId: "pm_456",
|
|
paidById: "emp_789",
|
|
transactionDate: "2025-07-21",
|
|
transactionId: "TXN-001234",
|
|
description: "Taxi fare from airport to hotel",
|
|
location: "New York",
|
|
supplerName: "City Taxi Service",
|
|
amount: 45.50,
|
|
noOfPersons: 2,
|
|
billAttachments: [
|
|
{
|
|
fileName: "receipt.pdf",
|
|
base64Data: "JVBERi0xLjQKJcfs...", // truncated base64 example string
|
|
contentType: "application/pdf",
|
|
fileSize: 450000, // less than 5MB
|
|
description: "Taxi receipt",
|
|
},
|
|
],
|
|
};
|