From 9215725dadd95d7c512c3846567c7c0b7042b857 Mon Sep 17 00:00:00 2001 From: Abhijeet Singh Date: Tue, 23 Apr 2024 04:12:54 +0530 Subject: [PATCH] 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 Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: Keith Williams Co-authored-by: Alex van Andel 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 --- packages/features/bookings/Booker/Booker.tsx | 4 ++++ packages/features/bookings/Booker/store.ts | 4 ++-- .../bookings/Booker/utils/query-param.ts | 24 ++++++++++++++----- packages/features/calendars/DatePicker.tsx | 4 +++- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/packages/features/bookings/Booker/Booker.tsx b/packages/features/bookings/Booker/Booker.tsx index f8378fad9e..1fe2c16950 100644 --- a/packages/features/bookings/Booker/Booker.tsx +++ b/packages/features/bookings/Booker/Booker.tsx @@ -140,6 +140,10 @@ const BookerComponent = ({ return setBookerState("booking"); }, [event, selectedDate, selectedTimeslot, setBookerState]); + const slot = getQueryParam("slot"); + useEffect(() => { + setSelectedTimeslot(slot || null); + }, [slot, setSelectedTimeslot]); const EventBooker = useMemo(() => { return bookerState === "booking" ? ( ((set, get) => ({ }); updateQueryParam("month", month); updateQueryParam("date", selectedDate ?? ""); - updateQueryParam("slot", selectedTimeslot ?? ""); + updateQueryParam("slot", selectedTimeslot ?? "", false); } //removeQueryParam("layout"); }, @@ -332,7 +332,7 @@ export const useBookerStore = create((set, get) => ({ selectedTimeslot: getQueryParam("slot") || null, setSelectedTimeslot: (selectedTimeslot: string | null) => { set({ selectedTimeslot }); - updateQueryParam("slot", selectedTimeslot ?? ""); + updateQueryParam("slot", selectedTimeslot ?? "", false); }, formValues: {}, setFormValues: (formValues: Record) => { diff --git a/packages/features/bookings/Booker/utils/query-param.ts b/packages/features/bookings/Booker/utils/query-param.ts index da098e5f7f..8da4d8c59b 100644 --- a/packages/features/bookings/Booker/utils/query-param.ts +++ b/packages/features/bookings/Booker/utils/query-param.ts @@ -1,14 +1,20 @@ -export const updateQueryParam = (param: string, value: string | number) => { +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") { - url.searchParams.delete(param); + removeQueryParam(param, shouldReplace); + return; } else { url.searchParams.set(param, `${value}`); } - - window.history.pushState({}, "", url.href); + 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) => { @@ -17,10 +23,16 @@ export const getQueryParam = (param: string) => { return new URLSearchParams(window.location.search).get(param); }; -export const removeQueryParam = (param: string) => { +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); - window.history.pushState({}, "", url.href); + 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); + } }; diff --git a/packages/features/calendars/DatePicker.tsx b/packages/features/calendars/DatePicker.tsx index 08a4487986..837b9144d6 100644 --- a/packages/features/calendars/DatePicker.tsx +++ b/packages/features/calendars/DatePicker.tsx @@ -235,7 +235,9 @@ const Days = ({ // If selected date not available in the month, select the first available date of the month props.onChange(firstAvailableDateOfTheMonth); } - + if (isSelectedDateAvailable) { + props.onChange(dayjs(selected)); + } if (!firstAvailableDateOfTheMonth) { props.onChange(null); }