178 lines
5.1 KiB
JavaScript
178 lines
5.1 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];
|
|
}
|
|
});
|
|
|
|
|
|
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;
|
|
},
|
|
enabled:!!id
|
|
});
|
|
};
|
|
|
|
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 () => {
|
|
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}))
|
|
let operationMode = 0; // default = new tenant, needs subscription
|
|
if (data?.subscriptionHistery?.length > 0) {
|
|
operationMode = 1; // tenant already has a subscription
|
|
} else if (data && !data.subscriptionHistery) {
|
|
operationMode = 2; // tenant exists but subscription not added yet
|
|
}
|
|
|
|
dispatch(setCurrentTenant({ operationMode, 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) => {
|
|
const { tenantId } = variables;
|
|
showToast("Tenant Plan Added SuccessFully", "success");
|
|
queryClient.invalidateQueries({ queryKey: ["Tenant", tenantId] });
|
|
if (onSuccessCallback) onSuccessCallback();
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.message || error.message || `Something went wrong`,
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useUpgradeSubscription = (onSuccessCallback) => {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: async (subscriptionPayload) => {
|
|
const res = await TenantRepository.upgradeSubscription(
|
|
subscriptionPayload
|
|
);
|
|
return res.data;
|
|
},
|
|
onSuccess: (data, variables) => {
|
|
const { tenantId } = variables;
|
|
showToast("Tenant Plan Upgraded Successfully", "success");
|
|
|
|
// Refetch tenant details
|
|
queryClient.invalidateQueries({ queryKey: ["Tenant", tenantId] });
|
|
queryClient.invalidateQueries({ queryKey: ["Tenants"] });
|
|
|
|
if (onSuccessCallback) onSuccessCallback();
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error?.response?.message ||
|
|
error?.response?.data?.errors ||
|
|
error.message ||
|
|
"Something went wrong",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|