Files
freeCodeCamp/common/app/App.jsx

168 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-06-17 21:04:28 -07:00
import React, { PropTypes } from 'react';
2016-06-01 15:52:08 -07:00
import { Button, Row } from 'react-bootstrap';
2016-01-27 11:34:44 -08:00
import { connect } from 'react-redux';
import { createSelector } from 'reselect';
import MapDrawer from './components/Map-Drawer.jsx';
2016-03-05 21:06:04 -08:00
import {
fetchUser,
initWindowHeight,
updateNavHeight,
2016-06-03 23:34:28 -07:00
toggleMapDrawer,
toggleMainChat,
updateAppLang,
trackEvent,
loadCurrentChallenge
2016-03-05 21:06:04 -08:00
} from './redux/actions';
2015-06-17 21:04:28 -07:00
2016-06-01 15:52:08 -07:00
import { submitChallenge } from './routes/challenges/redux/actions';
2016-01-06 09:33:55 -08:00
import Nav from './components/Nav';
import Toasts from './toasts/Toasts.jsx';
import { userSelector } from './redux/selectors';
2015-06-17 21:04:28 -07:00
2016-10-28 22:14:39 -07:00
const mapDispatchToProps = {
initWindowHeight,
updateNavHeight,
fetchUser,
submitChallenge,
toggleMapDrawer,
toggleMainChat,
updateAppLang,
trackEvent,
loadCurrentChallenge
};
2016-01-27 11:34:44 -08:00
const mapStateToProps = createSelector(
userSelector,
2016-10-28 22:14:39 -07:00
state => state.app.isSignInAttempted,
2016-06-01 15:52:08 -07:00
state => state.app.toast,
state => state.app.isMapDrawerOpen,
state => state.app.isMapAlreadyLoaded,
2016-06-01 15:52:08 -07:00
state => state.challengesApp.toast,
(
{ user: { username, points, picture } },
2016-10-28 22:14:39 -07:00
isSignInAttempted,
2016-06-01 15:52:08 -07:00
toast,
isMapDrawerOpen,
isMapAlreadyLoaded,
2016-06-01 15:52:08 -07:00
) => ({
2016-01-27 11:34:44 -08:00
username,
points,
picture,
2016-06-01 15:52:08 -07:00
toast,
2016-10-28 22:14:39 -07:00
showLoading: !isSignInAttempted,
isMapDrawerOpen,
isMapAlreadyLoaded,
isSignedIn: !!username
2016-01-27 11:34:44 -08:00
})
);
2016-10-28 22:14:39 -07:00
const propTypes = {
children: PropTypes.node,
username: PropTypes.string,
isSignedIn: PropTypes.bool,
points: PropTypes.number,
picture: PropTypes.string,
toast: PropTypes.object,
updateNavHeight: PropTypes.func,
initWindowHeight: PropTypes.func,
submitChallenge: PropTypes.func,
isMapDrawerOpen: PropTypes.bool,
isMapAlreadyLoaded: PropTypes.bool,
toggleMapDrawer: PropTypes.func,
toggleMainChat: PropTypes.func,
fetchUser: PropTypes.func,
showLoading: PropTypes.bool,
params: PropTypes.object,
updateAppLang: PropTypes.func.isRequired,
trackEvent: PropTypes.func.isRequired,
loadCurrentChallenge: PropTypes.func.isRequired
};
const contextTypes = { router: PropTypes.object };
2016-01-27 11:34:44 -08:00
// export plain class for testing
export class FreeCodeCamp extends React.Component {
componentWillReceiveProps(nextProps) {
if (this.props.params.lang !== nextProps.params.lang) {
this.props.updateAppLang(nextProps.params.lang);
}
}
2016-03-05 21:06:04 -08:00
componentDidMount() {
this.props.initWindowHeight();
if (!this.props.isSignedIn) {
this.props.fetchUser();
}
2016-03-05 21:06:04 -08:00
}
2016-06-01 15:52:08 -07:00
renderChallengeComplete() {
const { submitChallenge } = this.props;
return (
<Button
block={ true }
bsSize='small'
bsStyle='primary'
className='animated fadeIn'
onClick={ submitChallenge }
>
2016-06-01 15:52:08 -07:00
Submit and go to my next challenge
</Button>
);
}
2016-01-27 11:34:44 -08:00
render() {
const { router } = this.context;
const {
username,
points,
picture,
updateNavHeight,
isMapDrawerOpen,
isMapAlreadyLoaded,
2016-06-03 23:34:28 -07:00
toggleMapDrawer,
toggleMainChat,
2016-10-28 22:14:39 -07:00
showLoading,
params: { lang },
trackEvent,
loadCurrentChallenge
} = this.props;
const navProps = {
isOnMap: router.isActive(`/${lang}/map`),
username,
points,
picture,
updateNavHeight,
2016-06-03 23:34:28 -07:00
toggleMapDrawer,
toggleMainChat,
2016-10-28 22:14:39 -07:00
showLoading,
trackEvent,
loadCurrentChallenge
};
2016-01-27 11:34:44 -08:00
return (
<div>
<Nav { ...navProps }/>
<Row>
{ this.props.children }
</Row>
<MapDrawer
isAlreadyLoaded={ isMapAlreadyLoaded }
isOpen={ isMapDrawerOpen }
toggleMapDrawer={ toggleMapDrawer }
/>
<Toasts />
2016-01-27 11:34:44 -08:00
</div>
);
}
}
2016-10-28 22:14:39 -07:00
FreeCodeCamp.displayName = 'FreeCodeCamp';
FreeCodeCamp.contextTypes = contextTypes;
FreeCodeCamp.propTypes = propTypes;
export default connect(
mapStateToProps,
2016-10-28 22:14:39 -07:00
mapDispatchToProps
)(FreeCodeCamp);