Commit 5aad36cb authored by David Björkheim's avatar David Björkheim
Browse files

DEICH-5558 Deichman.no: Handle not reservable publications

parent efb4b341
......@@ -31,7 +31,7 @@ const ReserveButton = ({ items, formats, onReserve, numHolds }) => {
disabled={disableAllReservations}
onClick={onReserve}
>
Bestill
Velg
</Button>
{disableAllReservations && (
<p style={{ marginLeft: "20px" }}>
......
......@@ -13,7 +13,7 @@ export default function PublicationList({
ReserveComponent,
tjenesteKatalogUrl,
userCategory,
publications,
publications = [],
copies
}) {
const dispPublications = sortPublications(publications);
......@@ -41,7 +41,7 @@ export default function PublicationList({
}
PublicationList.defaultProps = {
items: [],
copies: {},
favourites: [],
limited: false,
userCategory: ""
......
......@@ -4,6 +4,7 @@ import { useDispatch, useSelector } from "react-redux";
import PublicationList from "./PublicationList";
import ReservationContainer from "../ReservationsContainer/ReservationContainer";
import LoginButton from "../LoginButton/LoginButton";
import { notReservableReason } from "../ReservePublicationWidget/ReservePublicationWidget";
export default function PublicationsAccordionContainer({ publication }) {
const [isLoading, setLoading] = useState(true);
......@@ -98,7 +99,10 @@ function ReserveComponent({ publication, copies }) {
const [reservationPublications, setReservationPublications] = useState([]);
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
const { numHolds } = copies[publication.recordId] || {};
const notReservable = notReservableReason(publication, copies);
if (notReservable) {
return <div>{notReservable}</div>;
}
return (
<>
{!isLoggedIn && (
......
......@@ -2,6 +2,7 @@ import { Block } from "@digibib/deichman-ui";
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { toggleFavourites } from "../../store/application";
import ReserveButton from "../PublicationCard/ReserveButton/ReserveButton";
import PublicationList from "../PublicationList";
export default function ReservationSelector({
......@@ -18,10 +19,6 @@ export default function ReservationSelector({
state => state.application.urls.tjenestekatalog
);
const decoratedPublications = publications.map(pub => {
const copyInfo = copies[pub.recordId] || {};
return { ...pub, ...copyInfo };
});
const ReserveComponent = ({ publication, copies }) => {
const { formats, recordId } = publication;
const copiesInfo = copies[publication.recordId] || {};
......@@ -30,7 +27,7 @@ export default function ReservationSelector({
items={copiesInfo.items}
formats={formats}
numHolds={copiesInfo.numHolds}
onClick={() => onClick(recordId)}
onReserve={() => onClick(recordId)}
/>
);
};
......@@ -38,7 +35,8 @@ export default function ReservationSelector({
<Block top={4}>
<h2>Velg en utgave</h2>
<PublicationList
items={decoratedPublications}
publications={publications}
copies={copies}
favourites={favourites}
parent={currentPublication.work}
onReserve={onClick}
......
......@@ -87,7 +87,9 @@ export default function ReservePublicationWidget({
publications
.filter(
pub =>
pub.mediaType === mediaType && showFields.includes("language")
pub.mediaType === mediaType &&
showFields.includes("language") &&
!notReservableReason(pub, copies)
)
.flatMap(({ languages = [] }) =>
languages.map(language => ({
......@@ -109,7 +111,8 @@ export default function ReservePublicationWidget({
pub =>
pub.mediaType === mediaType &&
pub.languages?.includes(language) &&
showFields.includes("format")
showFields.includes("format") &&
!notReservableReason(pub, copies)
)
.flatMap(({ formats = [] }) =>
formats.map(format => ({
......@@ -129,7 +132,9 @@ export default function ReservePublicationWidget({
publications
.filter(
pub =>
pub.mediaType === mediaType && showFields.includes("platform")
pub.mediaType === mediaType &&
showFields.includes("platform") &&
!notReservableReason(pub, copies)
)
.flatMap(({ platforms = [] }) =>
platforms.map(platform => ({
......@@ -142,7 +147,22 @@ export default function ReservePublicationWidget({
)
}
].filter(choice => choice.alternatives.length);
const reasons = Object.keys(
publications
.map(p => notReservableReason(p, copies))
.reduce((reasons, reason) => ({ ...reasons, [reason]: reason }), {})
);
if (reasons.length > 0) {
return (
<div className="reserve-work-widget">
<h3>{title}</h3>
<p>
{reasons.length === 1 && <>{reasons[0]}</>}
{reasons.length > 1 && "Kan ikke bestilles"}
</p>
</div>
);
}
return (
<div className="reserve-work-widget">
<h3>{title}</h3>
......@@ -200,10 +220,34 @@ export default function ReservePublicationWidget({
async function getCopies(publication) {
try {
const res = await fetch(
`/api/resources/publication/${publication.id}/biblio`
`/api/resources/copies?id=${publication.work.publications
.map(p => p.recordId)
.join(",")}`
);
return res.json();
} catch (err) {
console.error(err);
}
}
export function notReservableReason(publication, copies) {
const items = copies[publication.recordId]?.items || [];
const isReservable = items.filter(item => item.reservable).length > 0;
const isOnlyForBranchUse = items.every(item => item.notforloan);
if (
publication.formats.includes(
"http://data.deichman.no/format#MusicalInstrument"
)
) {
return "Kan ikke bestilles";
}
if (!isReservable && !isOnlyForBranchUse) {
return "Kan ikke bestilles";
}
if (isOnlyForBranchUse) {
return "Til bruk i biblioteket";
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment