143 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-08-18 11:41:13 +02:00
/**
* app.js
*
* This is the entry file for the application, only setup and boilerplate
* code.
*/
import 'babel-polyfill';
// Import all the third party stuff
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
2017-08-21 15:12:53 +02:00
import { ConnectedRouter } from 'react-router-redux';
import createHistory from 'history/createBrowserHistory';
2016-09-06 16:59:56 +02:00
import _ from 'lodash';
import 'sanitize.css/sanitize.css';
2017-09-09 15:49:59 +02:00
import 'whatwg-fetch';
2016-08-18 11:41:13 +02:00
import LanguageProvider from 'containers/LanguageProvider';
2017-08-21 15:12:53 +02:00
import App from 'containers/App';
import { showNotification } from 'containers/NotificationProvider/actions';
import { pluginLoaded, updatePlugin } from 'containers/App/actions';
2016-08-18 11:41:13 +02:00
2017-09-09 15:49:59 +02:00
import { plugins } from '../../config/admin.json';
import configureStore from './store';
2017-08-14 15:22:42 +02:00
import { translationMessages, languages } from './i18n';
2016-08-18 11:41:13 +02:00
// Create redux store with history
const initialState = {};
const history = createHistory({
basename: '/admin',
});
2017-08-21 15:12:53 +02:00
const store = configureStore(initialState, history);
2016-08-18 11:41:13 +02:00
const render = (translatedMessages) => {
ReactDOM.render(
<Provider store={store}>
<LanguageProvider messages={translatedMessages}>
2017-08-21 15:12:53 +02:00
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
2016-08-18 11:41:13 +02:00
</LanguageProvider>
</Provider>,
document.getElementById('app')
);
};
2016-08-18 11:47:26 +02:00
2016-08-18 11:41:13 +02:00
// Hot reloadable translation json files
if (module.hot) {
// modules.hot.accept does not accept dynamic dependencies,
// have to be constants at compile-time
module.hot.accept('./i18n', () => {
render(translationMessages);
});
}
// Chunked polyfill for browsers without Intl support
2016-09-09 11:21:54 +02:00
window.onload = function onLoad() {
if (!window.Intl) {
Promise.all([
System.import('intl'),
System.import('intl/locale-data/jsonp/en.js'),
2016-10-13 19:31:29 +02:00
System.import('intl/locale-data/jsonp/fr.js'),
2016-09-09 11:21:54 +02:00
]).then(() => render(translationMessages));
} else {
render(translationMessages);
}
};
2016-08-18 11:41:13 +02:00
2016-08-26 11:05:38 +02:00
/**
* Public Strapi object exposed to the `window` object
*/
/**
* Register a plugin
*
* @param params
*/
2016-09-06 16:59:56 +02:00
const registerPlugin = (plugin) => {
2017-08-14 17:09:34 +02:00
const formattedPlugin = plugin;
2016-10-13 19:31:29 +02:00
// Merge admin translation messages
2017-08-14 17:09:34 +02:00
_.merge(translationMessages, formattedPlugin.translationMessages);
formattedPlugin.leftMenuSections = formattedPlugin.leftMenuSections || [];
2016-09-30 18:25:04 +02:00
2017-08-14 17:09:34 +02:00
store.dispatch(pluginLoaded(formattedPlugin));
2016-08-26 11:05:38 +02:00
};
2016-09-30 18:25:04 +02:00
const displayNotification = (message, status) => {
store.dispatch(showNotification(message, status));
};
2016-10-13 19:31:29 +02:00
const port = window.Strapi && window.Strapi.port ? window.Strapi.port : 1337;
const apiUrl = window.Strapi && window.Strapi.apiUrl ? window.Strapi.apiUrl : `http://localhost:${port}`;
2016-08-26 11:05:38 +02:00
window.Strapi = {
registerPlugin,
2016-09-30 18:25:04 +02:00
notification: {
success: (message) => {
displayNotification(message, 'success');
},
warning: (message) => {
displayNotification(message, 'warning');
},
error: (message) => {
displayNotification(message, 'error');
},
info: (message) => {
displayNotification(message, 'info');
},
2016-10-05 11:32:31 +02:00
},
2016-10-13 19:31:29 +02:00
port,
apiUrl,
2017-03-18 17:34:00 +01:00
refresh: (pluginId) => ({
2016-10-13 19:31:29 +02:00
translationMessages: (translationMessagesUpdated) => {
render(_.merge({}, translationMessages, translationMessagesUpdated));
},
2017-08-14 17:09:34 +02:00
leftMenuSections: (leftMenuSectionsUpdated) => {
store.dispatch(updatePlugin(pluginId, 'leftMenuSections', leftMenuSectionsUpdated));
2017-03-18 17:34:00 +01:00
},
2016-10-13 19:31:29 +02:00
}),
router: history,
2017-08-14 15:22:42 +02:00
languages,
2016-08-26 11:05:38 +02:00
};
2017-09-09 15:49:59 +02:00
// Ping each plugins port defined in configuration
plugins.ports.forEach(pluginPort => {
// Define plugin url
const pluginUrl = `http://localhost:${pluginPort}/main.js`;
// Check that the server in running
fetch(pluginUrl)
.then(() => {
// Inject `script` tag in DOM
const script = window.document.createElement('script');
script.src = pluginUrl;
window.document.body.appendChild(script);
});
});