-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #1014: added parameters for configurate query string
- Loading branch information
1 parent
1996975
commit 3650340
Showing
4 changed files
with
55 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
export function loadQueryString<Value>(): 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<Value>; | ||
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: Value) { | ||
export function saveQueryString<Value>( | ||
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<string, any>); | ||
|
||
params.set("q", JSON.stringify(newValue)); | ||
const url = `${window.location.pathname}?${params.toString()}`; | ||
window.history.replaceState(null, "", url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters