2018-02-28 15:45:16 -05:00
|
|
|
import React from 'react';
|
|
|
|
import { render } from 'react-dom';
|
2019-11-14 11:25:04 +02:00
|
|
|
import { Provider, connect } from 'react-redux';
|
2022-09-28 20:04:00 -06:00
|
|
|
import { Router } from 'react-router-dom';
|
2021-05-30 18:49:33 +02:00
|
|
|
import { I18n } from 'react-polyglot';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
2022-09-28 20:04:00 -06:00
|
|
|
import { GlobalStyles } from './ui';
|
|
|
|
import { store } from './store';
|
2021-05-30 18:49:33 +02:00
|
|
|
import { history } from './routing/history';
|
|
|
|
import { loadConfig } from './actions/config';
|
|
|
|
import { authenticateUser } from './actions/auth';
|
|
|
|
import { getPhrases } from './lib/phrases';
|
|
|
|
import { selectLocale } from './reducers/config';
|
|
|
|
import { ErrorBoundary } from './components/UI';
|
|
|
|
import App from './components/App/App';
|
|
|
|
import './components/EditorWidgets';
|
|
|
|
import './mediaLibrary';
|
2018-07-06 18:56:28 -04:00
|
|
|
import 'what-input';
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2018-03-28 15:52:57 -04:00
|
|
|
const ROOT_ID = 'nc-root';
|
2018-03-06 17:14:09 -05:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function TranslatedApp({ locale, config }) {
|
2019-11-14 11:25:04 +02:00
|
|
|
return (
|
|
|
|
<I18n locale={locale} messages={getPhrases(locale)}>
|
2020-03-22 16:04:11 +02:00
|
|
|
<ErrorBoundary showBackup config={config}>
|
2021-03-11 15:40:12 +02:00
|
|
|
<Router history={history}>
|
2022-09-28 20:04:00 -06:00
|
|
|
<App />
|
2021-03-11 15:40:12 +02:00
|
|
|
</Router>
|
2019-11-14 11:25:04 +02:00
|
|
|
</ErrorBoundary>
|
|
|
|
</I18n>
|
|
|
|
);
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2019-11-14 11:25:04 +02:00
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function mapDispatchToProps(state) {
|
2020-03-22 16:04:11 +02:00
|
|
|
return { locale: selectLocale(state.config), config: state.config };
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2019-11-14 11:25:04 +02:00
|
|
|
|
|
|
|
const ConnectedTranslatedApp = connect(mapDispatchToProps)(TranslatedApp);
|
|
|
|
|
2018-03-28 15:52:57 -04:00
|
|
|
function bootstrap(opts = {}) {
|
2018-03-06 17:14:09 -05:00
|
|
|
const { config } = opts;
|
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
/**
|
|
|
|
* Log the version number.
|
|
|
|
*/
|
2022-09-30 06:13:47 -06:00
|
|
|
if (typeof SIMPLE_CMS_CORE_VERSION === 'string') {
|
|
|
|
console.info(`simple-cms-core ${SIMPLE_CMS_CORE_VERSION}`);
|
2018-07-27 14:26:03 -04:00
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
2018-03-28 15:52:57 -04:00
|
|
|
* Get DOM element where app will mount.
|
2018-02-28 15:45:16 -05:00
|
|
|
*/
|
2018-03-28 15:52:57 -04:00
|
|
|
function getRoot() {
|
|
|
|
/**
|
|
|
|
* Return existing root if found.
|
|
|
|
*/
|
|
|
|
const existingRoot = document.getElementById(ROOT_ID);
|
|
|
|
if (existingRoot) {
|
|
|
|
return existingRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If no existing root, create and return a new root.
|
|
|
|
*/
|
|
|
|
const newRoot = document.createElement('div');
|
|
|
|
newRoot.id = ROOT_ID;
|
|
|
|
document.body.appendChild(newRoot);
|
|
|
|
return newRoot;
|
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch config to store if received. This config will be merged into
|
|
|
|
* config.yml if it exists, and any portion that produces a conflict will be
|
|
|
|
* overwritten.
|
|
|
|
*/
|
2021-01-18 11:37:12 +02:00
|
|
|
store.dispatch(
|
|
|
|
loadConfig(config, function onLoad() {
|
|
|
|
store.dispatch(authenticateUser());
|
|
|
|
}),
|
|
|
|
);
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create connected root component.
|
|
|
|
*/
|
2021-02-08 20:01:21 +02:00
|
|
|
function Root() {
|
|
|
|
return (
|
|
|
|
<>
|
2022-09-19 21:15:15 -04:00
|
|
|
<GlobalStyles />
|
2021-02-08 20:01:21 +02:00
|
|
|
<Provider store={store}>
|
|
|
|
<ConnectedTranslatedApp />
|
|
|
|
</Provider>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render application root.
|
|
|
|
*/
|
2018-03-28 15:52:57 -04:00
|
|
|
render(<Root />, getRoot());
|
2018-02-28 15:45:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default bootstrap;
|