mon-entreprise/source/Provider.js

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-07-12 08:09:41 +00:00
import { defaultTracker, TrackerProvider } from 'Components/utils/withTracker'
2018-06-21 09:52:00 +00:00
import createHistory from 'history/createBrowserHistory'
import i18next from 'i18next'
2018-07-12 08:09:41 +00:00
import React, { PureComponent } from 'react'
2018-06-21 09:52:00 +00:00
import { I18nextProvider } from 'react-i18next'
import { Provider } from 'react-redux'
2018-06-21 09:52:00 +00:00
import { Router } from 'react-router-dom'
2018-07-12 08:09:41 +00:00
import reducers from 'Reducers/rootReducer'
import { applyMiddleware, compose, createStore } from 'redux'
import thunk from 'redux-thunk'
2018-07-12 08:09:41 +00:00
import computeThemeColours from 'Ui/themeColours'
import trackDomainActions from './middlewares/trackDomainActions'
import {
persistSimulation,
retrievePersistedSimulation
} from './storage/persist'
2018-07-12 08:09:41 +00:00
import ReactPiwik from './Tracker'
import { getIframeOption, getUrl } from './utils'
let tracker = defaultTracker
if (process.env.NODE_ENV === 'production') {
tracker = new ReactPiwik({
url: 'stats.data.gouv.fr',
siteId: 39,
trackErrors: true
})
}
2018-07-12 08:09:41 +00:00
if (process.env.NODE_ENV === 'production') {
let integratorUrl = getIframeOption('integratorUrl')
ReactPiwik.push([
'setCustomVariable',
1,
'urlPartenaire',
2018-07-12 08:09:41 +00:00
decodeURIComponent(integratorUrl || location.origin),
'visit'
])
}
let initialStore = {
iframe: getUrl().includes('iframe'),
themeColours: computeThemeColours(getIframeOption('couleur')),
previousSimulation: retrievePersistedSimulation()
}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
2018-07-12 08:09:41 +00:00
if (process.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker
2018-07-05 09:54:41 +00:00
.register('/sw.js')
.then(registration => {
console.log('SW registered: ', registration)
})
.catch(registrationError => {
console.log('SW registration failed: ', registrationError)
})
})
}
2018-07-12 08:09:41 +00:00
export default class Layout extends PureComponent {
constructor(props) {
super(props)
this.history = createHistory({
2018-07-12 08:09:41 +00:00
basename: process.env.NODE_ENV === 'production' ? '' : this.props.basename
})
const storeEnhancer = composeEnhancers(
applyMiddleware(
// Allows us to painlessly do route transition in action creators
thunk.withExtraArgument(this.history),
trackDomainActions(tracker)
)
)
this.store = createStore(reducers, initialStore, storeEnhancer)
persistSimulation(this.store)
2018-07-12 08:09:41 +00:00
}
render() {
return (
<Provider store={this.store}>
2018-07-12 08:09:41 +00:00
<TrackerProvider value={tracker}>
<I18nextProvider i18n={i18next}>
<Router history={tracker.connectToHistory(this.history)}>
2018-07-12 08:09:41 +00:00
<>{this.props.children}</>
</Router>
</I18nextProvider>
</TrackerProvider>
</Provider>
)
}
}