92 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-02-25 00:45:56 -08:00
import React from 'react';
import { connect } from 'react-redux';
import { loadConfig } from '../actions/config';
import { loginUser } from '../actions/auth';
import { currentBackend } from '../backends/backend';
2016-07-08 08:58:08 -03:00
import { LIST_POSTS, LIST_FAQ, HELP, MORE_COMMANDS } from '../actions/findbar';
2016-07-08 05:58:23 -03:00
import FindBar from './FindBar';
2016-02-25 00:45:56 -08:00
class App extends React.Component {
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>
<h1>Loading configuration...</h1>
</div>;
}
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-02-25 00:45:56 -08:00
render() {
const { user, config, children } = 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();
}
if (user == null) {
return this.authenticating();
}
2016-02-25 00:45:56 -08:00
return (
2016-07-08 05:58:23 -03:00
<div>
2016-07-08 07:20:20 -03:00
<FindBar commands={[
{ id: LIST_POSTS, pattern: 'List Posts' },
{ id: LIST_FAQ, pattern: 'List FAQs' },
{ id: HELP, pattern: 'Help' },
]} />
2016-07-08 05:58:23 -03:00
{children}
</div>
2016-02-25 00:45:56 -08:00
);
}
}
function mapStateToProps(state) {
2016-05-30 17:13:40 -07:00
const { auth, config } = state;
const user = auth && auth.get('user');
return { auth, config, user };
2016-02-25 00:45:56 -08:00
}
export default connect(mapStateToProps)(App);