From fa29fd516cda01b1e59ecdbbfd21955cf9067c34 Mon Sep 17 00:00:00 2001 From: Caroline <4971715+carolineBda@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:57:07 +0200 Subject: [PATCH] fix(localStorage error): do not crash if error "Failed to read the 'localStorage' property from 'Window': Access is denied for this document." is thrown (#5999) --- .../CommonSteps/Agreement/store/store.ts | 43 +++++++++++-------- packages/react-ui/src/ThemeProvider.js | 29 +++++-------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts index 066bb770fa..32b91b6190 100644 --- a/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts +++ b/packages/code-du-travail-frontend/src/outils/CommonSteps/Agreement/store/store.ts @@ -4,7 +4,11 @@ import { push as matopush } from "@socialgouv/matomo-next"; import { validateStep } from "./validator"; import { CommonAgreementStoreData, CommonAgreementStoreSlice } from "./types"; -import { STORAGE_KEY_AGREEMENT, StoreSlicePublicode } from "../../../types"; +import { + Agreement, + STORAGE_KEY_AGREEMENT, + StoreSlicePublicode, +} from "../../../types"; import { CommonInformationsStoreSlice } from "../../Informations/store"; import { loadPublicodes } from "../../../api"; import { ValidationResponse } from "../../../Components/SimulatorLayout"; @@ -16,7 +20,6 @@ import { import { pushAgreementEvents } from "../../../common/Agreement"; import { AgreementRoute } from "../../../common/type/WizardType"; import { isCcFullySupportedIndemniteLicenciement } from "../../../CommonIndemniteDepart/common"; -import { Agreement } from "../../../types"; const initialState: Omit< CommonAgreementStoreData, @@ -40,9 +43,7 @@ const createCommonAgreementStore: StoreSlicePublicode< agreementFunction: { onInitAgreementPage: () => { try { - const data = - window.localStorage && - window.localStorage.getItem(STORAGE_KEY_AGREEMENT); + const data = window?.localStorage?.getItem(STORAGE_KEY_AGREEMENT); if (data) { const parsedData: Agreement = JSON.parse(data); if (parsedData?.num !== get().agreementData.input.agreement?.num) { @@ -70,9 +71,7 @@ const createCommonAgreementStore: StoreSlicePublicode< set( produce( (state: CommonAgreementStoreSlice) => { - state.agreementData.input.informationError = isOk - ? false - : true; + state.agreementData.input.informationError = !isOk; } ) ); @@ -85,8 +84,12 @@ const createCommonAgreementStore: StoreSlicePublicode< }, onRouteChange: (value) => { if (value === "not-selected") { - if (window.localStorage) { - window.localStorage.removeItem(STORAGE_KEY_AGREEMENT); + try { + if (window?.localStorage) { + window.localStorage.removeItem(STORAGE_KEY_AGREEMENT); + } + } catch (e) { + console.error(e); } set( produce((state: CommonAgreementStoreSlice) => { @@ -105,19 +108,23 @@ const createCommonAgreementStore: StoreSlicePublicode< const isOk = get().informationsFunction.generatePublicodesQuestions(); set( produce((state: CommonAgreementStoreSlice) => { - state.agreementData.input.informationError = isOk ? false : true; + state.agreementData.input.informationError = !isOk; }) ); }, onAgreementChange: (agreement, enterprise) => { applyGenericValidation(get, set, "agreement", agreement); - if (agreement) { - window.localStorage.setItem( - STORAGE_KEY_AGREEMENT, - JSON.stringify(agreement) - ); - } else { - window.localStorage.removeItem(STORAGE_KEY_AGREEMENT); + try { + if (agreement) { + window?.localStorage?.setItem( + STORAGE_KEY_AGREEMENT, + JSON.stringify(agreement) + ); + } else { + window?.localStorage?.removeItem(STORAGE_KEY_AGREEMENT); + } + } catch (e) { + console.error(e); } applyGenericValidation(get, set, "enterprise", enterprise); const idcc = agreement?.num?.toString(); diff --git a/packages/react-ui/src/ThemeProvider.js b/packages/react-ui/src/ThemeProvider.js index 33bfc49a74..f9efcfa80e 100644 --- a/packages/react-ui/src/ThemeProvider.js +++ b/packages/react-ui/src/ThemeProvider.js @@ -11,29 +11,22 @@ const ThemeContext = React.createContext({ }); const isBlackAndWhiteTheme = () => { - if (typeof window !== "undefined") { - try { - return ( - window.localStorage && - Boolean( - window.localStorage.getItem(BLACK_AND_WHITE_STORAGE_KEY) === "true" - ) - ); - } catch (error) { - return false; - } + try { + return ( + window?.localStorage?.getItem(BLACK_AND_WHITE_STORAGE_KEY) === "true" + ); + } catch (error) { + return false; } - return false; }; const setBlackAndWhiteTheme = (value) => { - if (typeof window !== "undefined") { - try { - window.localStorage && - window.localStorage.setItem(BLACK_AND_WHITE_STORAGE_KEY, value); - } catch (error) { - console.error(error); + try { + if (window?.localStorage) { + window.localStorage.setItem(BLACK_AND_WHITE_STORAGE_KEY, value); } + } catch (error) { + console.error(error); } };