201 lines
5.4 KiB
JavaScript
201 lines
5.4 KiB
JavaScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import showToast from "../services/toastService";
|
|
import { DocumentRepository } from "../repositories/DocumentRepository";
|
|
|
|
// ----------------------Query-------------------------------
|
|
const cleanFilter = (filter) => {
|
|
const cleaned = { ...filter };
|
|
|
|
[
|
|
"uploadedByIds",
|
|
"documentCategoryIds",
|
|
"documentTypeIds",
|
|
"documentTagIds",
|
|
].forEach((key) => {
|
|
if (Array.isArray(cleaned[key]) && cleaned[key].length === 0) {
|
|
delete cleaned[key];
|
|
}
|
|
});
|
|
|
|
["startDate", "endDate", "isVerified"].forEach((key) => {
|
|
if (cleaned[key] === null) {
|
|
delete cleaned[key];
|
|
}
|
|
});
|
|
|
|
return cleaned;
|
|
};
|
|
export const useDocumentListByEntityId = (
|
|
entityTypeId,
|
|
entityId,
|
|
pageSize,
|
|
pageNumber,
|
|
filter,
|
|
searchString = "",
|
|
isActive
|
|
) => {
|
|
return useQuery({
|
|
queryKey: [
|
|
"DocumentList",
|
|
entityTypeId,
|
|
entityId,
|
|
pageSize,
|
|
pageNumber,
|
|
filter,
|
|
searchString,
|
|
isActive
|
|
],
|
|
queryFn: async () => {
|
|
const cleanedFilter = cleanFilter(filter);
|
|
const resp = await DocumentRepository.getDocumentList(
|
|
entityTypeId,
|
|
entityId,
|
|
pageSize,
|
|
pageNumber,
|
|
cleanedFilter,
|
|
searchString,
|
|
isActive
|
|
);
|
|
return resp.data
|
|
|
|
},
|
|
enabled: !!entityTypeId && !!entityId,
|
|
});
|
|
};
|
|
|
|
export const useDocumentFilterEntities = (entityTypeId) => {
|
|
return useQuery({
|
|
queryKey: ["DFilter", entityTypeId],
|
|
queryFn: async () =>
|
|
await DocumentRepository.getFilterEntities(entityTypeId),
|
|
});
|
|
};
|
|
|
|
export const useDocumentDetails = (documentId) => {
|
|
return useQuery({
|
|
queryKey: ["Document", documentId],
|
|
queryFn: async () => {
|
|
const resp = await DocumentRepository.getDocumentById(documentId);
|
|
return resp.data;
|
|
},
|
|
enabled: !!documentId,
|
|
});
|
|
};
|
|
|
|
export const useDocumentVersionList = (parentAttachmentId,pageSize,pageNumber) => {
|
|
return useQuery({
|
|
queryKey: ["DocumentVersionList", parentAttachmentId,pageSize,pageNumber],
|
|
queryFn: async () => {
|
|
const resp = await DocumentRepository.getDocumentVersionList(parentAttachmentId,pageSize,pageNumber);
|
|
return resp.data
|
|
},
|
|
|
|
enabled: !!parentAttachmentId,
|
|
});
|
|
};
|
|
|
|
|
|
export const useDocumentVersion = (id)=>{
|
|
return useQuery({
|
|
queryKey:["DocumentVersion",id],
|
|
queryFn:async()=> await DocumentRepository.getDocumentVersion(id),
|
|
enabled:!!id
|
|
})
|
|
}
|
|
|
|
export const useDocumentTags =()=>{
|
|
return useQuery({
|
|
queryKey:["DocumentTag"],
|
|
queryFn:async()=> {const resp = await DocumentRepository.getDocumentTags()
|
|
return resp.data
|
|
}
|
|
})
|
|
}
|
|
//----------------------- MUTATION -------------------------
|
|
|
|
export const useUploadDocument = (onSuccessCallBack) => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async (DocumentPayload) =>
|
|
DocumentRepository.uploadDocument(DocumentPayload),
|
|
onSuccess: (data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ["DocumentList"] });
|
|
|
|
if (onSuccessCallBack) onSuccessCallBack();
|
|
},
|
|
onError: (error) => {
|
|
console.log(error);
|
|
showToast(
|
|
error.response.data.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|
|
export const useUpdateDocument = (onSuccessCallBack) => {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: async ({ documentId, DocumentPayload }) =>
|
|
DocumentRepository.UpdateDocument(documentId, DocumentPayload),
|
|
onSuccess: (data, variables) => {
|
|
const { documentId } = variables;
|
|
queryClient.invalidateQueries({ queryKey: ["DocumentList"] });
|
|
queryClient.invalidateQueries({ queryKey: ["Document", documentId] });
|
|
if (onSuccessCallBack) onSuccessCallBack();
|
|
},
|
|
onError: (error) => {
|
|
console.log(error);
|
|
showToast(
|
|
error.response.data.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
});
|
|
};
|
|
|
|
export const useVerifyDocument = ()=>{
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn:async({documentId,isVerify}) => await DocumentRepository.verifyDocument(documentId,isVerify),
|
|
onSuccess: (data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ["DocumentVersionList"] });
|
|
queryClient.invalidateQueries({ queryKey: ["DocumentList"] });
|
|
queryClient.invalidateQueries({ queryKey: ["Document"] });
|
|
showToast(
|
|
data.response.data.message ||
|
|
"Document Successfully Verified !",
|
|
"success"
|
|
);
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.data.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
|
|
})
|
|
}
|
|
|
|
export const useActiveInActiveDocument = ()=>{
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn:async({documentId,isActive}) => await DocumentRepository.deleteDocument(documentId,isActive),
|
|
onSuccess: (data, variables) => {
|
|
const {isActive} = variables;
|
|
queryClient.invalidateQueries({ queryKey: ["DocumentList"] });
|
|
showToast(`Document ${isActive ? "restored":"Deleted"} successfully`,"success")
|
|
},
|
|
onError: (error) => {
|
|
showToast(
|
|
error.response.data.message ||
|
|
"Something went wrong please try again !",
|
|
"error"
|
|
);
|
|
},
|
|
|
|
})
|
|
} |