From 6d45e1c7c7073a62b831f88189fcadb54f9d4a11 Mon Sep 17 00:00:00 2001 From: Jeroen Reumkens Date: Fri, 30 Sep 2022 14:45:28 +0200 Subject: [PATCH] #4533: Show correct time format on booking page (#4771) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * #4533: Store timeformat preferences in localstorage when changing profile. This way you see the correct format on the booking page. Also abstracted the reuse of the localstorage key for timeformat into a small util function, to prevent typos in the future. * Update apps/web/components/booking/pages/AvailabilityPage.tsx * 4533: Changed 24hfromlocalstorage values from strings to real booleans for better DX. * Update apps/web/lib/clock.ts Co-authored-by: alannnc * Make code shorter by only handling remaining cases Co-authored-by: alannnc * Remove unnecessary boolean check Co-authored-by: alannnc Co-authored-by: Omar López Co-authored-by: alannnc --- .../booking/pages/AvailabilityPage.tsx | 5 ++--- apps/web/lib/clock.ts | 12 ++++++---- apps/web/pages/settings/profile.tsx | 3 ++- apps/web/pages/success.tsx | 4 ++-- .../pages/v2/settings/my-account/general.tsx | 3 +++ apps/web/pages/v2/success.tsx | 4 ++-- .../ee/payments/components/PaymentPage.tsx | 4 ++-- packages/lib/timeFormat.ts | 22 ++++++++++++++----- 8 files changed, 38 insertions(+), 19 deletions(-) diff --git a/apps/web/components/booking/pages/AvailabilityPage.tsx b/apps/web/components/booking/pages/AvailabilityPage.tsx index af43d3b955..6267299791 100644 --- a/apps/web/components/booking/pages/AvailabilityPage.tsx +++ b/apps/web/components/booking/pages/AvailabilityPage.tsx @@ -24,8 +24,7 @@ import useTheme from "@calcom/lib/hooks/useTheme"; import notEmpty from "@calcom/lib/notEmpty"; import { getRecurringFreq } from "@calcom/lib/recurringStrings"; import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry"; -import { detectBrowserTimeFormat } from "@calcom/lib/timeFormat"; -import { localStorage } from "@calcom/lib/webstorage"; +import { detectBrowserTimeFormat, getIs24hClockFromLocalStorage } from "@calcom/lib/timeFormat"; import { trpc } from "@calcom/trpc/react"; import { Icon } from "@calcom/ui/Icon"; import DatePicker from "@calcom/ui/v2/modules/booker/DatePicker"; @@ -225,7 +224,7 @@ function TimezoneDropdown({ const [isTimeOptionsOpen, setIsTimeOptionsOpen] = useState(false); useEffect(() => { - handleToggle24hClock(localStorage.getItem("timeOption.is24hClock") === "true"); + handleToggle24hClock(!!getIs24hClockFromLocalStorage()); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); diff --git a/apps/web/lib/clock.ts b/apps/web/lib/clock.ts index 1fd0b42273..dde33133d6 100644 --- a/apps/web/lib/clock.ts +++ b/apps/web/lib/clock.ts @@ -1,6 +1,10 @@ // handles logic related to user clock display using 24h display / timeZone options. import dayjs from "@calcom/dayjs"; -import { isBrowserLocale24h } from "@calcom/lib/timeFormat"; +import { + getIs24hClockFromLocalStorage, + isBrowserLocale24h, + setIs24hClockInLocalStorage, +} from "@calcom/lib/timeFormat"; import { localStorage } from "@calcom/lib/webstorage"; interface TimeOptions { @@ -20,8 +24,8 @@ const initClock = () => { return; } // This only sets browser locale if there's no preference on localStorage. - if (!localStorage.getItem("timeOption.is24hClock")) set24hClock(isBrowserLocale24h()); - timeOptions.is24hClock = localStorage.getItem("timeOption.is24hClock") === "true"; + if (getIs24hClockFromLocalStorage() === null) set24hClock(isBrowserLocale24h()); + timeOptions.is24hClock = !!getIs24hClockFromLocalStorage(); timeOptions.inviteeTimeZone = localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess(); }; @@ -32,7 +36,7 @@ const is24h = (is24hClock?: boolean) => { }; const set24hClock = (is24hClock: boolean) => { - localStorage.setItem("timeOption.is24hClock", is24hClock.toString()); + setIs24hClockInLocalStorage(is24hClock); timeOptions.is24hClock = is24hClock; }; diff --git a/apps/web/pages/settings/profile.tsx b/apps/web/pages/settings/profile.tsx index dd181b581e..20a65a7337 100644 --- a/apps/web/pages/settings/profile.tsx +++ b/apps/web/pages/settings/profile.tsx @@ -17,6 +17,7 @@ import TimezoneSelect, { ITimezone } from "react-timezone-select"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import showToast from "@calcom/lib/notification"; +import { setIs24hClockInLocalStorage } from "@calcom/lib/timeFormat"; import prisma from "@calcom/prisma"; import { TRPCClientErrorLike } from "@calcom/trpc/client"; import { trpc } from "@calcom/trpc/react"; @@ -229,7 +230,7 @@ function SettingsView(props: ComponentProps & { localeProp: str // Write time format to localStorage if available // Embed isn't applicable to profile pages. So ignore the rule // eslint-disable-next-line @calcom/eslint/avoid-web-storage - window.localStorage.setItem("timeOption.is24hClock", selectedTimeFormat.value === 12 ? "false" : "true"); + setIs24hClockInLocalStorage(selectedTimeFormat.value === 24); // TODO: Add validation diff --git a/apps/web/pages/success.tsx b/apps/web/pages/success.tsx index 3c09ddac3f..d8e9f84117 100644 --- a/apps/web/pages/success.tsx +++ b/apps/web/pages/success.tsx @@ -26,7 +26,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale"; import useTheme from "@calcom/lib/hooks/useTheme"; import { getEveryFreqFor } from "@calcom/lib/recurringStrings"; import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry"; -import { isBrowserLocale24h } from "@calcom/lib/timeFormat"; +import { getIs24hClockFromLocalStorage, isBrowserLocale24h } from "@calcom/lib/timeFormat"; import { localStorage } from "@calcom/lib/webstorage"; import prisma from "@calcom/prisma"; import Button from "@calcom/ui/Button"; @@ -203,7 +203,7 @@ export default function Success(props: SuccessProps) { // TODO: Add payment details }); setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess())); - setIs24h(!!localStorage.getItem("timeOption.is24hClock")); + setIs24h(!!getIs24hClockFromLocalStorage()); // eslint-disable-next-line react-hooks/exhaustive-deps }, [eventType, needsConfirmation]); diff --git a/apps/web/pages/v2/settings/my-account/general.tsx b/apps/web/pages/v2/settings/my-account/general.tsx index de75677cc2..d2881f42dc 100644 --- a/apps/web/pages/v2/settings/my-account/general.tsx +++ b/apps/web/pages/v2/settings/my-account/general.tsx @@ -3,6 +3,7 @@ import { useMemo } from "react"; import { useForm, Controller } from "react-hook-form"; import { useLocale } from "@calcom/lib/hooks/useLocale"; +import { setIs24hClockInLocalStorage } from "@calcom/lib/timeFormat"; import { trpc } from "@calcom/trpc/react"; import { Button } from "@calcom/ui/v2/core/Button"; import Meta from "@calcom/ui/v2/core/Meta"; @@ -110,6 +111,8 @@ const GeneralView = ({ localeProp }: GeneralViewProps) => {
{ + setIs24hClockInLocalStorage(values.timeFormat.value === 24); + mutation.mutate({ ...values, locale: values.locale.value, diff --git a/apps/web/pages/v2/success.tsx b/apps/web/pages/v2/success.tsx index 991b20bed0..fb0d39852b 100644 --- a/apps/web/pages/v2/success.tsx +++ b/apps/web/pages/v2/success.tsx @@ -26,7 +26,7 @@ import { useLocale } from "@calcom/lib/hooks/useLocale"; import useTheme from "@calcom/lib/hooks/useTheme"; import { getEveryFreqFor } from "@calcom/lib/recurringStrings"; import { collectPageParameters, telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry"; -import { isBrowserLocale24h } from "@calcom/lib/timeFormat"; +import { getIs24hClockFromLocalStorage, isBrowserLocale24h } from "@calcom/lib/timeFormat"; import { localStorage } from "@calcom/lib/webstorage"; import prisma from "@calcom/prisma"; import Button from "@calcom/ui/Button"; @@ -201,7 +201,7 @@ export default function Success(props: SuccessProps) { // TODO: Add payment details }); setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess())); - setIs24h(!!localStorage.getItem("timeOption.is24hClock")); + setIs24h(!!getIs24hClockFromLocalStorage()); // eslint-disable-next-line react-hooks/exhaustive-deps }, [eventType, needsConfirmation]); diff --git a/packages/features/ee/payments/components/PaymentPage.tsx b/packages/features/ee/payments/components/PaymentPage.tsx index 6c997063ea..41c8ab6ebf 100644 --- a/packages/features/ee/payments/components/PaymentPage.tsx +++ b/packages/features/ee/payments/components/PaymentPage.tsx @@ -11,7 +11,7 @@ import { sdkActionManager, useIsEmbed } from "@calcom/embed-core/embed-iframe"; import { WEBSITE_URL } from "@calcom/lib/constants"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import useTheme from "@calcom/lib/hooks/useTheme"; -import { isBrowserLocale24h } from "@calcom/lib/timeFormat"; +import { getIs24hClockFromLocalStorage, isBrowserLocale24h } from "@calcom/lib/timeFormat"; import { Icon } from "@calcom/ui/Icon"; import type { PaymentPageProps } from "../pages/payment"; @@ -26,7 +26,7 @@ const PaymentPage: FC = (props) => { useEffect(() => { let embedIframeWidth = 0; setDate(date.tz(localStorage.getItem("timeOption.preferredTimeZone") || dayjs.tz.guess())); - setIs24h(!!localStorage.getItem("timeOption.is24hClock")); + setIs24h(!!getIs24hClockFromLocalStorage()); if (isEmbed) { requestAnimationFrame(function fixStripeIframe() { // HACK: Look for stripe iframe and center position it just above the embed content diff --git a/packages/lib/timeFormat.ts b/packages/lib/timeFormat.ts index 4b8a4e4be6..9b1533c820 100644 --- a/packages/lib/timeFormat.ts +++ b/packages/lib/timeFormat.ts @@ -6,12 +6,24 @@ */ import { localStorage } from "@calcom/lib/webstorage"; +const is24hLocalstorageKey = "timeOption.is24hClock"; + +export const setIs24hClockInLocalStorage = (is24h: boolean) => + localStorage.setItem(is24hLocalstorageKey, is24h.toString()); + +export const getIs24hClockFromLocalStorage = () => { + const is24hFromLocalstorage = localStorage.getItem(is24hLocalstorageKey); + if (is24hFromLocalstorage === null) return null; + + return is24hFromLocalstorage === "true"; +}; + export const isBrowserLocale24h = () => { - const localStorageTimeFormat = localStorage.getItem("timeOption.is24hClock"); + const localStorageTimeFormat = getIs24hClockFromLocalStorage(); // If time format is already stored in the browser then retrieve and return early - if (localStorageTimeFormat === "true") { + if (localStorageTimeFormat === true) { return true; - } else if (localStorageTimeFormat === "false") { + } else if (localStorageTimeFormat === false) { return false; } @@ -19,10 +31,10 @@ export const isBrowserLocale24h = () => { if (typeof window !== "undefined" && navigator) locale = window.navigator?.language; if (!new Intl.DateTimeFormat(locale, { hour: "numeric" }).format(0).match(/M/)) { - localStorage.setItem("timeOption.is24hClock", "false"); + setIs24hClockInLocalStorage(false); return false; } else { - localStorage.setItem("timeOption.is24hClock", "true"); + setIs24hClockInLocalStorage(true); return true; } };