2017-11-10 01:10:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-01-27 19:34:44 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2017-03-13 23:17:07 +00:00
|
|
|
import ns from './ns.json';
|
2016-03-06 05:06:04 +00:00
|
|
|
import {
|
2017-08-01 03:04:01 +00:00
|
|
|
appMounted,
|
2016-03-06 05:06:04 +00:00
|
|
|
fetchUser,
|
2015-06-18 04:04:28 +00:00
|
|
|
|
2017-11-10 01:10:30 +00:00
|
|
|
isSignedInSelector
|
2017-08-01 03:04:01 +00:00
|
|
|
} from './redux';
|
2016-06-01 22:52:08 +00:00
|
|
|
|
2017-08-01 03:04:01 +00:00
|
|
|
import Nav from './Nav';
|
|
|
|
import Toasts from './Toasts';
|
2017-11-10 01:10:30 +00:00
|
|
|
import NotFound from './NotFound';
|
|
|
|
import { mainRouteSelector } from './routes/redux';
|
|
|
|
import Challenges from './routes/Challenges';
|
|
|
|
import Settings from './routes/Settings';
|
2015-06-18 04:04:28 +00:00
|
|
|
|
2016-10-29 05:14:39 +00:00
|
|
|
const mapDispatchToProps = {
|
2017-08-01 03:04:01 +00:00
|
|
|
appMounted,
|
2017-11-10 01:10:30 +00:00
|
|
|
fetchUser
|
2016-07-20 22:06:44 +00:00
|
|
|
};
|
|
|
|
|
2017-08-01 03:04:01 +00:00
|
|
|
const mapStateToProps = state => {
|
2017-11-10 01:10:30 +00:00
|
|
|
const isSignedIn = isSignedInSelector(state);
|
|
|
|
const route = mainRouteSelector(state);
|
2017-08-01 03:04:01 +00:00
|
|
|
return {
|
|
|
|
toast: state.app.toast,
|
2017-11-10 01:10:30 +00:00
|
|
|
isSignedIn,
|
|
|
|
route
|
2017-08-01 03:04:01 +00:00
|
|
|
};
|
|
|
|
};
|
2016-01-27 19:34:44 +00:00
|
|
|
|
2016-10-29 05:14:39 +00:00
|
|
|
const propTypes = {
|
2017-08-01 03:04:01 +00:00
|
|
|
appMounted: PropTypes.func.isRequired,
|
2016-10-29 05:14:39 +00:00
|
|
|
children: PropTypes.node,
|
2017-01-12 06:54:43 +00:00
|
|
|
fetchUser: PropTypes.func,
|
2016-10-29 05:14:39 +00:00
|
|
|
isSignedIn: PropTypes.bool,
|
2017-11-10 01:10:30 +00:00
|
|
|
route: PropTypes.string,
|
|
|
|
toast: PropTypes.object
|
|
|
|
};
|
|
|
|
|
|
|
|
const routes = {
|
|
|
|
challenges: Challenges,
|
|
|
|
settings: Settings
|
2016-10-29 05:14:39 +00:00
|
|
|
};
|
|
|
|
|
2016-01-27 19:34:44 +00:00
|
|
|
// export plain class for testing
|
|
|
|
export class FreeCodeCamp extends React.Component {
|
2016-03-06 05:06:04 +00:00
|
|
|
componentDidMount() {
|
2017-08-01 03:04:01 +00:00
|
|
|
this.props.appMounted();
|
2016-06-20 18:35:19 +00:00
|
|
|
if (!this.props.isSignedIn) {
|
|
|
|
this.props.fetchUser();
|
|
|
|
}
|
2016-03-06 05:06:04 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 19:34:44 +00:00
|
|
|
render() {
|
2017-11-10 01:10:30 +00:00
|
|
|
const {
|
|
|
|
route
|
|
|
|
} = this.props;
|
|
|
|
const Child = routes[route] || NotFound;
|
2017-08-01 03:04:01 +00:00
|
|
|
// we render nav after the content
|
|
|
|
// to allow the panes to update
|
|
|
|
// redux store, which will update the bin
|
|
|
|
// buttons in the nav
|
2016-01-27 19:34:44 +00:00
|
|
|
return (
|
2017-03-13 23:17:07 +00:00
|
|
|
<div className={ `${ns}-container` }>
|
2017-08-01 03:04:01 +00:00
|
|
|
<Nav />
|
2017-11-10 01:10:30 +00:00
|
|
|
<Child />
|
2016-07-06 18:47:16 +00:00
|
|
|
<Toasts />
|
2016-01-27 19:34:44 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 01:35:17 +00:00
|
|
|
FreeCodeCamp.displayName = 'freeCodeCamp';
|
2016-10-29 05:14:39 +00:00
|
|
|
FreeCodeCamp.propTypes = propTypes;
|
|
|
|
|
2016-06-20 18:35:19 +00:00
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
2016-10-29 05:14:39 +00:00
|
|
|
mapDispatchToProps
|
2016-06-20 18:35:19 +00:00
|
|
|
)(FreeCodeCamp);
|