From 73b0296c3920837848af246bdaa003e4815adb13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Cort=C3=A9s?= Date: Thu, 19 Oct 2023 14:39:57 -0600 Subject: [PATCH] frontend: initialize timeFormat preference (#2821) The `timeZone` preference was not being initialized, as default preferences were initialized as`{}`. This caused some `undefined` issues when trying to access it, this PR initializes the preferences object with the `timeFormat` set at `UTC` ### Testing Performed manual --- .../core/src/Contexts/preferences-context.tsx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/frontend/packages/core/src/Contexts/preferences-context.tsx b/frontend/packages/core/src/Contexts/preferences-context.tsx index c4fb88cd74..b582b93ea6 100644 --- a/frontend/packages/core/src/Contexts/preferences-context.tsx +++ b/frontend/packages/core/src/Contexts/preferences-context.tsx @@ -1,4 +1,5 @@ import React from "react"; +import _ from "lodash"; export type ActionType = "SetPref" | "RemovePref" | "SetLocalPref" | "RemoveLocalPref"; const STORAGE_KEY = "userPreferences"; @@ -6,7 +7,9 @@ type State = { key: string; value?: unknown }; type Action = { type: ActionType; payload: State }; type Dispatch = (action: Action) => void; type UserPreferencesProviderProps = { children: React.ReactNode }; -const DEFAULT_PREFERENCES = {} as State; +const DEFAULT_PREFERENCES: State = { + timeFormat: "UTC", +} as any; interface ContextProps { preferences: State; dispatch: Dispatch; @@ -60,6 +63,12 @@ const UserPreferencesProvider = ({ children }: UserPreferencesProviderProps) => let pref = DEFAULT_PREFERENCES; try { pref = JSON.parse(localStorage.getItem(STORAGE_KEY) || ""); + // If there are any missing default preferences, add them + Object.keys(DEFAULT_PREFERENCES).forEach(key => { + if (_.isEmpty(pref[key]) || !pref[key]) { + pref[key] = DEFAULT_PREFERENCES[key]; + } + }); } catch { localStorage.removeItem(STORAGE_KEY); }