2019-02-04 09:22:24 +00:00
|
|
|
import SetCSSColour from 'Components/utils/SetCssColour'
|
|
|
|
import { ThemeColoursProvider } from 'Components/utils/withColours'
|
|
|
|
import { SitePathProvider } from 'Components/utils/withSitePaths'
|
|
|
|
import { TrackerProvider } from 'Components/utils/withTracker'
|
|
|
|
import createHistory from 'history/createBrowserHistory'
|
|
|
|
import i18next from 'i18next'
|
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import { I18nextProvider } from 'react-i18next'
|
|
|
|
import { Provider as ReduxProvider } from 'react-redux'
|
|
|
|
import { Router } from 'react-router-dom'
|
|
|
|
import reducers from 'Reducers/rootReducer'
|
|
|
|
import { applyMiddleware, compose, createStore } from 'redux'
|
|
|
|
import { enableBatching } from 'redux-batched-actions'
|
|
|
|
import thunk from 'redux-thunk'
|
|
|
|
import { getIframeOption, inIframe } from './utils'
|
2018-05-24 16:28:37 +00:00
|
|
|
|
2018-05-25 09:46:46 +00:00
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
|
2018-05-24 16:28:37 +00:00
|
|
|
|
2018-08-23 14:42:47 +00:00
|
|
|
if (
|
|
|
|
process.env.NODE_ENV === 'production' &&
|
|
|
|
'serviceWorker' in navigator &&
|
|
|
|
!inIframe()
|
|
|
|
) {
|
2018-07-04 17:45:43 +00:00
|
|
|
window.addEventListener('load', () => {
|
|
|
|
navigator.serviceWorker
|
2018-07-05 09:54:41 +00:00
|
|
|
.register('/sw.js')
|
2018-07-04 17:45:43 +00:00
|
|
|
.then(registration => {
|
|
|
|
console.log('SW registered: ', registration)
|
|
|
|
})
|
|
|
|
.catch(registrationError => {
|
|
|
|
console.log('SW registration failed: ', registrationError)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2018-07-12 08:09:41 +00:00
|
|
|
|
2019-02-04 09:22:24 +00:00
|
|
|
export default class Provider extends PureComponent {
|
2018-07-23 13:21:00 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.history = createHistory({
|
2018-07-12 08:09:41 +00:00
|
|
|
basename: process.env.NODE_ENV === 'production' ? '' : this.props.basename
|
|
|
|
})
|
2018-10-21 17:06:04 +00:00
|
|
|
this.props.tracker?.connectToHistory(this.history)
|
2018-07-23 13:21:00 +00:00
|
|
|
const storeEnhancer = composeEnhancers(
|
|
|
|
applyMiddleware(
|
|
|
|
// Allows us to painlessly do route transition in action creators
|
2019-02-08 11:45:14 +00:00
|
|
|
thunk.withExtraArgument({
|
|
|
|
history: this.history,
|
|
|
|
sitePaths: this.props.sitePaths
|
|
|
|
}),
|
2018-09-20 12:44:24 +00:00
|
|
|
...props.reduxMiddlewares
|
2018-07-23 13:21:00 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-29 16:41:56 +00:00
|
|
|
if (this.props.language) {
|
|
|
|
i18next.changeLanguage(this.props.language)
|
2019-01-15 19:14:06 +00:00
|
|
|
if (this.props.initialStore)
|
|
|
|
this.props.initialStore.lang = this.props.language
|
2018-11-29 16:41:56 +00:00
|
|
|
}
|
2018-08-28 13:59:41 +00:00
|
|
|
this.store = createStore(
|
2018-11-21 17:16:51 +00:00
|
|
|
enableBatching(reducers),
|
2019-01-31 16:24:57 +00:00
|
|
|
this.props.initialStore,
|
2018-08-28 13:59:41 +00:00
|
|
|
storeEnhancer
|
2019-02-04 09:22:24 +00:00
|
|
|
)
|
2018-10-21 17:06:04 +00:00
|
|
|
this.props.onStoreCreated && this.props.onStoreCreated(this.store)
|
2018-07-12 08:09:41 +00:00
|
|
|
}
|
|
|
|
render() {
|
|
|
|
return (
|
2018-09-05 14:18:39 +00:00
|
|
|
// If IE < 11 display nothing
|
2019-02-04 09:22:24 +00:00
|
|
|
<ReduxProvider store={this.store}>
|
|
|
|
<ThemeColoursProvider colour={getIframeOption('couleur')}>
|
|
|
|
<TrackerProvider value={this.props.tracker}>
|
|
|
|
<SitePathProvider value={this.props.sitePaths}>
|
|
|
|
<SetCSSColour />
|
|
|
|
<I18nextProvider i18n={i18next}>
|
|
|
|
<Router history={this.history}>
|
|
|
|
<>{this.props.children}</>
|
|
|
|
</Router>
|
|
|
|
</I18nextProvider>
|
|
|
|
</SitePathProvider>
|
|
|
|
</TrackerProvider>
|
2019-01-31 16:24:57 +00:00
|
|
|
</ThemeColoursProvider>
|
2019-02-04 09:22:24 +00:00
|
|
|
</ReduxProvider>
|
2018-07-12 08:09:41 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|