| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | // /**
 | 
					
						
							|  |  |  | //  *
 | 
					
						
							|  |  |  | //  * app.js
 | 
					
						
							|  |  |  | //  *
 | 
					
						
							|  |  |  | //  * Entry point of the application
 | 
					
						
							|  |  |  | //  */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import '@babel/polyfill'; | 
					
						
							| 
									
										
										
										
											2017-08-18 14:17:15 +02:00
										 |  |  | import 'sanitize.css/sanitize.css'; | 
					
						
							| 
									
										
										
										
											2019-04-18 19:54:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Third party css library needed
 | 
					
						
							| 
									
										
										
										
											2019-04-25 14:04:48 +02:00
										 |  |  | // Currently unable to bundle them
 | 
					
						
							| 
									
										
										
										
											2019-04-18 19:54:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import 'react-select/dist/react-select.css'; | 
					
						
							|  |  |  | import 'react-datetime/css/react-datetime.css'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 11:18:40 +02:00
										 |  |  | import './styles/main.scss'; | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import React from 'react'; | 
					
						
							|  |  |  | import ReactDOM from 'react-dom'; | 
					
						
							|  |  |  | import { Provider } from 'react-redux'; | 
					
						
							| 
									
										
										
										
											2019-04-16 11:53:29 +02:00
										 |  |  | import { BrowserRouter } from 'react-router-dom'; | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import { merge } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2018-01-17 12:58:14 +01:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |   freezeApp, | 
					
						
							|  |  |  |   pluginLoaded, | 
					
						
							|  |  |  |   unfreezeApp, | 
					
						
							|  |  |  |   updatePlugin, | 
					
						
							| 
									
										
										
										
											2019-04-12 18:23:26 +02:00
										 |  |  |   getAppPluginsSucceeded, | 
					
						
							| 
									
										
										
										
											2019-02-22 10:05:07 +01:00
										 |  |  | } from './containers/App/actions'; | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | import { showNotification } from './containers/NotificationProvider/actions'; | 
					
						
							| 
									
										
										
										
											2019-04-16 12:54:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import basename from './utils/basename'; | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | import injectReducer from './utils/injectReducer'; | 
					
						
							|  |  |  | import injectSaga from './utils/injectSaga'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Import root component
 | 
					
						
							|  |  |  | import App from './containers/App'; | 
					
						
							|  |  |  | // Import Language provider
 | 
					
						
							|  |  |  | import LanguageProvider from './containers/LanguageProvider'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import configureStore from './configureStore'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Import i18n messages
 | 
					
						
							|  |  |  | import { translationMessages, languages } from './i18n'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Create redux store with history
 | 
					
						
							|  |  |  | import history from './utils/history'; | 
					
						
							| 
									
										
										
										
											2019-04-16 16:55:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import plugins from './plugins'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | const initialState = {}; | 
					
						
							|  |  |  | const store = configureStore(initialState, history); | 
					
						
							|  |  |  | const { dispatch } = store; | 
					
						
							| 
									
										
										
										
											2019-04-23 15:18:57 +02:00
										 |  |  | const MOUNT_NODE = | 
					
						
							|  |  |  |   document.getElementById('app') || document.createElement('div'); | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:55:53 +02:00
										 |  |  | dispatch(getAppPluginsSucceeded(Object.keys(plugins))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Object.keys(plugins).forEach(plugin => { | 
					
						
							|  |  |  |   const currentPlugin = plugins[plugin]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 00:32:21 +02:00
										 |  |  |   const pluginTradsPrefixed = languages.reduce((acc, lang) => { | 
					
						
							|  |  |  |     const currentLocale = currentPlugin.trads[lang]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-18 20:47:57 +02:00
										 |  |  |     if (currentLocale) { | 
					
						
							|  |  |  |       const localeprefixedWithPluginId = Object.keys(currentLocale).reduce( | 
					
						
							|  |  |  |         (acc2, current) => { | 
					
						
							|  |  |  |           acc2[`${plugins[plugin].id}.${current}`] = currentLocale[current]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           return acc2; | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-05-03 16:49:04 +02:00
										 |  |  |         {} | 
					
						
							| 
									
										
										
										
											2019-04-18 20:47:57 +02:00
										 |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       acc[lang] = localeprefixedWithPluginId; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-18 00:32:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return acc; | 
					
						
							|  |  |  |   }, {}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 16:55:53 +02:00
										 |  |  |   try { | 
					
						
							| 
									
										
										
										
											2019-04-18 00:32:21 +02:00
										 |  |  |     merge(translationMessages, pluginTradsPrefixed); | 
					
						
							| 
									
										
										
										
											2019-04-16 16:55:53 +02:00
										 |  |  |     dispatch(pluginLoaded(currentPlugin)); | 
					
						
							|  |  |  |   } catch (err) { | 
					
						
							|  |  |  |     console.log({ err }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2019-04-12 18:23:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | // TODO
 | 
					
						
							|  |  |  | const remoteURL = (() => { | 
					
						
							| 
									
										
										
										
											2019-05-15 12:15:08 +02:00
										 |  |  |     // Relative URL (ex: /dashboard)
 | 
					
						
							| 
									
										
										
										
											2019-04-24 14:32:20 +02:00
										 |  |  |   if (REMOTE_URL[0] === '/') { | 
					
						
							|  |  |  |     return (window.location.origin + REMOTE_URL).replace(/\/$/, ''); | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 14:32:20 +02:00
										 |  |  |   return REMOTE_URL.replace(/\/$/, ''); | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  | })(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const displayNotification = (message, status) => { | 
					
						
							|  |  |  |   dispatch(showNotification(message, status)); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | const lockApp = data => { | 
					
						
							|  |  |  |   dispatch(freezeApp(data)); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | const unlockApp = () => { | 
					
						
							|  |  |  |   dispatch(unfreezeApp()); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | window.strapi = Object.assign(window.strapi || {}, { | 
					
						
							|  |  |  |   node: MODE || 'host', | 
					
						
							|  |  |  |   remoteURL, | 
					
						
							| 
									
										
										
										
											2019-05-13 16:01:16 +02:00
										 |  |  |   backendURL: BACKEND_URL === '/' ? window.location.origin : BACKEND_URL, | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |   notification: { | 
					
						
							|  |  |  |     success: message => { | 
					
						
							|  |  |  |       displayNotification(message, 'success'); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     warning: message => { | 
					
						
							|  |  |  |       displayNotification(message, 'warning'); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     error: message => { | 
					
						
							|  |  |  |       displayNotification(message, 'error'); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     info: message => { | 
					
						
							|  |  |  |       displayNotification(message, 'info'); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   refresh: pluginId => ({ | 
					
						
							|  |  |  |     translationMessages: translationMessagesUpdated => { | 
					
						
							|  |  |  |       render(merge({}, translationMessages, translationMessagesUpdated)); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     leftMenuSections: leftMenuSectionsUpdated => { | 
					
						
							|  |  |  |       store.dispatch( | 
					
						
							| 
									
										
										
										
											2019-05-03 16:49:04 +02:00
										 |  |  |         updatePlugin(pluginId, 'leftMenuSections', leftMenuSectionsUpdated) | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |       ); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }), | 
					
						
							|  |  |  |   router: history, | 
					
						
							|  |  |  |   languages, | 
					
						
							|  |  |  |   currentLanguage: | 
					
						
							|  |  |  |     window.localStorage.getItem('strapi-admin-language') || | 
					
						
							|  |  |  |     window.navigator.language || | 
					
						
							|  |  |  |     window.navigator.userLanguage || | 
					
						
							|  |  |  |     'en', | 
					
						
							|  |  |  |   lockApp, | 
					
						
							|  |  |  |   unlockApp, | 
					
						
							|  |  |  |   injectReducer, | 
					
						
							|  |  |  |   injectSaga, | 
					
						
							|  |  |  |   store, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const render = messages => { | 
					
						
							|  |  |  |   ReactDOM.render( | 
					
						
							|  |  |  |     <Provider store={store}> | 
					
						
							|  |  |  |       <LanguageProvider messages={messages}> | 
					
						
							| 
									
										
										
										
											2019-04-16 11:53:29 +02:00
										 |  |  |         <BrowserRouter basename={basename}> | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |           <App store={store} /> | 
					
						
							| 
									
										
										
										
											2019-04-16 11:53:29 +02:00
										 |  |  |         </BrowserRouter> | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |       </LanguageProvider> | 
					
						
							|  |  |  |     </Provider>, | 
					
						
							| 
									
										
										
										
											2019-05-03 16:49:04 +02:00
										 |  |  |     MOUNT_NODE | 
					
						
							| 
									
										
										
										
											2019-04-11 19:07:00 +02:00
										 |  |  |   ); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (module.hot) { | 
					
						
							|  |  |  |   module.hot.accept(['./i18n', './containers/App'], () => { | 
					
						
							|  |  |  |     ReactDOM.unmountComponentAtNode(MOUNT_NODE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     render(translationMessages); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-23 15:18:57 +02:00
										 |  |  | if (NODE_ENV !== 'test') { | 
					
						
							|  |  |  |   // Chunked polyfill for browsers without Intl support
 | 
					
						
							|  |  |  |   if (!window.Intl) { | 
					
						
							|  |  |  |     new Promise(resolve => { | 
					
						
							|  |  |  |       resolve(import('intl')); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |       .then(() => | 
					
						
							|  |  |  |         Promise.all([ | 
					
						
							|  |  |  |           import('intl/locale-data/jsonp/en.js'), | 
					
						
							|  |  |  |           import('intl/locale-data/jsonp/de.js'), | 
					
						
							| 
									
										
										
										
											2019-05-03 16:49:04 +02:00
										 |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2019-04-23 15:18:57 +02:00
										 |  |  |       ) // eslint-disable-line prettier/prettier
 | 
					
						
							|  |  |  |       .then(() => render(translationMessages)) | 
					
						
							|  |  |  |       .catch(err => { | 
					
						
							|  |  |  |         throw err; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     render(translationMessages); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2017-11-02 19:36:09 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-12 18:23:26 +02:00
										 |  |  | // @Pierre Burgy exporting dispatch for the notifications...
 | 
					
						
							| 
									
										
										
										
											2019-04-02 22:45:21 +02:00
										 |  |  | export { dispatch }; | 
					
						
							| 
									
										
										
										
											2019-04-12 18:23:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // TODO remove this for the new Cypress tests
 | 
					
						
							|  |  |  | if (window.Cypress) { | 
					
						
							|  |  |  |   window.__store__ = Object.assign(window.__store__ || {}, { store }); | 
					
						
							|  |  |  | } |