diff --git a/src/components/material/MaterialAvailabilityText/MaterialAvailabilityText.tsx b/src/components/material/MaterialAvailabilityText/MaterialAvailabilityText.tsx index 6858690857..63460975cf 100644 --- a/src/components/material/MaterialAvailabilityText/MaterialAvailabilityText.tsx +++ b/src/components/material/MaterialAvailabilityText/MaterialAvailabilityText.tsx @@ -22,12 +22,12 @@ const MaterialAvailabilityText: React.FC = ({ manifestations }) => { const t = useText(); const materialType = head(getMaterialTypes(manifestations)); const isbns = getAllIdentifiers(manifestations); - const reservablePidsFromAnotherLibrary = + const { materialIsReservableFromAnotherLibrary } = useReservableFromAnotherLibrary(manifestations); if (hasCorrectAccessType(AccessTypeCode.Physical, manifestations)) { const pids = getAllPids(manifestations); - if (reservablePidsFromAnotherLibrary.length) { + if (materialIsReservableFromAnotherLibrary) { return ( {t("reservableFromAnotherLibraryText")} diff --git a/src/components/material/material-buttons/MaterialButtons.tsx b/src/components/material/material-buttons/MaterialButtons.tsx index 1773040a14..73146bc03b 100644 --- a/src/components/material/material-buttons/MaterialButtons.tsx +++ b/src/components/material/material-buttons/MaterialButtons.tsx @@ -35,10 +35,10 @@ const MaterialButtons: FC = ({ // articles appear as a part of journal/periodical publications and can't be // physically loaned for themseleves. - const reservablePidsFromAnotherLibrary = + const { materialIsReservableFromAnotherLibrary } = useReservableFromAnotherLibrary(manifestations); - if (reservablePidsFromAnotherLibrary.length > 0) { + if (materialIsReservableFromAnotherLibrary) { return ( 0 && patron) { + if (materialIsReservableFromAnotherLibrary && patron) { const { patronId, name, emailAddress, preferredPickupBranch } = patron; // Save reservation to open order. mutateOpenOrder( { input: { - pids: [...reservablePidsFromAnotherLibrary], + pids: reservablePidsFromAnotherLibrary, pickUpBranch: selectedBranch ? removePrefixFromBranchId(selectedBranch) : removePrefixFromBranchId(preferredPickupBranch), @@ -253,7 +254,7 @@ export const ReservationModalBody = ({
- {reservablePidsFromAnotherLibrary?.length > 0 ? ( + {materialIsReservableFromAnotherLibrary ? ( t("reservableFromAnotherLibraryText") ) : ( 0 && + materialIsReservableFromAnotherLibrary && selectedInterest === null ? Number(defaultInterestDaysForOpenOrder) : selectedInterest diff --git a/src/core/utils/useReservableFromAnotherLibrary.tsx b/src/core/utils/useReservableFromAnotherLibrary.tsx index a418eadbb3..f10cf4ded1 100644 --- a/src/core/utils/useReservableFromAnotherLibrary.tsx +++ b/src/core/utils/useReservableFromAnotherLibrary.tsx @@ -6,7 +6,10 @@ import { Pid } from "./types/ids"; const useReservableFromAnotherLibrary = ( manifestations: Manifestation[] -): Pid[] => { +): { + reservablePidsFromAnotherLibrary: Pid[]; + materialIsReservableFromAnotherLibrary: boolean; +} => { const config = useConfig(); const { data: holdingsData } = useGetHoldings({ faustIds: getAllFaustIds(manifestations), @@ -16,14 +19,26 @@ const useReservableFromAnotherLibrary = ( // If there is no holdings data or if there are holdings that are reservable, we return an empty array. // Because we use the array length to determine if we should show the button or not. if (holdingsData?.some(({ reservable }) => reservable === true)) { - return []; + return { + reservablePidsFromAnotherLibrary: [], + materialIsReservableFromAnotherLibrary: false + }; } - return manifestations + const reservablePidsFromAnotherLibrary = manifestations .filter(({ catalogueCodes }) => catalogueCodes?.otherCatalogues.some((code) => code.startsWith("OVE")) ) .map(({ pid }) => pid); + + const materialIsReservableFromAnotherLibrary = Boolean( + reservablePidsFromAnotherLibrary.length + ); + + return { + reservablePidsFromAnotherLibrary, + materialIsReservableFromAnotherLibrary + }; }; export default useReservableFromAnotherLibrary; diff --git a/src/tests/unit/useReservableFromAnotherLibrary.test.tsx b/src/tests/unit/useReservableFromAnotherLibrary.test.tsx index d3bbe9036f..b52798559f 100644 --- a/src/tests/unit/useReservableFromAnotherLibrary.test.tsx +++ b/src/tests/unit/useReservableFromAnotherLibrary.test.tsx @@ -73,7 +73,10 @@ describe("useReservableFromAnotherLibrary", () => { ); act(() => { - expect(result.current).toEqual(["870970-basis:27721257"]); + expect(result.current.reservablePidsFromAnotherLibrary).toEqual([ + "870970-basis:27721257" + ]); + expect(result.current.materialIsReservableFromAnotherLibrary).toBe(true); }); }); @@ -103,7 +106,8 @@ describe("useReservableFromAnotherLibrary", () => { ); act(() => { - expect(result.current).toEqual([]); + expect(result.current.reservablePidsFromAnotherLibrary).toEqual([]); + expect(result.current.materialIsReservableFromAnotherLibrary).toBe(false); }); }); });