2
0
Files
cal/calcom/packages/features/bookings/Booker/utils/query-param.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-09 00:39:27 +02:00
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);
}
};