Skip to content

Commit

Permalink
Refactor getReaderPlayerType
Browse files Browse the repository at this point in the history
Eliminate redundant code, prevent duplicate invocations of `getMaterialTypes`, and consolidate the logic into a single function for determining the type. Use the `type` from `useReaderPlayerButtons` to control when player logic is enabled.
  • Loading branch information
kasperbirch1 committed Dec 10, 2024
1 parent 53fb947 commit 8e3ea82
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
11 changes: 6 additions & 5 deletions src/apps/material/material.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import {
import MaterialDisclosure from "./MaterialDisclosure";
import ReservationFindOnShelfModals from "./ReservationFindOnShelfModals";
import PlayerModal from "../../components/material/player-modal/PlayerModal";
import { hasPlayerManifestation } from "../../components/reader-player/helper";
import useReaderPlayerButtons from "../../core/utils/useReaderPlayerButtons";

export interface MaterialProps {
Expand All @@ -57,9 +56,11 @@ const Material: React.FC<MaterialProps> = ({ wid }) => {
const { data: userData } = usePatronData();
const [isUserBlocked, setIsUserBlocked] = useState<boolean | null>(null);
const { track } = useStatistics();
const { identifier, orderId } = useReaderPlayerButtons(
selectedManifestations
);
const {
type: readerPlayerType,
identifier,
orderId
} = useReaderPlayerButtons(selectedManifestations);

useEffect(() => {
setIsUserBlocked(!!(userData?.patron && isBlocked(userData.patron)));
Expand Down Expand Up @@ -176,7 +177,7 @@ const Material: React.FC<MaterialProps> = ({ wid }) => {
setSelectedPeriodical={setSelectedPeriodical}
/>
))}
{hasPlayerManifestation(selectedManifestations) && (
{readerPlayerType === "player" && (
<>
{identifier && <PlayerModal identifier={identifier} />}
{orderId && <PlayerModal orderId={orderId} />}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import React, { FC } from "react";
import { Manifestation } from "../../../../core/utils/types/entities";
import {
hasPlayerManifestation,
hasReaderManifestation
} from "../../../reader-player/helper";
import MaterialSecondaryLink from "../generic/MaterialSecondaryLink";
import MaterialSecondaryButton from "../generic/MaterialSecondaryButton";
import { playerModalId } from "../../player-modal/helper";
Expand Down Expand Up @@ -73,7 +69,7 @@ const MaterialButtonsOnlineInternal: FC<MaterialButtonsOnlineInternalType> = ({
return null;
};

if (hasReaderManifestation(manifestations) && identifier) {
if (type === "reader" && identifier) {
return (
<>
{renderReaderButton()}
Expand All @@ -92,7 +88,7 @@ const MaterialButtonsOnlineInternal: FC<MaterialButtonsOnlineInternalType> = ({
);
}

if (hasPlayerManifestation(manifestations) && identifier) {
if (type === "player" && identifier) {
return (
<>
{renderPlayerButton()}
Expand Down
43 changes: 19 additions & 24 deletions src/components/reader-player/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,6 @@ export const removeAppendedAssets = () => {
});
};

export const hasReaderManifestation = (manifestations: Manifestation[]) => {
const materialTypes = getMaterialTypes(manifestations);
return materialTypes.some(
(type) =>
type === ManifestationMaterialType.ebook ||
type === ManifestationMaterialType.pictureBookOnline ||
type === ManifestationMaterialType.animatedSeriesOnline ||
type === ManifestationMaterialType.yearBookOnline
);
};

export const hasPlayerManifestation = (manifestations: Manifestation[]) => {
const materialTypes = getMaterialTypes(manifestations);
return materialTypes.some(
(type) =>
type === ManifestationMaterialType.audioBook ||
type === ManifestationMaterialType.podcast ||
type === ManifestationMaterialType.musicOnline ||
type === ManifestationMaterialType.audioBookTape
);
};

export const getOrderIdByIdentifier = ({
loans,
identifier
Expand All @@ -109,8 +87,25 @@ export const getOrderIdByIdentifier = ({
export const getReaderPlayerType = (
manifestations: Manifestation[]
): "reader" | "player" | null => {
if (hasReaderManifestation(manifestations)) return "reader";
if (hasPlayerManifestation(manifestations)) return "player";
const materialTypes = getMaterialTypes(manifestations);

const readerTypes = [
ManifestationMaterialType.ebook,
ManifestationMaterialType.pictureBookOnline,
ManifestationMaterialType.animatedSeriesOnline,
ManifestationMaterialType.yearBookOnline
];

const playerTypes = [
ManifestationMaterialType.audioBook,
ManifestationMaterialType.podcast,
ManifestationMaterialType.musicOnline,
ManifestationMaterialType.audioBookTape
];

if (readerTypes.some((type) => materialTypes.includes(type))) return "reader";
if (playerTypes.some((type) => materialTypes.includes(type))) return "player";

return null;
};

Expand Down

0 comments on commit 8e3ea82

Please sign in to comment.