Commit 3f933bc9 authored by David Björkheim's avatar David Björkheim
Browse files

DEICH-5832 Deichman.no: Update publication on filtering

parent c426e20f
......@@ -45,10 +45,9 @@ const availableFields = {
};
function getNumHolds(copies, recordId) {
return copies[recordId]?.numHolds || 0
return copies[recordId]?.numHolds || 0;
}
export default function ReservePublicationWidget({
publications,
currentPublication,
......@@ -56,8 +55,8 @@ export default function ReservePublicationWidget({
}) {
const { languages, formats, platforms } = currentPublication;
const [language, _setLanguage] = useState(languages?.[0]);
const [format, setFormat] = useState(formats?.[0]);
const [platform, setPlatform] = useState(platforms?.[0]);
const [format, _setFormat] = useState(formats?.[0]);
const [platform, _setPlatform] = useState(platforms?.[0]);
const [reservationPublications, setReservationPublications] = useState([]);
const isLoggedIn = useSelector(state => state.auth.isLoggedIn);
const mediaType = currentPublication.mediaType;
......@@ -69,8 +68,8 @@ export default function ReservePublicationWidget({
useEffect(
() => {
_setLanguage(languages?.[0]);
setFormat(formats?.[0]);
setPlatform(platforms?.[0]);
_setFormat(formats?.[0]);
_setPlatform(platforms?.[0]);
},
[languages, formats, platforms]
);
......@@ -81,9 +80,33 @@ export default function ReservePublicationWidget({
router,
currentPublication.work,
mediaType,
format,
platform,
newLanguage
);
};
const setFormat = newFormat => {
_setFormat(newFormat);
navigateToLanguagePublication(
router,
currentPublication.work,
mediaType,
newFormat,
platform,
language
);
};
const setPlatform = newPlatform => {
_setPlatform(newPlatform);
navigateToLanguagePublication(
router,
currentPublication.work,
mediaType,
format,
newPlatform,
language
);
};
const onReserve = choices => {
setReservationPublications(
......@@ -249,12 +272,27 @@ async function navigateToLanguagePublication(
router,
work,
mediaType,
format,
platform,
language
) {
try {
let params = `filter=languages_${TRANSLATIONS[language]}&filter=mediaType_${
TRANSLATIONS[mediaType]
}`;
const filters = [
{
template: input => `languages_${TRANSLATIONS[input]}`,
value: language
},
{
template: input => `mediaType_${TRANSLATIONS[input]}`,
value: mediaType
},
{ template: input => `formats_${TRANSLATIONS[input]}`, value: format },
{ template: input => `platforms_${TRANSLATIONS[input]}`, value: platform }
];
let params = filters
.filter(({ value }) => Boolean(value))
.map(({ template, value }) => `filter=${template(value)}`)
.join("&");
const response = await fetch(
`/api/resources/publicationByWorkUris?${params}`,
{
......@@ -265,15 +303,17 @@ async function navigateToLanguagePublication(
);
const result = await response.json();
const newId = result.hits?.[0]?.id;
const newQuery = { ...router.query };
delete newQuery.publicationId;
if (newId) {
await router.push(
{
pathname: `/utgivelse/[publicationId]`,
query: router.query
query: newQuery
},
{
pathname: `/utgivelse/${newId}`,
query: router.query
query: newQuery
}
);
}
......
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