2016-02-25 00:45:56 -08:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { loadConfig } from '../actions/config';
|
2016-02-25 12:31:21 -08:00
|
|
|
import { loginUser } from '../actions/auth';
|
|
|
|
import { currentBackend } from '../backends/backend';
|
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>;
|
|
|
|
}
|
|
|
|
|
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-02-25 00:45:56 -08:00
|
|
|
render() {
|
2016-02-25 12:31:21 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-02-25 12:31:21 -08:00
|
|
|
if (user == null) {
|
|
|
|
return this.authenticating();
|
|
|
|
}
|
|
|
|
|
2016-02-25 00:45:56 -08:00
|
|
|
return (
|
|
|
|
<div>{children}</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
2016-05-30 17:13:40 -07:00
|
|
|
const { auth, config } = state;
|
|
|
|
const user = auth && auth.get('user');
|
2016-02-25 12:31:21 -08:00
|
|
|
|
2016-05-30 17:13:40 -07:00
|
|
|
return {auth, config, user};
|
2016-02-25 00:45:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(App);
|