135 lines
5.0 KiB
TypeScript
135 lines
5.0 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import { findDocumentByDocumentType, getDocumentList, RELATIVE_PATH_PREFIX,
|
|
saveToGlobalDocumentMap, storeImageLocallyReturnRelativePath } from '../components/utlis/commonFunctions';
|
|
import { CaseDetail, DOCUMENT_TYPE, TDocumentObj } from '../screens/caseDetails/interface';
|
|
import { getSignedApi, ISignedRequest } from '../action/dataActions';
|
|
import { GlobalDocumentMap } from '../../App';
|
|
|
|
|
|
const getDocumentUrlFromCaseDetails = (caseDetails: CaseDetail, documentType: DOCUMENT_TYPE) => {
|
|
if (GlobalDocumentMap?.[caseDetails?.id]?.[documentType]) {
|
|
return GlobalDocumentMap[caseDetails.id][documentType];
|
|
}
|
|
|
|
const documentList = getDocumentList(caseDetails);
|
|
return findDocumentByDocumentType(documentList, documentType)?.uri;
|
|
}
|
|
|
|
const getInitialDocumentObj = (caseDetails: CaseDetail) => (documentTypeList: DOCUMENT_TYPE[]) => {
|
|
return documentTypeList?.reduce((acc, curr) => (
|
|
{ ...acc, [curr]: getDocumentUrlFromCaseDetails(caseDetails, curr) }), {} as TDocumentObj);
|
|
};
|
|
|
|
const useFetchDocument = (caseDetails: CaseDetail, documentTypeList: DOCUMENT_TYPE[], enableCaching = true, enableBatching = false) => {
|
|
|
|
|
|
const getInitialDocumentObjFn = getInitialDocumentObj(caseDetails);
|
|
|
|
const [documentObj, setDocumentObj] = useState<TDocumentObj>(
|
|
getInitialDocumentObjFn(documentTypeList)
|
|
);
|
|
|
|
const apiInProgressReferenceIds = useRef<string[]>([]);
|
|
|
|
const [retryForDocuments, setRetryForDocuments] = useState<DOCUMENT_TYPE[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (enableCaching) {
|
|
let localDocumentObj = { ...documentObj } as TDocumentObj;
|
|
(async () => {
|
|
(async () => {
|
|
for (const [documentType, documentValue] of Object.entries(localDocumentObj)) {
|
|
if (documentValue && documentType in DOCUMENT_TYPE && !documentValue?.startsWith(RELATIVE_PATH_PREFIX)) {
|
|
const localImagePath = await storeImageLocallyReturnRelativePath(documentValue);
|
|
localDocumentObj = {
|
|
...localDocumentObj,
|
|
[documentType]: localImagePath
|
|
}
|
|
}
|
|
}
|
|
})().then(async () => {
|
|
setDocumentObj(localDocumentObj);
|
|
})
|
|
})();
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (enableCaching && documentObj) {
|
|
(async () => {
|
|
await saveToGlobalDocumentMap(caseDetails?.id, documentObj);
|
|
})();
|
|
}
|
|
}, [documentObj, enableCaching]);
|
|
|
|
useEffect(() => {
|
|
if (!retryForDocuments?.length) return;
|
|
|
|
const documentList = getDocumentList(caseDetails);
|
|
let imageReferenceId: string | undefined;
|
|
|
|
for (let retryForDocumentsItem of retryForDocuments) {
|
|
imageReferenceId = findDocumentByDocumentType(documentList, retryForDocumentsItem)?.referenceId;
|
|
|
|
(async () => {
|
|
if (!imageReferenceId) {
|
|
setDocumentObj(documentObj => ({
|
|
...documentObj,
|
|
[retryForDocumentsItem]: ''
|
|
}))
|
|
return;
|
|
}
|
|
|
|
if (apiInProgressReferenceIds.current.includes(imageReferenceId)) return;
|
|
|
|
apiInProgressReferenceIds.current.push(imageReferenceId);
|
|
|
|
const signedRequestPayload: ISignedRequest = [
|
|
{
|
|
documentReferenceId: imageReferenceId,
|
|
caseId: caseDetails.id,
|
|
caseType: caseDetails.caseType,
|
|
},
|
|
];
|
|
|
|
setLoading(true);
|
|
const response = await getSignedApi(signedRequestPayload, enableBatching);
|
|
setLoading(false);
|
|
const url = response?.imageUrl;
|
|
|
|
if (!url) {
|
|
setDocumentObj(documentObj => ({
|
|
...documentObj,
|
|
[retryForDocumentsItem]: ''
|
|
}))
|
|
return;
|
|
}
|
|
|
|
const localImagePath = await storeImageLocallyReturnRelativePath(url);
|
|
|
|
if (localImagePath) {
|
|
setDocumentObj(documentObj => ({
|
|
...documentObj,
|
|
[retryForDocumentsItem]: localImagePath
|
|
}));
|
|
}
|
|
})();
|
|
}
|
|
|
|
return () => {
|
|
apiInProgressReferenceIds.current = apiInProgressReferenceIds.current.filter(item => item !== imageReferenceId);
|
|
}
|
|
}, [retryForDocuments]);
|
|
|
|
return {
|
|
documentObj,
|
|
setDocumentObj,
|
|
setRetryForUnsignedDocuments: setRetryForDocuments,
|
|
retryForUnsignedDocuments: retryForDocuments,
|
|
loading
|
|
};
|
|
};
|
|
|
|
export default useFetchDocument;
|