Files
calendar/packages/features/bookings/Booker/utils/query-param.ts
T
9215725dad fix: browser back button not working (#13345)
* 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 <peeroke@gmail.com>
Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Alex van Andel <me@alexvanandel.com>
Co-authored-by: Carina Wollendorfer <30310907+CarinaWolli@users.noreply.github.com>
Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com>
Co-authored-by: Udit Takkar <udit222001@gmail.com>
2024-04-22 18:42:54 -04:00

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);
}
};