Commit 743ab69d authored by David Björkheim's avatar David Björkheim
Browse files

DEICH-5558: Remove filters for publicationList

parent a4444e7b
......@@ -4,7 +4,7 @@ import { translations as TRANSLATIONS } from "../../../constants/translations";
import { Flex, Block, Text } from "@digibib/deichman-ui";
const Availability = ({ languages = [], items }) => {
const Availability = ({ languages = [], items = [] }) => {
const availableItems = items.reduce((sum, item) => sum + item.available, 0);
const totalItems = items.reduce((sum, item) => sum + item.total, 0);
......
import React from "react";
import PropTypes from "prop-types";
import {
getActiveFiltersFromUrlLegacy,
getActiveCustomSearchFilters
} from "../../utilities/filters";
import MT_WEIGHTS from "../../constants/mediaTypeWeights";
import LNG_WEIGHTS from "../../constants/languageWeights";
import PublicationCard from "../PublicationCard";
import { translations as TRANSLATIONS } from "../../constants/translations";
import "./styles.css";
......@@ -19,16 +13,10 @@ export default function PublicationList({
ReserveComponent,
tjenesteKatalogUrl,
userCategory,
query,
publications,
copies,
publicationId
copies
}) {
const dispPublications = filterAndSortItems({
query,
publications,
publicationId
});
const dispPublications = sortPublications(publications);
return (
<section className="publication-list">
......@@ -54,7 +42,6 @@ export default function PublicationList({
PublicationList.defaultProps = {
items: [],
query: {},
favourites: [],
limited: false,
userCategory: ""
......@@ -63,7 +50,6 @@ PublicationList.defaultProps = {
PublicationList.propTypes = {
publications: PropTypes.arrayOf(PropTypes.object),
copies: PropTypes.object,
query: PropTypes.object,
favourites: PropTypes.array,
onFavourite: PropTypes.func.isRequired,
ReserveComponent: PropTypes.func.isRequired,
......@@ -74,70 +60,10 @@ PublicationList.propTypes = {
// --------- Utils below ------------------
function ensureArray(obj) {
if (!obj || obj === null) {
return [];
}
if (Array.isArray(obj)) {
return obj;
}
return [obj];
}
function filterAndSortItems({ query, publications, publicationId }) {
const activeFilters = [
...getActiveFiltersFromUrlLegacy(query),
...getActiveCustomSearchFilters(query)
];
const filterValuesByType = activeFilters.reduce((acc, f) => {
if (!acc[f.type]) {
acc[f.type] = [];
}
acc[f.type].push(f.paramValue || f.value);
return acc;
}, {});
const processed = publications.filter(pub => {
// Check if publication matches at least one value for every filter type
const isMatch = Object.keys(filterValuesByType).every(type => {
switch (type) {
case "language":
return ensureArray(pub.languages).some(l =>
filterValuesByType[type].includes(TRANSLATIONS[l])
);
case "branch":
return ensureArray(pub.items).some(i =>
filterValuesByType[type].includes(i.branchcode)
);
case "excludeUnavailable":
return ensureArray(pub.items).some(i => i.available > 0);
case "mediatype":
return [pub.mediaType].some(m =>
filterValuesByType[type].includes(TRANSLATIONS[m])
);
case "format":
return ensureArray(pub.formats).some(f =>
filterValuesByType[type].includes(TRANSLATIONS[f])
);
case "yearFrom":
return pub.publicationYear >= filterValuesByType[type];
case "yearTo":
return pub.publicationYear <= filterValuesByType[type];
default:
return true;
}
});
return isMatch;
});
function sortPublications(processed) {
// Group and sort by MediaType, then by language,
// but always sort selected publication first
const sorted = processed.sort(
(a, b) =>
(a.id === publicationId || 1000) - (b.id === publicationId || 1000) ||
MT_WEIGHTS[a.mediaType] - MT_WEIGHTS[b.mediaType] ||
((a.languages && LNG_WEIGHTS[a.languages[0]]) || 100) -
((b.languages && LNG_WEIGHTS[b.languages[0]]) || 100) ||
b.publicationYear - a.publicationYear
......
import React, { useEffect, useState } from "react";
import { Accordion, Block, Button, Loader } from "@digibib/deichman-ui";
import { useDispatch, useSelector } from "react-redux";
import ActiveFilters from "../ActiveFilters";
import PublicationList from "./PublicationList";
import { useRouter } from "next/router";
import ReservationContainer from "../ReservationsContainer/ReservationContainer";
import LoginButton from "../LoginButton/LoginButton";
......@@ -18,17 +16,11 @@ export default function PublicationsAccordionContainer({ publication }) {
state => state.application.urls.tjenestekatalog
);
const { query } = useRouter();
const getCopies = id => {
return (copies[publication.id] && copies[publication.id][id]) || {};
};
const publications = publication.work.publications.filter(
p => p.mediaType === publication.mediaType && p.id !== publication.id
);
return (
<Block top={8}>
<Block top={8} bottom={8}>
<Accordion text="Andre utgaver" closedByDefault large showDividers>
<>
<LoadDataTrigger
......@@ -44,9 +36,7 @@ export default function PublicationsAccordionContainer({ publication }) {
)}
{!isLoading && publications.length > 0 && (
<Block top={5}>
<ActiveFilters type="publication" />
<PublicationList
query={query}
publications={publications}
copies={copies[publication.id] || {}}
favourites={favourites}
......
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