From f40db66adef0573770d9ff82710ac4a6e69a45eb Mon Sep 17 00:00:00 2001 From: Kasper Birch Date: Tue, 28 Jan 2025 10:00:03 +0100 Subject: [PATCH] Remove use of `useLoans` and `useReservations` in `useReaderPlayer` The use of `useLoans` and `useReservations` caused unexpected behavior, where the FBS API was called, leading to Cypress test failures. I was unable to get `enabled: !isUserAnonymous` to work properly with the FBS API. However, I believe it is also preferable to call Publizon directly from `useReaderPlayer`, as FBS materials are not relevant in this context. Before this change, I temporarily fixed the Cypress test by adding fixtures for: `cy.intercept("GET", "**/external/agencyid/patrons/patronid/loans/v2", {`inside src/components/reservation/reservation.test.ts. --- src/core/utils/useReaderPlayer.tsx | 34 +++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/core/utils/useReaderPlayer.tsx b/src/core/utils/useReaderPlayer.tsx index 75ab3996d..4e077c7a3 100644 --- a/src/core/utils/useReaderPlayer.tsx +++ b/src/core/utils/useReaderPlayer.tsx @@ -1,5 +1,3 @@ -import useLoans from "./useLoans"; -import useReservations from "./useReservations"; import { Manifestation } from "./types/entities"; import { getManifestationIsbn } from "../../apps/material/helper"; import { @@ -10,6 +8,14 @@ import { import { isAnonymous } from "./helpers/user"; import useOnlineAvailabilityData from "../../components/availability-label/useOnlineAvailabilityData"; import { hasCorrectAccess } from "../../components/material/material-buttons/helper"; +import { + useGetV1UserLoans, + useGetV1UserReservations +} from "../publizon/publizon"; +import { + mapPublizonLoanToLoanType, + mapPublizonReservationToReservationType +} from "./helpers/list-mapper"; const useReaderPlayer = (manifestations: Manifestation[] | null) => { const isUserAnonymous = isAnonymous(); @@ -23,13 +29,19 @@ const useReaderPlayer = (manifestations: Manifestation[] | null) => { ? getManifestationIsbn(manifestations[0]) : null; - const { - publizon: { loans } - } = useLoans(); - - const { - publizon: { reservations } - } = useReservations(); + const { data: loansPublizon } = useGetV1UserLoans( + {}, + { query: { enabled: !isUserAnonymous } } + ); + const loans = loansPublizon?.loans + ? mapPublizonLoanToLoanType(loansPublizon.loans) + : null; + const { data: reservationsPublizon } = useGetV1UserReservations({ + query: { enabled: !isUserAnonymous } + }); + const reservations = reservationsPublizon?.reservations + ? mapPublizonReservationToReservationType(reservationsPublizon.reservations) + : null; const availabilityData = useOnlineAvailabilityData({ enabled: !!identifier, @@ -46,7 +58,9 @@ const useReaderPlayer = (manifestations: Manifestation[] | null) => { loans && identifier ? getOrderIdByIdentifier({ loans, identifier }) : null; const isAllReadyReservedButtonVisible = - identifier && isIdentifierReserved(identifier, reservations); + identifier && reservations + ? isIdentifierReserved(identifier, reservations) + : false; const isMaterialLoanedButtonVisible = !!orderId; const isLoanButtonVisible = isUserAnonymous || isAvailable; const isReserveButtonVisible = !isAvailable;