-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1348 from danskernesdigitalebibliotek/DDFLSBP-722…
…-availability-labels-viser-udlant-under-udgaver-skont-bogen-er-hjemme Availability bug
- Loading branch information
Showing
11 changed files
with
731 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
Access, | ||
AccessTypeCode | ||
} from "../../core/dbc-gateway/generated/graphql"; | ||
import { FaustId } from "../../core/utils/types/ids"; | ||
import { isOnline } from "./helper"; | ||
import useOnlineAvailabilityData from "./useOnlineAvailabilityData"; | ||
import usePhysicalAvailabilityData from "./usePhysicalAvailabilityData"; | ||
|
||
const useAvailabilityData = ({ | ||
accessTypes, | ||
access, | ||
faustIds, | ||
manifestText, | ||
isbn | ||
}: { | ||
accessTypes: AccessTypeCode[]; | ||
access: Access["__typename"][]; | ||
faustIds: FaustId[]; | ||
manifestText: string; | ||
isbn: string | null; | ||
}) => { | ||
const availabilityOnline = useOnlineAvailabilityData({ | ||
enabled: isOnline(accessTypes), | ||
access, | ||
faustIds, | ||
isbn | ||
}); | ||
|
||
const availabilityPhysical = usePhysicalAvailabilityData({ | ||
enabled: !isOnline(accessTypes), | ||
faustIds, | ||
manifestText | ||
}); | ||
|
||
if (isOnline(accessTypes)) { | ||
return availabilityOnline; | ||
} | ||
|
||
return availabilityPhysical; | ||
}; | ||
|
||
export default useAvailabilityData; |
96 changes: 96 additions & 0 deletions
96
src/components/availability-label/useOnlineAvailabilityData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useEffect, useState } from "react"; | ||
import { | ||
useGetV1LoanstatusIdentifier, | ||
useGetV1ProductsIdentifier | ||
} from "../../core/publizon/publizon"; | ||
import { Access } from "../../core/dbc-gateway/generated/graphql"; | ||
import { FaustId } from "../../core/utils/types/ids"; | ||
import { publizonProductStatuses } from "./types"; | ||
|
||
const useOnlineAvailabilityData = ({ | ||
enabled, | ||
access, | ||
faustIds, | ||
isbn | ||
}: { | ||
enabled: boolean; | ||
access: Access["__typename"][]; | ||
faustIds: FaustId[] | null; | ||
isbn: string | null; | ||
}) => { | ||
const [isAvailable, setIsAvailable] = useState<null | boolean>(null); | ||
|
||
// Find out if the material is cost free. | ||
const { isLoading: isLoadingIdentifier, data: dataIdentifier } = | ||
useGetV1ProductsIdentifier(isbn ?? "", { | ||
query: { | ||
// Publizon / useGetV1ProductsIdentifier is responsible for online | ||
// materials. It requires an ISBN to do lookups. | ||
enabled: enabled && isAvailable === null && isbn !== null | ||
} | ||
}); | ||
|
||
// Ereol request. | ||
const { isLoading: isLoadingEreolData, data: dataEreol } = | ||
useGetV1LoanstatusIdentifier(isbn || "", { | ||
// Publizon / useGetV1LoanstatusIdentifier shows loan status per material. | ||
// This status is only available for products found on Ereol. Other online | ||
// materials are always supposed to be shown as "available" | ||
enabled: | ||
enabled && | ||
isAvailable === null && | ||
!!isbn && | ||
// If the material is free (I think it is called blue material btw.) | ||
// we should not load the loan status because then we know that it is available. | ||
// So If the material is not free and we know it is an "Ereol" material we should load the loan status. | ||
dataIdentifier?.product?.costFree === false && | ||
access.some((acc) => acc === "Ereol") | ||
}); | ||
|
||
useEffect(() => { | ||
if ( | ||
!enabled || | ||
isAvailable !== null || | ||
isLoadingIdentifier !== false || | ||
isLoadingEreolData !== false | ||
) { | ||
return; | ||
} | ||
|
||
// If we have ereol data, we can use that to determine the availability. | ||
if (dataEreol && dataEreol.loanStatus) { | ||
setIsAvailable(publizonProductStatuses[dataEreol.loanStatus].isAvailable); | ||
} | ||
}, [ | ||
isLoadingIdentifier, | ||
isAvailable, | ||
faustIds, | ||
enabled, | ||
dataEreol, | ||
isLoadingEreolData | ||
]); | ||
|
||
// If hook is not enabled make it clear that the loading and availability status is unknown. | ||
if (!enabled) { | ||
return { | ||
isLoading: null, | ||
isAvailable: null | ||
}; | ||
} | ||
|
||
// An online material is by default always available if the availability status has not been set yet. | ||
if (isAvailable === null) { | ||
return { | ||
isLoading: false, | ||
isAvailable: true | ||
}; | ||
} | ||
|
||
// Return the availability status. | ||
return { | ||
isLoading: isLoadingIdentifier && isLoadingEreolData, | ||
isAvailable | ||
}; | ||
}; | ||
|
||
export default useOnlineAvailabilityData; |
Oops, something went wrong.