Commit 9a34bd7e authored by David Björkheim's avatar David Björkheim
Browse files

DEICH-5558 Deichman.no: fix condition + better loading for reserve widget

parent 5aad36cb
......@@ -51,6 +51,7 @@ export default function ReservePublicationWidget({
const [platform, setPlatform] = useState(platforms?.[0]);
const [copies, setCopies] = useState({});
const [isLoading, setLoading] = useState(true);
const [reservationPublications, setReservationPublications] = useState([]);
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
......@@ -60,9 +61,13 @@ export default function ReservePublicationWidget({
useEffect(
() => {
let cancelUpdate = false;
getCopies(currentPublication).then(
newCopies => !cancelUpdate && setCopies(newCopies)
);
setLoading(true);
getCopies(currentPublication).then(newCopies => {
if (!cancelUpdate) {
setCopies(newCopies);
setLoading(false);
}
});
return () => (cancelUpdate = true);
},
[currentPublication]
......@@ -152,8 +157,12 @@ export default function ReservePublicationWidget({
.map(p => notReservableReason(p, copies))
.reduce((reasons, reason) => ({ ...reasons, [reason]: reason }), {})
);
if (reasons.length > 0) {
if (isLoading) {
return null;
}
if (publications.every(p => notReservableReason(p, copies))) {
return (
<div data-animate-in="top" data-animation-order="4">
<div className="reserve-work-widget">
<h3>{title}</h3>
<p>
......@@ -161,9 +170,11 @@ export default function ReservePublicationWidget({
{reasons.length > 1 && "Kan ikke bestilles"}
</p>
</div>
</div>
);
}
return (
<div data-animate-in="top" data-animation-order="4">
<div className="reserve-work-widget">
<h3>{title}</h3>
{!isLoggedIn && (
......@@ -214,6 +225,7 @@ export default function ReservePublicationWidget({
</PublicationDetailsButton>
</Block>
</div>
</div>
);
}
......@@ -236,7 +248,7 @@ export function notReservableReason(publication, copies) {
const isOnlyForBranchUse = items.every(item => item.notforloan);
if (
publication.formats.includes(
publication.formats?.includes(
"http://data.deichman.no/format#MusicalInstrument"
)
) {
......
......@@ -120,12 +120,10 @@ const WorkHero = ({
</Block>
<Block top={6} responsive>
<div data-animate-in="top" data-animation-order="4">
<ReservePublicationWidget
publications={publications}
currentPublication={publicationData}
/>
</div>
</Block>
</div>
</GridItem>
......
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