Skip to content

Commit

Permalink
Add materialIsReservableFromAnotherLibrary to `useReservableFromAno…
Browse files Browse the repository at this point in the history
…therLibrary`

 For better code readability
  • Loading branch information
kasperbirch1 committed Apr 8, 2024
1 parent 5d38f24 commit 5658543
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const MaterialAvailabilityText: React.FC<Props> = ({ 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 (
<MaterialAvailabilityTextParagraph>
{t("reservableFromAnotherLibraryText")}
Expand Down
4 changes: 2 additions & 2 deletions src/components/material/material-buttons/MaterialButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ const MaterialButtons: FC<MaterialButtonsProps> = ({
// 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 (
<MaterialButtonReservableFromAnotherLibrary
size={size}
Expand Down
15 changes: 8 additions & 7 deletions src/components/reservation/ReservationModalBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ export const ReservationModalBody = ({
!!selectedPeriodical
);

const reservablePidsFromAnotherLibrary = useReservableFromAnotherLibrary(
selectedManifestations
);
const {
reservablePidsFromAnotherLibrary,
materialIsReservableFromAnotherLibrary
} = useReservableFromAnotherLibrary(selectedManifestations);

// If we don't have all data for displaying the view render nothing.
if (!userResponse.data || !holdingsResponse.data) {
Expand Down Expand Up @@ -178,14 +179,14 @@ export const ReservationModalBody = ({
);
}

if (reservablePidsFromAnotherLibrary?.length > 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),
Expand Down Expand Up @@ -253,7 +254,7 @@ export const ReservationModalBody = ({
<div>
<div className="reservation-modal-submit">
<MaterialAvailabilityTextParagraph>
{reservablePidsFromAnotherLibrary?.length > 0 ? (
{materialIsReservableFromAnotherLibrary ? (
t("reservableFromAnotherLibraryText")
) : (
<StockAndReservationInfo
Expand Down Expand Up @@ -302,7 +303,7 @@ export const ReservationModalBody = ({
selectedBranch={selectedBranch}
selectBranchHandler={setSelectedBranch}
selectedInterest={
reservablePidsFromAnotherLibrary?.length > 0 &&
materialIsReservableFromAnotherLibrary &&
selectedInterest === null
? Number(defaultInterestDaysForOpenOrder)
: selectedInterest
Expand Down
21 changes: 18 additions & 3 deletions src/core/utils/useReservableFromAnotherLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
8 changes: 6 additions & 2 deletions src/tests/unit/useReservableFromAnotherLibrary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -103,7 +106,8 @@ describe("useReservableFromAnotherLibrary", () => {
);

act(() => {
expect(result.current).toEqual([]);
expect(result.current.reservablePidsFromAnotherLibrary).toEqual([]);
expect(result.current.materialIsReservableFromAnotherLibrary).toBe(false);
});
});
});

0 comments on commit 5658543

Please sign in to comment.