Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prioritied items in dropdowns #1486

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/components/search/advancedSearch/advancedSearchContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default function AdvancedSearchDropdown({
menuItems = [],
updateIndex,
}) {
const { fieldSearchFromUrl } = useAdvancedSearchContext();
const { fieldSearchFromUrl, workType } = useAdvancedSearchContext();

const [dropdownQuery, setDropdownQuery] = useState("");

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ export function useMenuItemsState(menuItems, updateIndex) {
undefined
);

useEffect(() => {
toggleMenuItemsState({
type: ToggleMenuItemsEnum.RESET,
payload: menuItems,
});
}, [menuItems.length]);

useEffect(() => {
function resetOnResetSearch() {
toggleMenuItemsState({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
Expand Down
215 changes: 204 additions & 11 deletions src/components/search/advancedSearch/useDefaultItemsForDropdownUnits.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
agesFormatterAndComparitor,
agesIndices,
dummy__pegi,
publicationYearFormatterAndComparitor,
publicationYearIndices,
} from "@/components/search/advancedSearch/advancedSearchHelpers/dummy__default_advanced_search_fields";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
};
Expand Down Expand Up @@ -165,6 +363,7 @@ export function useDefaultItemsForDropdownUnits({ initDropdowns }, workType) {
isLoading,
error,
index: DropdownIndicesEnum.GENRE,
workType,
})
),
indexName: DropdownIndicesEnum.GENRE,
Expand All @@ -177,6 +376,7 @@ export function useDefaultItemsForDropdownUnits({ initDropdowns }, workType) {
isLoading,
error,
index: DropdownIndicesEnum.MATERIAL_TYPES_SPECIFIC,
workType: workType,
})
),
indexName: DropdownIndicesEnum.MATERIAL_TYPES_SPECIFIC,
Expand Down Expand Up @@ -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,
};

Expand Down
Loading