From 0b46f61a239b4b4db452b6980c482628ba1fff97 Mon Sep 17 00:00:00 2001 From: sean-brydon <55134778+sean-brydon@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:16:02 +0100 Subject: [PATCH] feat: (Overlay) Persist toggle option (#11961) --- .../Booker/components/LargeCalendar.tsx | 3 +- .../OverlayCalendarContainer.tsx | 174 ++++++++++-------- .../bookings/components/AvailableTimes.tsx | 3 +- 3 files changed, 105 insertions(+), 75 deletions(-) diff --git a/packages/features/bookings/Booker/components/LargeCalendar.tsx b/packages/features/bookings/Booker/components/LargeCalendar.tsx index 86fd16996a..f71dfe113e 100644 --- a/packages/features/bookings/Booker/components/LargeCalendar.tsx +++ b/packages/features/bookings/Booker/components/LargeCalendar.tsx @@ -19,7 +19,8 @@ export const LargeCalendar = ({ extraDays }: { extraDays: number }) => { const schedule = useScheduleForEvent({ prefetchNextMonth: !!extraDays && dayjs(date).month() !== dayjs(date).add(extraDays, "day").month(), }); - const displayOverlay = getQueryParam("overlayCalendar") === "true"; + const displayOverlay = + getQueryParam("overlayCalendar") === "true" || localStorage.getItem("overlayCalendarSwitchDefault"); const event = useEvent(); const eventDuration = selectedEventDuration || event?.data?.length || 30; diff --git a/packages/features/bookings/Booker/components/OverlayCalendar/OverlayCalendarContainer.tsx b/packages/features/bookings/Booker/components/OverlayCalendar/OverlayCalendarContainer.tsx index a44543f67c..f0a521cea0 100644 --- a/packages/features/bookings/Booker/components/OverlayCalendar/OverlayCalendarContainer.tsx +++ b/packages/features/bookings/Booker/components/OverlayCalendar/OverlayCalendarContainer.tsx @@ -1,5 +1,5 @@ import { useSession } from "next-auth/react"; -import { useRouter, useSearchParams, usePathname } from "next/navigation"; +import { useSearchParams, useRouter, usePathname } from "next/navigation"; import { useState, useCallback, useEffect } from "react"; import dayjs from "@calcom/dayjs"; @@ -17,19 +17,108 @@ import { OverlayCalendarSettingsModal } from "../OverlayCalendar/OverlayCalendar import { useLocalSet } from "../hooks/useLocalSet"; import { useOverlayCalendarStore } from "./store"; -export function OverlayCalendarContainer() { +interface OverlayCalendarSwitchProps { + setContinueWithProvider: (val: boolean) => void; + setCalendarSettingsOverlay: (val: boolean) => void; + enabled?: boolean; +} + +function OverlayCalendarSwitch({ + setCalendarSettingsOverlay, + setContinueWithProvider, + enabled, +}: OverlayCalendarSwitchProps) { const { t } = useLocale(); + const layout = useBookerStore((state) => state.layout); + const searchParams = useSearchParams(); + const { data: session } = useSession(); + const router = useRouter(); + const pathname = usePathname(); + const switchEnabled = enabled; + + // Toggle query param for overlay calendar + const toggleOverlayCalendarQueryParam = useCallback( + (state: boolean) => { + const current = new URLSearchParams(Array.from(searchParams.entries())); + if (state) { + current.set("overlayCalendar", "true"); + localStorage.setItem("overlayCalendarSwitchDefault", "true"); + } else { + current.delete("overlayCalendar"); + localStorage.removeItem("overlayCalendarSwitchDefault"); + } + // cast to string + const value = current.toString(); + const query = value ? `?${value}` : ""; + router.push(`${pathname}${query}`); + }, + [searchParams, pathname, router] + ); + + /** + * If a user is not logged in and the overlay calendar query param is true, + * show the continue modal so they can login / create an account + */ + useEffect(() => { + if (!session && switchEnabled) { + toggleOverlayCalendarQueryParam(false); + setContinueWithProvider(true); + } + }, [session, switchEnabled, setContinueWithProvider, toggleOverlayCalendarQueryParam]); + + return ( +