From 3650340034fe300beaeb0a195b00c0cb822d82fc Mon Sep 17 00:00:00 2001 From: lorenzo venneri Date: Thu, 27 Jun 2024 16:50:20 +0200 Subject: [PATCH] Issue #1014: added parameters for configurate query string --- .../src/components/queryString.ts | 39 ++++++++++++++++--- .../src/components/useSelections.tsx | 9 +++-- .../search-frontend/src/embeddable/Main.tsx | 8 ++++ .../search-frontend/src/embeddable/entry.tsx | 7 ++++ 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/js-packages/search-frontend/src/components/queryString.ts b/js-packages/search-frontend/src/components/queryString.ts index 604917e42..637458bda 100644 --- a/js-packages/search-frontend/src/components/queryString.ts +++ b/js-packages/search-frontend/src/components/queryString.ts @@ -1,13 +1,42 @@ -export function loadQueryString(): Value | null { +import { queryStringValues } from "../embeddable/entry"; +import { SearchToken } from "./client"; + +export function loadQueryString< + Value extends { text?: string; textOnChange?: string }, +>(defaultValue: Value): Value { const params = new URLSearchParams(window.location.search); const q = params.get("q"); - if (q) return JSON.parse(q); - return null; + if (q) { + try { + const loadedValue = JSON.parse(q) as Partial; + const combinedValue = { ...defaultValue, ...loadedValue }; + + if (!combinedValue.textOnChange && combinedValue.text) { + combinedValue.textOnChange = combinedValue.text; + } + + return combinedValue; + } catch (error) { + console.error("Error parsing query string:", error); + } + } + return defaultValue; } -export function saveQueryString(value: Value) { +export function saveQueryString( + value: any, + queryStringValues: queryStringValues, +) { const params = new URLSearchParams(window.location.search); - params.set("q", JSON.stringify(value)); + + const newValue = queryStringValues?.reduce((acc, prop) => { + if (prop in value) { + acc[prop] = value[prop]; + } + return acc; + }, {} as Record); + + params.set("q", JSON.stringify(newValue)); const url = `${window.location.pathname}?${params.toString()}`; window.history.replaceState(null, "", url); } diff --git a/js-packages/search-frontend/src/components/useSelections.tsx b/js-packages/search-frontend/src/components/useSelections.tsx index 87e60dbb3..95c76181a 100644 --- a/js-packages/search-frontend/src/components/useSelections.tsx +++ b/js-packages/search-frontend/src/components/useSelections.tsx @@ -7,15 +7,18 @@ import { } from "./client"; import { loadQueryString, saveQueryString } from "./queryString"; import { containsAtLeastOne } from "../embeddable/Main"; +import { queryStringValues } from "../embeddable/entry"; export function useSelections({ useKeycloak = true, useQueryString = true, defaultString = "", + queryStringValues, }: { useKeycloak?: boolean; useQueryString?: boolean; defaultString?: string; + queryStringValues: queryStringValues; }) { const defaultSearch = { text: defaultString, @@ -27,7 +30,7 @@ export function useSelections({ const [state, dispatch] = React.useReducer( reducer, defaultSearch, - () => loadQueryString() ?? defaultSearch, + (defaultSearch) => loadQueryString(defaultSearch), ); const [canSave, setCanSave] = React.useState(false); @@ -45,10 +48,10 @@ export function useSelections({ }, []); React.useEffect(() => { if (useKeycloak && canSave && useQueryString) { - saveQueryString(state); + saveQueryString(state, queryStringValues); } else { if (!useKeycloak && useQueryString) { - saveQueryString(state); + saveQueryString(state, queryStringValues); } } }, [canSave, state]); diff --git a/js-packages/search-frontend/src/embeddable/Main.tsx b/js-packages/search-frontend/src/embeddable/Main.tsx index c36425b33..b33c43d40 100644 --- a/js-packages/search-frontend/src/embeddable/Main.tsx +++ b/js-packages/search-frontend/src/embeddable/Main.tsx @@ -80,6 +80,12 @@ export function Main({ const isHoverChangeDetail = configuration.resultList?.changeOnOver ?? true; const isActiveSkeleton = configuration?.isActiveSkeleton || false; const viewButton = configuration?.viewButton; + const queryStringValues = configuration?.queryStringValues || [ + "text", + "selection", + "textOnChange", + "filters", + ]; const skeletonCustom = { tabs: configuration.skeletonTabsCustom, results: configuration.skeletonResultsCustom, @@ -97,12 +103,14 @@ export function Main({ useKeycloak, useQueryString, defaultString: configuration.defaultString || "", + queryStringValues, }); const [selectionsStateSuggestions, selectionsDispatchSuggestions] = useSelections({ useKeycloak, useQueryString, defaultString: configuration.defaultString || "", + queryStringValues, }); const [sortAfterKey, setSortAfterKey] = React.useState(""); const [totalResult, setTotalResult] = React.useState(null); diff --git a/js-packages/search-frontend/src/embeddable/entry.tsx b/js-packages/search-frontend/src/embeddable/entry.tsx index 3468e6513..1655c7d14 100644 --- a/js-packages/search-frontend/src/embeddable/entry.tsx +++ b/js-packages/search-frontend/src/embeddable/entry.tsx @@ -356,6 +356,7 @@ export type Configuration = { details: Element | string | null; filters: Element | string | null; login: Element | string | null; + queryStringValues: queryStringValues; results: Element | string | null; search: Element | string | null; sortable: Element | string | null; @@ -418,6 +419,7 @@ const defaultConfiguration: Configuration = { memoryResults: null, numberResult: null, numberResultOfFilters: null, + queryStringValues: null, removeFilters: null, resultList: null, resultListPagination: null, @@ -476,3 +478,8 @@ declare global { OpenK9: typeof OpenK9; } } + +export type queryStringValues = + | ("text" | "selection" | "textOnChange" | "filters")[] + | null + | undefined;