import React from 'react';
import { connect } from 'react-redux';
import { loadConfig } from '../actions/config';
import { loginUser } from '../actions/auth';
import { currentBackend } from '../backends/backend';
class App extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
this.props.dispatch(loadConfig());
}
componentWillReceiveProps(nextProps) {
//this.props.dispatch(loadBackend());
}
configError(config) {
return
Error loading the CMS configuration
The "config.yml" file could not be loaded or failed to parse properly.
Error message: {config.get('error')}
;
}
configLoading() {
return
Loading configuration...
;
}
handleLogin(credentials) {
this.props.dispatch(loginUser(credentials));
}
authenticating() {
const { auth } = this.props;
const backend = currentBackend(this.props.config);
if (backend == null) {
return Waiting for backend...
;
}
return
{React.createElement(backend.authComponent(), {
onLogin: this.handleLogin.bind(this),
error: auth && auth.get('error'),
isFetching: auth && auth.get('isFetching')
})}
;
}
render() {
const { user, config, children } = this.props;
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();
}
return (
{children}
);
}
}
function mapStateToProps(state) {
const { auth } = state;
return {
auth: auth,
user: auth && auth.get('user'),
config: state.config
};
}
export default connect(mapStateToProps)(App);