mon-entreprise/source/Provider.js

79 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

import SetCSSColour from 'Components/utils/SetCssColour'
import { 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 { getIframeOption, inIframe } from './utils'
import { enableBatching } from 'redux-batched-actions'
let initialStore = {
themeColours: computeThemeColours(getIframeOption('couleur'))
}
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
if (
process.env.NODE_ENV === 'production' &&
'serviceWorker' in navigator &&
!inIframe()
) {
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
})
this.props.tracker?.connectToHistory(this.history)
const storeEnhancer = composeEnhancers(
applyMiddleware(
// Allows us to painlessly do route transition in action creators
thunk.withExtraArgument(this.history),
...props.reduxMiddlewares
)
)
this.store = createStore(
enableBatching(reducers),
{ ...initialStore, ...this.props.initialStore },
storeEnhancer
)
this.props.onStoreCreated && this.props.onStoreCreated(this.store)
if (this.props.language) {
i18next.changeLanguage(this.props.language)
}
2018-07-12 08:09:41 +00:00
}
render() {
return (
// If IE < 11 display nothing
<Provider store={this.store}>
<TrackerProvider value={this.props.tracker}>
<SetCSSColour />
2018-07-12 08:09:41 +00:00
<I18nextProvider i18n={i18next}>
2018-11-06 16:18:45 +00:00
<Router history={this.history}>
2018-07-12 08:09:41 +00:00
<>{this.props.children}</>
</Router>
</I18nextProvider>
</TrackerProvider>
</Provider>
)
}
}