Skip to content

Commit

Permalink
fix(localStorage error): do not crash if error "Failed to read the 'l…
Browse files Browse the repository at this point in the history
…ocalStorage' property from 'Window': Access is denied for this document." is thrown (#5999)
  • Loading branch information
carolineBda authored Jun 27, 2024
1 parent 931c58c commit fa29fd5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<PublicodesSimulator>,
Expand All @@ -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) {
Expand Down Expand Up @@ -70,9 +71,7 @@ const createCommonAgreementStore: StoreSlicePublicode<
set(
produce(
(state: CommonAgreementStoreSlice<PublicodesSimulator>) => {
state.agreementData.input.informationError = isOk
? false
: true;
state.agreementData.input.informationError = !isOk;
}
)
);
Expand All @@ -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<PublicodesSimulator>) => {
Expand All @@ -105,19 +108,23 @@ const createCommonAgreementStore: StoreSlicePublicode<
const isOk = get().informationsFunction.generatePublicodesQuestions();
set(
produce((state: CommonAgreementStoreSlice<PublicodesSimulator>) => {
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();
Expand Down
29 changes: 11 additions & 18 deletions packages/react-ui/src/ThemeProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down

0 comments on commit fa29fd5

Please sign in to comment.