Skip to content

Commit

Permalink
Merge pull request #1433 from danskernesdigitalebibliotek/DDFBRA-49-v…
Browse files Browse the repository at this point in the history
…i-har-xx-eksemplarer-af-materialet-hjemme-vises-kun-under-handleknap-for-indlogget-bruger

We have XX specimens - fix business logic
  • Loading branch information
spaceo authored Sep 24, 2024
2 parents e1bcd94 + f6611ac commit fde3aa1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/components/availability-label/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type GetParrentAvailabilityLabelClassProps = {
export const isOnline = (accessTypes: AccessTypeCode[]): boolean =>
accessTypes?.includes(AccessTypeCode.Online) ?? false;

export const isArticle = (manifestText: string): boolean =>
export const isArticleByLabelText = (manifestText: string): boolean =>
articleTypes.some((type) => manifestText.toLowerCase() === type);

export const getParentAvailabilityLabelClass = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useConfig } from "../../core/utils/config";
import { FaustId } from "../../core/utils/types/ids";
import useGetAvailability from "../../core/utils/useGetAvailability";
import { isArticle } from "./helper";
import { isArticleByLabelText } from "./helper";

const usePhysicalAvailabilityData = ({
enabled,
Expand All @@ -22,7 +22,8 @@ const usePhysicalAvailabilityData = ({
// FBS / useGetAvailabilityV3 is responsible for handling availability
// for physical items. This will be the majority of all materials so we
// use this for everything except materials that are explicitly online.
enabled: enabled && faustIds !== null && !isArticle(manifestText)
enabled:
enabled && faustIds !== null && !isArticleByLabelText(manifestText)
}
}
});
Expand All @@ -38,7 +39,7 @@ const usePhysicalAvailabilityData = ({
}

// Articles are always available.
if (isArticle(manifestText)) {
if (isArticleByLabelText(manifestText)) {
return {
isLoading: false,
isAvailable: true
Expand Down
41 changes: 28 additions & 13 deletions src/components/material/MaterialHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
convertPostIdToFaustId,
creatorsToString,
flattenCreators,
getAllFaustIds,
getManifestationsPids,
getMaterialTypes,
getWorkPid
Expand All @@ -27,11 +28,11 @@ import { Manifestation, Work } from "../../core/utils/types/entities";
import { PeriodicalEdition } from "./periodical/helper";
import { useStatistics } from "../../core/statistics/useStatistics";
import { statistics } from "../../core/statistics/statistics";
import { hasCorrectMaterialType } from "./material-buttons/helper";
import { ManifestationMaterialType } from "../../core/utils/types/material-type";
import { useItemHasBeenVisible } from "../../core/utils/helpers/lazy-load";
import { getManifestationLanguageIsoCode } from "../../apps/material/helper";
import { isAnonymous } from "../../core/utils/helpers/user";
import { isPeriodical, shouldShowMaterialAvailabilityText } from "./helper";
import useAvailabilityData from "../availability-label/useAvailabilityData";
import { AccessTypeCode } from "../../core/dbc-gateway/generated/graphql";

interface MaterialHeaderProps {
wid: WorkId;
Expand Down Expand Up @@ -75,10 +76,6 @@ const MaterialHeader: React.FC<MaterialHeaderProps> = ({
);
};
const author = creatorsToString(flattenCreators(creators), t);
const isPeriodical = hasCorrectMaterialType(
ManifestationMaterialType.magazine,
selectedManifestations
);
const containsDanish = mainLanguages.some((language) =>
language?.isoCode.toLowerCase().includes("dan")
);
Expand All @@ -95,6 +92,18 @@ const MaterialHeader: React.FC<MaterialHeaderProps> = ({
const languageIsoCode = getManifestationLanguageIsoCode(
selectedManifestations
);
// We need availability in order to show availability text under action buttons
const { isAvailable } = useAvailabilityData({
// "accessTypes" will always be physical here - shouldShowMaterialAvailabilityText() helper
// rules out all online materials.
accessTypes: [AccessTypeCode.Physical],
access: [undefined],
faustIds: getAllFaustIds(selectedManifestations),
isbn: null, // Not needed for physical materials.
// "manifestText" is used inside the availability hook to check whether the material is an article
// which we check inside shouldShowMaterialAvailabilityText() helper here.
manifestText: "NOT AN ARTICLE"
});

useDeepCompareEffect(() => {
track("click", {
Expand Down Expand Up @@ -154,7 +163,7 @@ const MaterialHeader: React.FC<MaterialHeaderProps> = ({
{/* The CTA buttons apparently only make sense on a global work */}
{!isGlobalMaterial && showItem && (
<>
{isPeriodical && (
{isPeriodical(selectedManifestations) && (
<MaterialPeriodical
faustId={convertPostIdToFaustId(pid)}
selectedPeriodical={selectedPeriodical}
Expand All @@ -171,11 +180,17 @@ const MaterialHeader: React.FC<MaterialHeaderProps> = ({
materialTitleId={materialTitleId}
/>
</div>
{!isAnonymous() && (
<MaterialAvailabilityText
manifestations={selectedManifestations}
/>
)}
{/* MaterialAvailabilityText is only shown for:
- physical manifestations
- that are not periodical or articles
- that are available in at least one local library branch
*/}
{shouldShowMaterialAvailabilityText(selectedManifestations) &&
isAvailable && (
<MaterialAvailabilityText
manifestations={selectedManifestations}
/>
)}
</>
)}
{children}
Expand Down
31 changes: 31 additions & 0 deletions src/components/material/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { AccessTypeCode } from "../../core/dbc-gateway/generated/graphql";
import { Manifestation } from "../../core/utils/types/entities";
import { ManifestationMaterialType } from "../../core/utils/types/material-type";
import { hasCorrectMaterialType, isArticle } from "./material-buttons/helper";

export const isPhysical = (manifestations: Manifestation[]) => {
return manifestations.some((manifestation) => {
return manifestation.accessTypes.some((acc) => {
return acc.code === AccessTypeCode.Physical;
});
});
};

export const isPeriodical = (manifestations: Manifestation[]) => {
return hasCorrectMaterialType(
ManifestationMaterialType.magazine,
manifestations
);
};

export const shouldShowMaterialAvailabilityText = (
manifestations: Manifestation[]
) => {
return (
isPhysical(manifestations) &&
!isPeriodical(manifestations) &&
!isArticle(manifestations)
);
};

export default {};

0 comments on commit fde3aa1

Please sign in to comment.