mirror of
https://github.com/hanzoai/calendar.git
synced 2026-09-24 22:42:24 +00:00
* fixed the browser back button not woking * fix back button * added shouldReplace param to query-params helper. * revert yarn.lock * fix: test --------- Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Udit Takkar <[email protected]> Co-authored-by: Keith Williams <[email protected]> Co-authored-by: Alex van Andel <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: Joe Au-Yeung <[email protected]> Co-authored-by: Udit Takkar <[email protected]>
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
export const updateQueryParam = (param: string, value: string | number, shouldReplace = true) => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
const url = new URL(window.location.href);
|
|
if (url.searchParams.get(param) === value) return;
|
|
|
|
if (value === "" || value === "null") {
|
|
removeQueryParam(param, shouldReplace);
|
|
return;
|
|
} else {
|
|
url.searchParams.set(param, `${value}`);
|
|
}
|
|
if (shouldReplace) {
|
|
window.history.replaceState({ ...window.history.state, as: url.href }, "", url.href);
|
|
} else {
|
|
window.history.pushState({ ...window.history.state, as: url.href }, "", url.href);
|
|
}
|
|
};
|
|
|
|
export const getQueryParam = (param: string) => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
return new URLSearchParams(window.location.search).get(param);
|
|
};
|
|
|
|
export const removeQueryParam = (param: string, shouldReplace = true) => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
const url = new URL(window.location.href);
|
|
if (!url.searchParams.get(param)) return;
|
|
|
|
url.searchParams.delete(param);
|
|
if (shouldReplace) {
|
|
window.history.replaceState({ ...window.history.state, as: url.href }, "", url.href);
|
|
} else {
|
|
window.history.pushState({ ...window.history.state, as: url.href }, "", url.href);
|
|
}
|
|
};
|