197 lines
5.4 KiB
JavaScript
197 lines
5.4 KiB
JavaScript
import { useSelector, useDispatch } from "react-redux";
|
|
import {
|
|
toggleOrgModal,
|
|
openOrgModal,
|
|
closeOrgModal,
|
|
} from "../slices/localVariablesSlice";
|
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import OrganizationRepository from "../repositories/OrganizationRespository";
|
|
import showToast from "../services/toastService";
|
|
|
|
export const useOrganizationModal = () => {
|
|
const dispatch = useDispatch();
|
|
const { isOpen, orgData, startStep, prevStep, flowType } = useSelector(
|
|
(state) => state.localVariables.OrganizationModal
|
|
);
|
|
|
|
return {
|
|
isOpen,
|
|
orgData,
|
|
startStep,
|
|
prevStep,
|
|
flowType,
|
|
onOpen: (options = {}) =>
|
|
dispatch(
|
|
openOrgModal({
|
|
isOpen: true,
|
|
orgData: options.orgData ?? orgData ?? null,
|
|
startStep: options.startStep ?? startStep ?? 1,
|
|
prevStep: options.prevStep ?? prevStep ?? 1,
|
|
flowType: options.flowType ?? flowType ?? "default",
|
|
})
|
|
),
|
|
onClose: () => dispatch(closeOrgModal()),
|
|
onToggle: () => dispatch(toggleOrgModal()),
|
|
};
|
|
};
|
|
|
|
// ================================Query=============================================================
|
|
|
|
export const useOrganization=(id)=>{
|
|
return useQuery({
|
|
queryKey:["organization",id],
|
|
queryFn:async()=> {
|
|
const resp = await await OrganizationRepository.getOrganizaion(id);
|
|
return resp.data
|
|
},
|
|
enabled:!!id
|
|
})
|
|
}
|
|
export const useOrganizationBySPRID = (sprid) => {
|
|
return useQuery({
|
|
queryKey: ["organization by", sprid],
|
|
queryFn: async () => {
|
|
const resp = await OrganizationRepository.getOrganizationBySPRID(sprid);
|
|
return resp.data;
|
|
},
|
|
enabled: !!sprid,
|
|
});
|
|
};
|
|
|
|
export const useOrganizationsList = (
|
|
pageSize,
|
|
pageNumber,
|
|
active,
|
|
sprid,
|
|
searchString = ""
|
|
) => {
|
|
return useQuery({
|
|
queryKey: [
|
|
"organizationList",
|
|
pageSize,
|
|
pageNumber,
|
|
active,
|
|
sprid,
|
|
searchString,
|
|
],
|
|
queryFn: async () => {
|
|
const resp = await OrganizationRepository.getOrganizationList(
|
|
pageSize,
|
|
pageNumber,
|
|
active,
|
|
sprid,
|
|
searchString
|
|
);
|
|
return resp.data;
|
|
},
|
|
keepPreviousData: true,
|
|
});
|
|
};
|
|
|
|
export const useOrganizationEmployees = (
|
|
projectId,
|
|
organizationId,
|
|
searchString
|
|
) => {
|
|
return useQuery({
|
|
queryKey: ["OrgEmployees", projectId, organizationId, searchString],
|
|
queryFn: async () =>
|
|
await OrganizationRepository.getOrganizationEmployees(
|
|
projectId,
|
|
organizationId,
|
|
searchString
|
|
),
|
|
enabled: !!projectId ,
|
|
});
|
|
};
|
|
|
|
// =================================Mutation========================================================
|
|
|
|
export const useCreateOrganization = (onSuccessCallback) => {
|
|
const useClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (OrgPayload) =>
|
|
await OrganizationRepository.createOrganization(OrgPayload),
|
|
onSuccess: (_, variables) => {
|
|
useClient.invalidateQueries({ queryKey: ["organizationList"] });
|
|
showToast("Organization created successfully", "success");
|
|
if (onSuccessCallback) onSuccessCallback();
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.data.message ||
|
|
error.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useAssignOrgToProject = (onSuccessCallback) => {
|
|
const useClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (payload) =>
|
|
await OrganizationRepository.assignOrganizationToProject(payload),
|
|
onSuccess: (_, variables) => {
|
|
const { projectId } = variables;
|
|
useClient.invalidateQueries({
|
|
queryKey: ["projectAssignedOrganiztions"],
|
|
});
|
|
useClient.invalidateQueries({
|
|
queryKey: ["projectAssignedServices", projectId],
|
|
});
|
|
showToast("Organization successfully", "success");
|
|
if (onSuccessCallback) onSuccessCallback();
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.data.message ||
|
|
error.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|
|
export const useAssignOrgToTenant = (onSuccessCallback) => {
|
|
const useClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (payload) =>
|
|
await OrganizationRepository.assignOrganizationToTenanat(payload),
|
|
onSuccess: (_, variables) => {
|
|
useClient.invalidateQueries({ queryKey: ["organizationList"] });
|
|
showToast("Organization added successfully", "success");
|
|
if (onSuccessCallback) onSuccessCallback();
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.data.message ||
|
|
error.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|
|
export const useUpdateOrganization = (onSuccessCallback) => {
|
|
const useClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({orgId,payload}) =>
|
|
await OrganizationRepository.updateOrganizaion(orgId,payload),
|
|
onSuccess: (_, variables) => {
|
|
useClient.invalidateQueries({ queryKey: ["organizationList"] });
|
|
showToast("Organization Updated successfully", "success");
|
|
if (onSuccessCallback) onSuccessCallback();
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.data.message ||
|
|
error.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|