mirror of
https://github.com/betagouv/mon-entreprise
synced 2025-02-09 05:15:02 +00:00
* Utilisation de la version stable de TypeScript 3.7 * Début de migration du State Redux. Plutôt que de redéfinir les types en doublon par rapport aux actions et reducers, on utilise les valeurs retournées par ces fonctions comme source pour les types globaux. * Modification de tsconfig pour meilleur typage dans VS Code * Meilleur typage de l'environnement : suppression de @types/node qui était trop large (contient tout l'environnement serveur), et remplacement par @types/webpack-env. Par ailleurs typage des variables d'environnement utilisées. * Début de migration de l'économie collaborative * Migration de nombreux composants UI * Mise à jour de dépendances pour récupérer un meilleur typage * Ajout d'un hook pour configurer les simulateurs * Suppression du higher-order component "withSitePaths", on utilise systématiquement le hook useContext. L'essentiel de l'application est maintenant migré, reste le moteur !
120 lines
3.4 KiB
TypeScript
120 lines
3.4 KiB
TypeScript
import { ThemeColoursProvider } from 'Components/utils/withColours'
|
|
import { SitePathProvider, SitePaths } from 'Components/utils/withSitePaths'
|
|
import { TrackerProvider } from 'Components/utils/withTracker'
|
|
import { createBrowserHistory, History } from 'history'
|
|
import { AvailableLangs } from 'i18n'
|
|
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, { RootState } from 'Reducers/rootReducer'
|
|
import { applyMiddleware, compose, createStore, Middleware, Store } from 'redux'
|
|
import thunk from 'redux-thunk'
|
|
import Tracker from 'Tracker'
|
|
import { inIframe } from './utils'
|
|
|
|
declare global {
|
|
interface Window {
|
|
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: any
|
|
}
|
|
}
|
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
|
|
|
|
if (
|
|
process.env.NODE_ENV === 'production' &&
|
|
'serviceWorker' in navigator &&
|
|
!inIframe()
|
|
) {
|
|
window.addEventListener('load', () => {
|
|
navigator.serviceWorker
|
|
.register('/sw.js')
|
|
.then(registration => {
|
|
// eslint-disable-next-line no-console
|
|
console.log('SW registered: ', registration)
|
|
})
|
|
.catch(registrationError => {
|
|
// eslint-disable-next-line no-console
|
|
console.log('SW registration failed: ', registrationError)
|
|
})
|
|
})
|
|
}
|
|
|
|
type ProviderProps = {
|
|
tracker: Tracker
|
|
basename: string
|
|
sitePaths: SitePaths
|
|
language: AvailableLangs
|
|
initialStore: RootState
|
|
onStoreCreated: (store: Store) => void
|
|
reduxMiddlewares: Array<Middleware>
|
|
}
|
|
|
|
export default class Provider extends PureComponent<ProviderProps> {
|
|
history: History
|
|
store: Store
|
|
constructor(props: ProviderProps) {
|
|
super(props)
|
|
this.history = createBrowserHistory({
|
|
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({
|
|
history: this.history,
|
|
sitePaths: this.props.sitePaths
|
|
}),
|
|
...props.reduxMiddlewares
|
|
)
|
|
)
|
|
if (this.props.language) {
|
|
i18next.changeLanguage(this.props.language)
|
|
if (this.props.initialStore)
|
|
this.props.initialStore.lang = this.props.language
|
|
}
|
|
this.store = createStore(reducers, this.props.initialStore, storeEnhancer)
|
|
this.props.onStoreCreated?.(this.store)
|
|
|
|
// Remove loader
|
|
var css = document.createElement('style')
|
|
css.type = 'text/css'
|
|
css.innerHTML = `
|
|
#js {
|
|
animation: appear 0.5s;
|
|
opacity: 1;
|
|
}
|
|
#loading {
|
|
display: none !important;
|
|
}`
|
|
document.body.appendChild(css)
|
|
}
|
|
componentWillUnmount() {
|
|
this.props.tracker.disconnectFromHistory()
|
|
}
|
|
render() {
|
|
const iframeCouleur = new URLSearchParams(
|
|
document?.location.search.substring(1)
|
|
).get('couleur')
|
|
return (
|
|
// If IE < 11 display nothing
|
|
<ReduxProvider store={this.store}>
|
|
<ThemeColoursProvider
|
|
colour={iframeCouleur && decodeURIComponent(iframeCouleur)}
|
|
>
|
|
<TrackerProvider value={this.props.tracker}>
|
|
<SitePathProvider value={this.props.sitePaths}>
|
|
<I18nextProvider i18n={i18next}>
|
|
<Router history={this.history}>
|
|
<>{this.props.children}</>
|
|
</Router>
|
|
</I18nextProvider>
|
|
</SitePathProvider>
|
|
</TrackerProvider>
|
|
</ThemeColoursProvider>
|
|
</ReduxProvider>
|
|
)
|
|
}
|
|
}
|