Skip to content

Commit

Permalink
Develop solution that uses dcApiToken expires.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewjordan committed Jan 16, 2024
1 parent fa956ad commit b3ba004
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
5 changes: 4 additions & 1 deletion app/assets/js/components/UI/IIIF/Viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ const IIIFViewer = ({ fileSets, iiifContent, workTypeId }) => {
*/
const handleCanvasIdCallback = (canvasId) => {
if (canvasId) {
const canvasIndex = canvasId.split("/").pop();
if (fileSets[canvasIndex]?.id === activeMediaFileSet?.id) return;

dispatch({
type: "updateActiveMediaFileSet",
fileSet: fileSets[canvasId.split("/").pop()],
fileSet: fileSets[canvasIndex],
});
}
return;
Expand Down
50 changes: 31 additions & 19 deletions app/assets/js/components/Work/Work.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,35 @@ const Work = ({ work }: { work: WorkType }) => {
/**
* Get the DC API super user token from the API every 5 minutes.
*/
const { data: dataDcApiToken, error: errorDcApiToken } = useQuery(
GET_DC_API_TOKEN,
{ pollInterval: 300000 },
);
const {
data: dataDcApiToken,
error: errorDcApiToken,
refetch: refetchDcApiToken,
} = useQuery(GET_DC_API_TOKEN);

const dcApiToken = dataDcApiToken?.dcApiToken?.token;
const { token, expires } = dataDcApiToken?.dcApiToken || {};

useEffect(
() =>
useEffect(() => {
if (token) {
workDispatch({
type: "updateDcApiToken",
dcApiToken,
}),
[dcApiToken],
);
dcApiToken: token,
});
}

const handleTokenUpdate = setInterval(() => {
const timePadding = 60000; // 1 minute
const currentDate = new Date();
const expiresDate = new Date(expires);
const expireDifference = expiresDate.getTime() - currentDate.getTime();
console.log(currentDate, expiresDate, expireDifference);
if (expireDifference < timePadding) {
refetchDcApiToken();
}
}, 100);

return () => clearInterval(handleTokenUpdate);
}, [token]);

if (errorDcApiToken) console.error(errorDcApiToken);

Expand Down Expand Up @@ -70,14 +84,12 @@ const Work = ({ work }: { work: WorkType }) => {
<section>
<div data-testid="viewer">
{isViewerReady ? (
<>
<IIIFViewer
fileSet={activeMediaFileSet}
fileSets={[...filterFileSets(fileSets).access]}
iiifContent={work.manifestUrl}
workTypeId={work.workType?.id}
/>
</>
<IIIFViewer
fileSet={activeMediaFileSet}
fileSets={[...filterFileSets(fileSets).access]}
iiifContent={work.manifestUrl}
workTypeId={work.workType?.id}
/>
) : (
<p className="has-text-centered has-text-grey is-size-5">
No filesets have been associated with this work.
Expand Down

0 comments on commit b3ba004

Please sign in to comment.