diff --git a/source/Provider.js b/source/Provider.js index 760368ab6..1d4373d94 100644 --- a/source/Provider.js +++ b/source/Provider.js @@ -11,10 +11,6 @@ import { applyMiddleware, compose, createStore } from 'redux' import thunk from 'redux-thunk' import computeThemeColours from 'Ui/themeColours' import trackDomainActions from './middlewares/trackDomainActions' -import { - persistSimulation, - retrievePersistedSimulation -} from './storage/persist' import ReactPiwik from './Tracker' import { getIframeOption, getUrl, inIframe } from './utils' @@ -47,8 +43,7 @@ if (process.env.NODE_ENV === 'production') { let initialStore = { iframe: getUrl().includes('iframe'), - themeColours: computeThemeColours(getIframeOption('couleur')), - previousSimulation: retrievePersistedSimulation() + themeColours: computeThemeColours(getIframeOption('couleur')) } const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose @@ -83,8 +78,12 @@ export default class Layout extends PureComponent { trackDomainActions(tracker) ) ) - this.store = createStore(reducers, initialStore, storeEnhancer) - persistSimulation(this.store) + this.store = createStore( + reducers, + { ...initialStore, ...this.props.initialStore }, + storeEnhancer + ) + this.props.onStoreCreated(this.store) if (this.props.language) { i18next.changeLanguage(this.props.language) } diff --git a/source/sites/embauche.gouv.fr/App.js b/source/sites/embauche.gouv.fr/App.js index 232befbac..fc1bd7b69 100644 --- a/source/sites/embauche.gouv.fr/App.js +++ b/source/sites/embauche.gouv.fr/App.js @@ -5,6 +5,10 @@ import React, { Component } from 'react' import { Redirect, Route, Switch } from 'react-router-dom' import 'Ui/index.css' import Provider from '../../Provider' +import { + persistSimulation, + retrievePersistedSimulation +} from '../../storage/persistSimulation' import About from './pages/About' import Contact from './pages/Contact' import Contribution from './pages/Contribution' @@ -19,8 +23,13 @@ import RulesList from './pages/RulesList' class EmbaucheRoute extends Component { render() { return ( - - + +
diff --git a/source/sites/embauche.gouv.fr/entry.js b/source/sites/embauche.gouv.fr/entry.js index 2933ac662..ca9564fa0 100644 --- a/source/sites/embauche.gouv.fr/entry.js +++ b/source/sites/embauche.gouv.fr/entry.js @@ -2,4 +2,5 @@ import React from 'react' import { render } from 'react-dom' import App from './App' let anchor = document.querySelector('#js') + render(, anchor) diff --git a/source/sites/mycompanyinfrance.fr/App.js b/source/sites/mycompanyinfrance.fr/App.js index 5c748220c..68437cccc 100644 --- a/source/sites/mycompanyinfrance.fr/App.js +++ b/source/sites/mycompanyinfrance.fr/App.js @@ -3,6 +3,10 @@ import React, { Component } from 'react' import { Route, Switch } from 'react-router-dom' import 'Ui/index.css' import Provider from '../../Provider' +import { + persistEverything, + retrievePersistedState +} from '../../storage/persistEverything' import './App.css' import Landing from './Landing' import CreateMyCompany from './pages/Company' @@ -10,7 +14,6 @@ import Footer from './pages/Footer/Footer' import StepsHeader from './pages/Header/StepsHeader' import HiringProcess from './pages/HiringProcess' import SocialSecurity from './pages/SocialSecurity' - class InFranceRoute extends Component { componentDidMount() { if (typeof sessionStorage !== 'undefined') { @@ -19,7 +22,11 @@ class InFranceRoute extends Component { } render() { return ( - +
diff --git a/source/sites/mycompanyinfrance.fr/entry.js b/source/sites/mycompanyinfrance.fr/entry.js index 2933ac662..a85ed24df 100644 --- a/source/sites/mycompanyinfrance.fr/entry.js +++ b/source/sites/mycompanyinfrance.fr/entry.js @@ -1,5 +1,7 @@ import React from 'react' import { render } from 'react-dom' import App from './App' + let anchor = document.querySelector('#js') + render(, anchor) diff --git a/source/storage/persistEverything.js b/source/storage/persistEverything.js new file mode 100644 index 000000000..acd354573 --- /dev/null +++ b/source/storage/persistEverything.js @@ -0,0 +1,23 @@ +/* @flow */ + +import type { Store } from 'redux' +import { debounce } from '../utils' +import type { State } from 'Types/State' +import type { Action } from 'Types/ActionsTypes' + +const VERSION = 1 + +const LOCAL_STORAGE_KEY = 'mycompanyinfrance::persisted-everything:v' + VERSION + +export function persistEverything(store: Store) { + const listener = () => { + const state = store.getState() + window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(state)) + } + store.subscribe(debounce(1000, listener)) +} + +export function retrievePersistedState(): ?State { + const serializedState = window.localStorage.getItem(LOCAL_STORAGE_KEY) + return serializedState ? JSON.parse(serializedState) : null +} diff --git a/source/storage/persist.js b/source/storage/persistSimulation.js similarity index 92% rename from source/storage/persist.js rename to source/storage/persistSimulation.js index 9b3ab3a14..2766c00c7 100644 --- a/source/storage/persist.js +++ b/source/storage/persistSimulation.js @@ -2,7 +2,7 @@ import type { Store } from 'redux' import { debounce } from '../utils' -import { deserialize, serialize } from './serialize' +import { deserialize, serialize } from './serializeSimulation' import type { State, SavedSimulation } from '../types/State' import type { Action } from 'Types/ActionsTypes' diff --git a/source/storage/serialize.js b/source/storage/serializeSimulation.js similarity index 100% rename from source/storage/serialize.js rename to source/storage/serializeSimulation.js