From 41e44482b8ddcc148f34eeb23f7e78a6ab8040a9 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Sat, 12 Oct 2019 19:12:51 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Supprime=20des=20fonctions=20"ut?= =?UTF-8?q?ils"=20inutiles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ces fonctions n'étaient utilisées qu'une fois ou deux et constituent des indirections inutiles : getIframeOption, parseDataAttributes, setToSessionStorage, getFromSessionStorage et isNumeric. Préférer les fonctions de la "bibliothèque standard": sessionStorage et URLSearchParams. --- source/Provider.js | 7 ++++-- source/i18n.js | 11 ++------- source/sites/mon-entreprise.fr/App.js | 4 +-- source/utils.js | 35 --------------------------- test/mecanisms.test.js | 3 +-- 5 files changed, 10 insertions(+), 50 deletions(-) diff --git a/source/Provider.js b/source/Provider.js index 37b95fa1b..c90109258 100644 --- a/source/Provider.js +++ b/source/Provider.js @@ -10,7 +10,7 @@ import { Router } from 'react-router-dom' import reducers from 'Reducers/rootReducer' import { applyMiddleware, compose, createStore } from 'redux' import thunk from 'redux-thunk' -import { getIframeOption, inIframe } from './utils' +import { inIframe } from './utils' const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose @@ -73,10 +73,13 @@ export default class Provider extends PureComponent { this.props.tracker.disconnectFromHistory() } render() { + const iframeCouleur = new URLSearchParams( + document.location.search.substring(1) + ).get('couleur') return ( // If IE < 11 display nothing - + diff --git a/source/i18n.js b/source/i18n.js index de65ecd54..da2559857 100644 --- a/source/i18n.js +++ b/source/i18n.js @@ -1,20 +1,13 @@ import i18next from 'i18next' import { initReactI18next } from 'react-i18next' import enTranslations from './locales/en.yaml' -import { - getFromSessionStorage, - getIframeOption, - parseDataAttributes, - setToSessionStorage -} from './utils' let lang = - getIframeOption('lang') || new URLSearchParams(document.location.search.substring(1)).get('lang') || - parseDataAttributes(getFromSessionStorage('lang')) || + sessionStorage?.getItem('lang')?.match(/^(fr|en)$/)?.[0] || 'fr' -setToSessionStorage('lang', lang) +sessionStorage?.setItem('lang', lang) i18next .use(initReactI18next) .init({ diff --git a/source/sites/mon-entreprise.fr/App.js b/source/sites/mon-entreprise.fr/App.js index 6d538704e..c42b2fdf7 100644 --- a/source/sites/mon-entreprise.fr/App.js +++ b/source/sites/mon-entreprise.fr/App.js @@ -19,7 +19,7 @@ import { retrievePersistedSimulation } from '../../storage/persistSimulation' import Tracker, { devTracker } from '../../Tracker' -import { inIframe, setToSessionStorage } from '../../utils' +import { inIframe } from '../../utils' import './App.css' import Footer from './layout/Footer/Footer' import { PrivacyContent } from './layout/Footer/Privacy' @@ -59,7 +59,7 @@ const middlewares = [ function InFranceRoute({ basename, language }) { useEffect(() => { - setToSessionStorage('lang', language) + sessionStorage?.setItem('lang', language) }, [language]) const paths = constructLocalizedSitePath(language) const rules = language === 'en' ? baseRulesEn : baseRulesFr diff --git a/source/utils.js b/source/utils.js index d33b6e45b..823f5e895 100644 --- a/source/utils.js +++ b/source/utils.js @@ -4,31 +4,6 @@ import { map } from 'ramda' export let capitalise0 = (name: string) => name && name[0].toUpperCase() + name.slice(1) -export let getUrl = () => - typeof window !== 'undefined' ? window.location.href.toString() : null - -export let parseDataAttributes = (value: any) => - value === 'undefined' - ? undefined - : value === null - ? null - : !isNaN(value) - ? +value - : /* value is a normal string */ - value - -export let getIframeOption = (optionName: string) => { - let url = getUrl(), - hasOption = url?.includes(optionName + '=') - return parseDataAttributes( - hasOption && url.split(optionName + '=')[1].split('&')[0] - ) -} - -export function isNumeric(val: number) { - return Number(parseFloat(val)) === val -} - export function debounce( timeout: number, fn: ArgType => void @@ -94,13 +69,3 @@ export const constructSitePaths = ( sitePaths ) }) - -export const getFromSessionStorage = softCatch(where => { - typeof sessionStorage !== 'undefined' ? sessionStorage[where] : null -}) - -export const setToSessionStorage = softCatch((where, what) => { - if (typeof sessionStorage !== 'undefined') { - sessionStorage[where] = what - } -}) diff --git a/test/mecanisms.test.js b/test/mecanisms.test.js index ddbbd63c9..1a23be7e2 100644 --- a/test/mecanisms.test.js +++ b/test/mecanisms.test.js @@ -10,7 +10,6 @@ import { analyse, parseAll } from '../source/engine/traverse' import { collectMissingVariables } from '../source/engine/generateQuestions' import testSuites from './load-mecanism-tests' import * as R from 'ramda' -import { isNumeric } from '../source/utils' import { serialiseUnit } from 'Engine/units' describe('Mécanismes', () => @@ -43,7 +42,7 @@ describe('Mécanismes', () => missing = collectMissingVariables(analysis.targets), target = analysis.targets[0] - if (isNumeric(valeur)) { + if (typeof valeur === 'number') { expect(target.nodeValue).to.be.closeTo(valeur, 0.001) } else if (valeur !== undefined) { expect(target).to.have.property('nodeValue', valeur)