2016-02-25 00:45:56 -08:00
|
|
|
import React from 'react';
|
2016-09-15 11:12:33 +02:00
|
|
|
import pluralize from 'pluralize';
|
2016-09-15 18:56:08 +02:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { Layout, Panel } from 'react-toolbox';
|
2016-02-25 00:45:56 -08:00
|
|
|
import { loadConfig } from '../actions/config';
|
2016-02-25 12:31:21 -08:00
|
|
|
import { loginUser } from '../actions/auth';
|
|
|
|
import { currentBackend } from '../backends/backend';
|
2016-09-15 11:12:33 +02:00
|
|
|
import {
|
|
|
|
SHOW_COLLECTION,
|
|
|
|
CREATE_COLLECTION,
|
|
|
|
HELP,
|
2016-09-16 12:54:26 +02:00
|
|
|
runCommand,
|
2016-09-15 11:12:33 +02:00
|
|
|
createNewEntryInCollection
|
|
|
|
} from '../actions/findbar';
|
2016-09-16 12:54:26 +02:00
|
|
|
import { AppHeader, Loader } from '../components/UI/index';
|
2016-07-14 17:17:18 -03:00
|
|
|
import styles from './App.css';
|
2016-02-25 00:45:56 -08:00
|
|
|
|
|
|
|
class App extends React.Component {
|
2016-09-15 18:56:08 +02:00
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
componentDidMount() {
|
|
|
|
this.props.dispatch(loadConfig());
|
|
|
|
}
|
|
|
|
|
|
|
|
configError(config) {
|
|
|
|
return <div>
|
|
|
|
<h1>Error loading the CMS configuration</h1>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<p>The "config.yml" file could not be loaded or failed to parse properly.</p>
|
|
|
|
<p><strong>Error message:</strong> {config.get('error')}</p>
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
configLoading() {
|
|
|
|
return <div>
|
2016-09-14 18:25:45 -03:00
|
|
|
<Loader active>Loading configuration...</Loader>
|
2016-02-25 00:45:56 -08:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
handleLogin(credentials) {
|
|
|
|
this.props.dispatch(loginUser(credentials));
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticating() {
|
|
|
|
const { auth } = this.props;
|
|
|
|
const backend = currentBackend(this.props.config);
|
|
|
|
|
|
|
|
if (backend == null) {
|
|
|
|
return <div><h1>Waiting for backend...</h1></div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return <div>
|
|
|
|
{React.createElement(backend.authComponent(), {
|
|
|
|
onLogin: this.handleLogin.bind(this),
|
|
|
|
error: auth && auth.get('error'),
|
|
|
|
isFetching: auth && auth.get('isFetching')
|
|
|
|
})}
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2016-07-20 12:15:29 -03:00
|
|
|
generateFindBarCommands() {
|
|
|
|
// Generate command list
|
|
|
|
const commands = [];
|
|
|
|
const defaultCommands = [];
|
|
|
|
|
|
|
|
this.props.collections.forEach(collection => {
|
|
|
|
commands.push({
|
|
|
|
id: `show_${collection.get('name')}`,
|
|
|
|
pattern: `Show ${pluralize(collection.get('label'))}`,
|
|
|
|
type: SHOW_COLLECTION,
|
2016-09-15 18:56:08 +02:00
|
|
|
payload: { collectionName: collection.get('name') }
|
2016-07-20 12:15:29 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (defaultCommands.length < 5) defaultCommands.push(`show_${collection.get('name')}`);
|
|
|
|
|
|
|
|
if (collection.get('create') === true) {
|
|
|
|
commands.push({
|
|
|
|
id: `create_${collection.get('name')}`,
|
|
|
|
pattern: `Create new ${pluralize(collection.get('label'), 1)}(:itemName as ${pluralize(collection.get('label'), 1)} Name)`,
|
|
|
|
type: CREATE_COLLECTION,
|
2016-09-15 18:56:08 +02:00
|
|
|
payload: { collectionName: collection.get('name') }
|
2016-07-20 12:15:29 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
commands.push({ id: HELP, type: HELP, pattern: 'Help' });
|
|
|
|
defaultCommands.push(HELP);
|
|
|
|
|
|
|
|
return { commands, defaultCommands };
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
render() {
|
2016-09-15 11:12:33 +02:00
|
|
|
const { user, config, children, collections } = this.props;
|
2016-02-25 00:45:56 -08:00
|
|
|
|
|
|
|
if (config === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.get('error')) {
|
|
|
|
return this.configError(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.get('isFetching')) {
|
|
|
|
return this.configLoading();
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
if (user == null) {
|
|
|
|
return this.authenticating();
|
|
|
|
}
|
|
|
|
|
2016-07-20 12:15:29 -03:00
|
|
|
const { commands, defaultCommands } = this.generateFindBarCommands();
|
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
return (
|
2016-09-15 18:56:08 +02:00
|
|
|
<Layout>
|
|
|
|
<Panel scrollY>
|
|
|
|
<AppHeader
|
|
|
|
collections={collections}
|
|
|
|
commands={commands}
|
|
|
|
defaultCommands={defaultCommands}
|
2016-09-16 12:54:26 +02:00
|
|
|
runCommand={this.props.runCommand}
|
|
|
|
onCreateEntryClick={this.props.createNewEntryInCollection}
|
2016-09-15 18:56:08 +02:00
|
|
|
/>
|
|
|
|
<div className={`${styles.alignable} ${styles.main}`}>
|
|
|
|
{children}
|
2016-09-15 11:12:33 +02:00
|
|
|
</div>
|
2016-09-15 18:56:08 +02:00
|
|
|
</Panel>
|
|
|
|
</Layout>
|
2016-02-25 00:45:56 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
2016-07-20 12:15:29 -03:00
|
|
|
const { auth, config, collections } = state;
|
2016-05-30 17:13:40 -07:00
|
|
|
const user = auth && auth.get('user');
|
2016-02-25 12:31:21 -08:00
|
|
|
|
2016-07-20 12:15:29 -03:00
|
|
|
return { auth, config, collections, user };
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
|
|
|
|
2016-09-16 12:54:26 +02:00
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
return {
|
|
|
|
dispatch,
|
|
|
|
runCommand: (type, payload) => {
|
|
|
|
dispatch(runCommand(type, payload));
|
|
|
|
},
|
|
|
|
createNewEntryInCollection: (collectionName) => {
|
|
|
|
dispatch(createNewEntryInCollection(collectionName));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(App);
|