Sauvegarde automatiquement l'état de l'application sur infrance

pull/316/head
Johan Girod 2018-08-28 15:59:41 +02:00
parent ec0a76f9ed
commit b29370ec25
8 changed files with 54 additions and 13 deletions

View File

@ -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)
}

View File

@ -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 (
<Provider basename="embauche">
<TrackPageView/>
<Provider
basename="embauche"
initialStore={{
previousSimulation: retrievePersistedSimulation()
}}
onStoreCreated={persistSimulation}>
<TrackPageView />
<Header />
<Switch>
<Route exact path="/" component={Home} />

View File

@ -2,4 +2,5 @@ import React from 'react'
import { render } from 'react-dom'
import App from './App'
let anchor = document.querySelector('#js')
render(<App />, anchor)

View File

@ -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 (
<Provider basename="infrance" language="en">
<Provider
basename="infrance"
language="en"
initialStore={retrievePersistedState()}
onStoreCreated={persistEverything}>
<TrackPageView />
<div id="content">
<Switch>

View File

@ -1,5 +1,7 @@
import React from 'react'
import { render } from 'react-dom'
import App from './App'
let anchor = document.querySelector('#js')
render(<App />, anchor)

View File

@ -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<State, Action>) {
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
}

View File

@ -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'