135 lines
3.9 KiB
JavaScript
135 lines
3.9 KiB
JavaScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { TenantRepository } from "../repositories/TenantRepository";
|
|
import { MarketRepository } from "../repositories/MarketRepository";
|
|
import showToast from "../services/toastService";
|
|
import { useDispatch } from "react-redux";
|
|
import { setCurrentTenant } from "../slices/globalVariablesSlice";
|
|
import { ITEMS_PER_PAGE } from "../utils/constants";
|
|
import moment from "moment";
|
|
|
|
|
|
|
|
const cleanFilter = (filter) => {
|
|
const cleaned = { ...filter };
|
|
|
|
["industryIds", "references"].forEach((key) => {
|
|
if (Array.isArray(cleaned[key]) && cleaned[key].length === 0) {
|
|
delete cleaned[key];
|
|
}
|
|
});
|
|
|
|
// moment.utc() to get consistent UTC ISO strings
|
|
if (!cleaned.startDate) {
|
|
cleaned.startDate = moment.utc().subtract(7, "days").startOf("day").toISOString();
|
|
}
|
|
|
|
if (!cleaned.endDate) {
|
|
cleaned.endDate = moment.utc().startOf("day").toISOString();
|
|
}
|
|
return cleaned;
|
|
};
|
|
|
|
|
|
export const useTenants = (pageNumber, filter, searchString = "") => {
|
|
return useQuery({
|
|
queryKey: ["Tenants", pageNumber, filter, searchString],
|
|
queryFn: async () => {
|
|
const cleanedFilter = cleanFilter(filter);
|
|
const response = await TenantRepository.getTenantList(
|
|
ITEMS_PER_PAGE,
|
|
pageNumber,
|
|
cleanedFilter,
|
|
searchString
|
|
);
|
|
return response.data;
|
|
},
|
|
keepPreviousData: true,
|
|
});
|
|
};
|
|
|
|
export const useTenantDetails =(id)=>{
|
|
return useQuery({
|
|
queryKey:["Tenant",id],
|
|
queryFn:async()=>{
|
|
const response = await TenantRepository.getTenantDetails(id);
|
|
return response.data;
|
|
},
|
|
})
|
|
}
|
|
|
|
|
|
export const useIndustries=()=>{
|
|
return useQuery({
|
|
queryKey:['Industries'],
|
|
queryFn: async()=>{
|
|
const res = await MarketRepository.getIndustries();
|
|
return res.data;
|
|
}
|
|
})
|
|
}
|
|
|
|
export const useSubscriptionPlan=(freq)=>{
|
|
return useQuery({
|
|
queryKey:['SubscriptionPlan',freq],
|
|
queryFn:async()=>{
|
|
console.log("call")
|
|
const res = await TenantRepository.getSubscriptionPlan(freq);
|
|
return res.data;
|
|
}
|
|
})
|
|
}
|
|
|
|
// ------------Mutation---------------------
|
|
|
|
export const useCreateTenant = (onSuccessCallback)=>{
|
|
const dispatch = useDispatch()
|
|
return useMutation({
|
|
mutationFn:async(tenantPayload)=>{
|
|
const res = await TenantRepository.createTenant(tenantPayload);
|
|
return res.data;
|
|
},
|
|
onSuccess:(data,variables)=>{
|
|
showToast("Tenant Created SuccessFully","success")
|
|
dispatch(setCurrentTenant({operationMode:0,data:data}))
|
|
if(onSuccessCallback) onSuccessCallback()
|
|
},
|
|
onError:(error)=>{
|
|
showToast(error.response.message || error?.response?.data?.errors || `Something went wrong`,"error")
|
|
}
|
|
|
|
})
|
|
}
|
|
export const useUpdateTenantDetails = (onSuccessCallback)=>{
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn:async({id,tenantPayload})=> TenantRepository.updateTenantDetails(id,tenantPayload),
|
|
onSuccess:(_,variables)=>{
|
|
const {id} = variables.tenantPayload;
|
|
queryClient.invalidateQueries({queryKey:["Tenant",id]})
|
|
queryClient.invalidateQueries({queryKey:["Tenants"]})
|
|
if(onSuccessCallback) onSuccessCallback()
|
|
},
|
|
onError:(error)=>{
|
|
showToast(error.response.message || error.message || `Something went wrong`,"error")
|
|
}
|
|
})
|
|
}
|
|
|
|
export const useAddSubscription =(onSuccessCallback)=>{
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn:async(subscriptionPayload)=>{
|
|
const res = await TenantRepository.addSubscription(subscriptionPayload);
|
|
return res.data;
|
|
},
|
|
onSuccess:(data,variables)=>{
|
|
showToast("Tenant Plan Added SuccessFully","success")
|
|
queryClient.invalidateQueries({queryKey:["Tenants"]});
|
|
if(onSuccessCallback) onSuccessCallback()
|
|
},
|
|
onError:(error)=>{
|
|
showToast(error.response.message || error.message || `Something went wrong`,"error")
|
|
}
|
|
})
|
|
} |