diff --git a/src/components/search/advancedSearch/advancedSearchContext.js b/src/components/search/advancedSearch/advancedSearchContext.js index 8b20d0da0..0d6b93fa0 100644 --- a/src/components/search/advancedSearch/advancedSearchContext.js +++ b/src/components/search/advancedSearch/advancedSearchContext.js @@ -238,9 +238,13 @@ export default function AdvancedSearchProvider({ children, router }) { // filter out dropdowns to be removed (they are in url but NOT in on this page) const filteredDropDowns = dropdownSearchIndices.filter(function (el) { - return !!!dropdownsToRemove.find( - (drop) => drop.searchIndex === el.searchIndex - ); + if (dropdownUnits.find((unit) => unit.indexName === el.searchIndex)) { + return true; + } + return false; + // return !!!dropdownsToRemove.find( + // (drop) => drop.searchIndex === el.searchIndex + // ); }); //only add dropdownSearchIndices to object if there are values diff --git a/src/components/search/advancedSearch/advancedSearchDropdown/AdvancedSearchDropdown.js b/src/components/search/advancedSearch/advancedSearchDropdown/AdvancedSearchDropdown.js index ab5a7dbb6..a4bbcbc1e 100644 --- a/src/components/search/advancedSearch/advancedSearchDropdown/AdvancedSearchDropdown.js +++ b/src/components/search/advancedSearch/advancedSearchDropdown/AdvancedSearchDropdown.js @@ -90,7 +90,7 @@ export default function AdvancedSearchDropdown({ menuItems = [], updateIndex, }) { - const { fieldSearchFromUrl } = useAdvancedSearchContext(); + const { fieldSearchFromUrl, workType } = useAdvancedSearchContext(); const [dropdownQuery, setDropdownQuery] = useState(""); @@ -134,7 +134,11 @@ export default function AdvancedSearchDropdown({ type: ToggleMenuItemsEnum.RESET, payload: menuItems, }); - }, [JSON.stringify(fieldSearchFromUrl.dropdownSearchIndices)]); + }, [ + JSON.stringify(fieldSearchFromUrl.dropdownSearchIndices), + workType, + menuItems.length, + ]); const sortedMenuItemsState = [ ...(!isEmpty(dropdownQuery) diff --git a/src/components/search/advancedSearch/advancedSearchHelpers/dropdownReducerFunctions.js b/src/components/search/advancedSearch/advancedSearchHelpers/dropdownReducerFunctions.js index 21a9aef5b..f92e4bda1 100644 --- a/src/components/search/advancedSearch/advancedSearchHelpers/dropdownReducerFunctions.js +++ b/src/components/search/advancedSearch/advancedSearchHelpers/dropdownReducerFunctions.js @@ -116,13 +116,6 @@ export function useMenuItemsState(menuItems, updateIndex) { undefined ); - useEffect(() => { - toggleMenuItemsState({ - type: ToggleMenuItemsEnum.RESET, - payload: menuItems, - }); - }, [menuItems.length]); - useEffect(() => { function resetOnResetSearch() { toggleMenuItemsState({ diff --git a/src/components/search/advancedSearch/advancedSearchHelpers/dummy__default_advanced_search_fields.js b/src/components/search/advancedSearch/advancedSearchHelpers/dummy__default_advanced_search_fields.js index b75186804..b6e6a41d0 100644 --- a/src/components/search/advancedSearch/advancedSearchHelpers/dummy__default_advanced_search_fields.js +++ b/src/components/search/advancedSearch/advancedSearchHelpers/dummy__default_advanced_search_fields.js @@ -3,6 +3,69 @@ import Translate, { getLanguage } from "@/components/base/translate"; import { getFirstMatch } from "@/lib/utils"; import { DropdownIndicesEnum } from "@/components/search/advancedSearch/useDefaultItemsForDropdownUnits"; +// players are outcommented - will be used later - we wait for jed :) +// export function dummy__players() { +// return { +// prioritisedItems: prioritisedPlayers, +// prioritisedFormType: FormTypeEnum.CHECKBOX, +// unprioritisedItems: [], +// unprioritisedFormType: FormTypeEnum.CHECKBOX, +// overrideValueAs: "name", +// }; +// } +// +// const prioritisedPlayers = [ +// { +// term: "For 1 spiller", +// key: "For 1 spiller", +// }, +// { +// term: "For 2 spillere", +// key: "For 2 spillere", +// }, +// { +// term: "For 3-5 spillere", +// key: "For 3-5 spillere", +// }, +// { +// term: "For 6+ spillere", +// key: "For 6+ spillere", +// }, +// ]; + +export function dummy__pegi() { + return { + prioritisedItems: prioritisedPegi, + prioritisedFormType: FormTypeEnum.CHECKBOX, + unprioritisedItems: [], + unprioritisedFormType: FormTypeEnum.CHECKBOX, + overrideValueAs: "name", + }; +} + +const prioritisedPegi = [ + { + term: "Pegi: 3", + key: "Pegi: 3", + }, + { + term: "Pegi: 7", + key: "Pegi: 7", + }, + { + term: "Pegi: 12", + key: "Pegi: 12", + }, + { + term: "Pegi: 16", + key: "Pegi: 16", + }, + { + term: "Pegi: 18", + key: "Pegi: 18", + }, +]; + export function nameForActionLinkContainer(formType, searchIndex) { return `${formType}__${searchIndex}`; } diff --git a/src/components/search/advancedSearch/useDefaultItemsForDropdownUnits.js b/src/components/search/advancedSearch/useDefaultItemsForDropdownUnits.js index df3e7e482..40e1c3300 100644 --- a/src/components/search/advancedSearch/useDefaultItemsForDropdownUnits.js +++ b/src/components/search/advancedSearch/useDefaultItemsForDropdownUnits.js @@ -1,6 +1,7 @@ import { agesFormatterAndComparitor, agesIndices, + dummy__pegi, publicationYearFormatterAndComparitor, publicationYearIndices, } from "@/components/search/advancedSearch/advancedSearchHelpers/dummy__default_advanced_search_fields"; @@ -29,6 +30,188 @@ const specialIndices = new Set([ ]); const specialFormTypes = new Set([FormTypeEnum.ACTION_LINK_CONTAINER]); +const prioritized = { + all: { + MAINLANGUAGES: [ + "dansk", + "engelsk", + "tysk", + "fransk", + "svensk", + "norsk", + "færøsk", + "grønlandsk", + "arabisk", + "ukrainsk", + ], + }, + literature: { + MAINLANGUAGES: [ + "dansk", + "engelsk", + "tysk", + "fransk", + "svensk", + "norsk", + "færøsk", + "grønlandsk", + "arabisk", + "ukrainsk", + ], + MATERIAL_TYPES_SPECIFIC: [ + "bog", + "e-bog", + "lydbog (online)", + "lydbog (cd)", + "lydbog (cd-mp3)", + "billedbog", + "tegneserie", + "graphic novel", + "bog stor skrift", + ], + GENRE: [ + "roman", + "noveller", + "digte", + "biografier", + "krimi", + "fantasy", + "spænding", + "romantik", + "humor", + "strikning", + "opskrifter", + "rejseguides", + ], + }, + article: { + MATERIAL_TYPES_SPECIFIC: ["artikel", "artikel (online)"], + MAINLANGUAGES: [ + "dansk", + "engelsk", + "tysk", + "fransk", + "svensk", + "norsk", + "færøsk", + "grønlandsk", + "arabisk", + "ukrainsk", + ], + GENRE: [ + "kronikker (incl kronik)", + "interviews (incl. interview)", + "essays", + "rejsebeskrivelser", + "erindringer", + "nekrologer (incl. nekrolog)", + "noveller", + "digte (incl. digt(e))", + "tests", + "opskrifter", + ], + }, + movie: { + MATERIAL_TYPES_SPECIFIC: [ + "film (online)", + "film (dvd)", + "film (blu-ray)", + "film (blu-ray 3d)", + "film (blu-ray 4k)", + "tv-serie (online)", + "tv-serie (dvd)", + "tv-serie (blu-ray)", + "tv-serie (blu-ray 4k)", + "musik (dvd)", + "musik (blu-ray)", + ], + GENRE: [ + "tV-serier", + "dokumentarfilm", + "børnefilm", + "tegnefilm", + "drama", + "komedier", + "krimi", + "science fiction", + "actionfilm", + "thriller", + ], + FILMNATIONALITY: [ + "danske film", + "amerikanske film", + "engelske film", + "franske film", + "tyske film", + "italienske film", + "svenske film", + "norske film", + "japanske film", + ], + }, + music: { + MATERIAL_TYPES_SPECIFIC: [ + "musik (cd)", + "musik (grammofonplade)", + "musik (dvd)", + "film (dvd)", + ], + GENRE: [ + "rock", + "jazz", + "pop", + "klassisk musik 1950 ->", + "folk", + "hiphop", + "electronica", + "metal", + "folkemusik", + "country", + "singer/songwriter", + ], + }, + game: { + GAMEPLATFORM: [ + "brætspil", + "playstation 5", + "playstation 4", + "xbox series X", + "xbox one", + "nintendo switch", + ], + GENRE: [ + "shooters", + "actionspil", + "sdventurespil", + "rollespil", + "simulationsspil", + "strategispil", + "racerspil", + "platformsspil", + ], + }, + sheetmusic: { + MAINLANGUAGES: [ + "dansk", + "engelsk", + "tysk", + "fransk", + "svensk", + "norsk", + "færøsk", + "grønlandsk", + "arabisk", + "ukrainsk", + ], + GENERALAUDIENCE: [ + "for begyndere", + "for let ævede", + "for øvede", + "for musikskoler", + "for folkeskolen", + ], + }, +}; /** * If there is a fieldSearch.dropdownSearchIndices, this function * enriches the dropdownUnit with its isSelected @@ -120,13 +303,28 @@ function getFacetsForIndex(data, index) { return facets?.values || []; } -function parseForFacets({ data, isLoading, error, index }) { +function parseForFacets({ data, isLoading, error, index, workType = "all" }) { + const key = Object.keys(DropdownIndicesEnum).find( + (dropdown) => DropdownIndicesEnum[dropdown] === index + ); + const ontop = prioritized[workType]?.[key]?.toReversed() || []; + + let facets = []; + if (ontop) { + facets = getFacetsForIndex(data, index)?.sort( + (a, b) => ontop.indexOf(b.key) - ontop.indexOf(a.key) + ); + } const itemForDropDowns = { prioritisedItems: - isLoading || error ? [] : getFacetsForIndex(data, index)?.slice(0, 3), + isLoading || error ? [] : facets?.slice(0, ontop?.length || 3), prioritisedFormType: FormTypeEnum.CHECKBOX, unprioritisedItems: - isLoading || error ? [] : getFacetsForIndex(data, index), + isLoading || error + ? [] + : facets + ?.slice(ontop?.length || 3, facets?.length) + .sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)), unprioritisedFormType: FormTypeEnum.CHECKBOX, overrideValueAs: "name", }; @@ -165,6 +363,7 @@ export function useDefaultItemsForDropdownUnits({ initDropdowns }, workType) { isLoading, error, index: DropdownIndicesEnum.GENRE, + workType, }) ), indexName: DropdownIndicesEnum.GENRE, @@ -177,6 +376,7 @@ export function useDefaultItemsForDropdownUnits({ initDropdowns }, workType) { isLoading, error, index: DropdownIndicesEnum.MATERIAL_TYPES_SPECIFIC, + workType: workType, }) ), indexName: DropdownIndicesEnum.MATERIAL_TYPES_SPECIFIC, @@ -230,14 +430,7 @@ export function useDefaultItemsForDropdownUnits({ initDropdowns }, workType) { // }; const pegi = { - items: convertToDropdownInput( - parseForFacets({ - data: facetResponse, - isLoading, - error, - index: DropdownIndicesEnum.PEGI, - }) - ), + items: convertToDropdownInput(dummy__pegi()), indexName: DropdownIndicesEnum.PEGI, }; diff --git a/src/components/search/advancedSearch/useDropdownSearchIndices.js b/src/components/search/advancedSearch/useDropdownSearchIndices.js index ee1a4f630..3022acca9 100644 --- a/src/components/search/advancedSearch/useDropdownSearchIndices.js +++ b/src/components/search/advancedSearch/useDropdownSearchIndices.js @@ -60,27 +60,25 @@ export function useDropdownSearchIndices(fieldSearchFromUrl, workType) { workType ); - const [dropdownInitState, setDropdownInitState] = useState([]); - /** @typedef {({ type?: string, payload: DropdownSearchIndex}) => void} UpdateDropdownSearchIndices */ /** @typedef {{value: (Array.|Array.>), searchIndex: string}} DropdownSearchIndex */ const /** @type {[Array., UpdateDropdownSearchIndices]} */ [ dropdownSearchIndices, updateDropdownSearchIndices, - ] = useReducer(dropdownReducer, getDefaultDropdownIndices(), (initState) => - !isEmpty(dropdownInitState) ? dropdownInitState : initState + ] = useReducer( + dropdownReducer, + getDefaultDropdownIndices(), + () => fieldSearchFromUrl.dropdownSearchIndices || [] ); const resetMenuItemsEvent = new Event("resetMenuItemsEvent"); function resetDropdownIndices() { - setDropdownInitState([]); updateDropdownSearchIndices({ type: DropdownReducerEnum.RESET }); } return { dropdownUnits: dropdownUnits, - setDropdownInitState: setDropdownInitState, dropdownSearchIndices: dropdownSearchIndices, updateDropdownSearchIndices: updateDropdownSearchIndices, resetDropdownIndices: resetDropdownIndices,