freeCodeCamp/common/app/App.jsx

187 lines
4.1 KiB
JavaScript
Raw Normal View History

2015-06-18 04:04:28 +00:00
import React, { PropTypes } from 'react';
2016-06-01 22:52:08 +00:00
import { Button, Row } from 'react-bootstrap';
2016-01-07 22:51:41 +00:00
import { ToastMessage, ToastContainer } from 'react-toastr';
2016-01-27 19:34:44 +00:00
import { compose } from 'redux';
import { connect } from 'react-redux';
2016-05-04 23:46:19 +00:00
import { contain } from 'redux-epic';
2016-01-27 19:34:44 +00:00
import { createSelector } from 'reselect';
import MapDrawer from './components/Map-Drawer.jsx';
2016-03-06 05:06:04 +00:00
import {
fetchUser,
initWindowHeight,
updateNavHeight,
2016-06-04 06:34:28 +00:00
toggleMapDrawer,
toggleMainChat
2016-03-06 05:06:04 +00:00
} from './redux/actions';
2015-06-18 04:04:28 +00:00
2016-06-01 22:52:08 +00:00
import { submitChallenge } from './routes/challenges/redux/actions';
2016-01-06 17:33:55 +00:00
import Nav from './components/Nav';
2016-06-01 22:52:08 +00:00
import { randomCompliment } from './utils/get-words';
2015-06-18 04:04:28 +00:00
2016-01-07 22:51:41 +00:00
const toastMessageFactory = React.createFactory(ToastMessage.animation);
2016-01-27 19:34:44 +00:00
const mapStateToProps = createSelector(
2016-06-01 22:52:08 +00:00
state => state.app.username,
state => state.app.points,
state => state.app.picture,
state => state.app.toast,
state => state.app.isMapDrawerOpen,
state => state.app.isMapAlreadyLoaded,
2016-06-01 22:52:08 +00:00
state => state.challengesApp.toast,
(
2016-01-27 19:34:44 +00:00
username,
points,
picture,
2016-06-01 22:52:08 +00:00
toast,
isMapDrawerOpen,
isMapAlreadyLoaded,
2016-06-01 22:52:08 +00:00
showChallengeComplete
) => ({
2016-01-27 19:34:44 +00:00
username,
points,
picture,
2016-06-01 22:52:08 +00:00
toast,
isMapDrawerOpen,
isMapAlreadyLoaded,
2016-06-01 22:52:08 +00:00
showChallengeComplete
2016-01-27 19:34:44 +00:00
})
);
2016-06-01 22:52:08 +00:00
const bindableActions = {
initWindowHeight,
updateNavHeight,
fetchUser,
submitChallenge,
2016-06-04 06:34:28 +00:00
toggleMapDrawer,
toggleMainChat
2016-06-01 22:52:08 +00:00
};
2016-01-27 19:34:44 +00:00
const fetchContainerOptions = {
fetchAction: 'fetchUser',
isPrimed({ username }) {
return !!username;
}
};
2015-06-18 04:04:28 +00:00
2016-01-27 19:34:44 +00:00
// export plain class for testing
export class FreeCodeCamp extends React.Component {
static displayName = 'FreeCodeCamp';
2016-01-07 22:51:41 +00:00
2016-01-27 19:34:44 +00:00
static propTypes = {
children: PropTypes.node,
username: PropTypes.string,
points: PropTypes.number,
picture: PropTypes.string,
2016-03-06 05:06:04 +00:00
toast: PropTypes.object,
updateNavHeight: PropTypes.func,
2016-06-01 22:52:08 +00:00
initWindowHeight: PropTypes.func,
showChallengeComplete: PropTypes.number,
submitChallenge: PropTypes.func,
isMapDrawerOpen: PropTypes.bool,
isMapAlreadyLoaded: PropTypes.bool,
2016-06-04 06:34:28 +00:00
toggleMapDrawer: PropTypes.func,
toggleMainChat: PropTypes.func
2016-01-27 19:34:44 +00:00
};
2016-06-01 22:52:08 +00:00
componentWillReceiveProps({
toast: nextToast = {},
showChallengeComplete: nextCC = 0
}) {
const {
toast = {},
showChallengeComplete
} = this.props;
2016-01-27 19:34:44 +00:00
if (toast.id !== nextToast.id) {
this.refs.toaster[nextToast.type || 'success'](
nextToast.message,
nextToast.title,
{
closeButton: true,
timeOut: 10000
}
);
}
2016-06-01 22:52:08 +00:00
if (nextCC !== showChallengeComplete) {
this.refs.toaster.success(
this.renderChallengeComplete(),
randomCompliment(),
{
closeButton: true,
timeOut: 0,
extendedTimeOut: 0
}
);
}
2016-01-27 19:34:44 +00:00
}
2016-03-06 05:06:04 +00:00
componentDidMount() {
this.props.initWindowHeight();
2016-03-06 05:06:04 +00:00
}
2016-06-01 22:52:08 +00:00
renderChallengeComplete() {
const { submitChallenge } = this.props;
return (
<Button
block={ true }
bsSize='small'
bsStyle='primary'
className='animated fadeIn'
onClick={ submitChallenge }
>
Submit and go to my next challenge
</Button>
);
}
2016-01-27 19:34:44 +00:00
render() {
const {
username,
points,
picture,
updateNavHeight,
isMapDrawerOpen,
isMapAlreadyLoaded,
2016-06-04 06:34:28 +00:00
toggleMapDrawer,
toggleMainChat
} = this.props;
const navProps = {
username,
points,
picture,
updateNavHeight,
2016-06-04 06:34:28 +00:00
toggleMapDrawer,
toggleMainChat
};
2016-01-27 19:34:44 +00:00
return (
<div>
<Nav { ...navProps }/>
<Row>
{ this.props.children }
</Row>
<MapDrawer
isAlreadyLoaded={ isMapAlreadyLoaded }
isOpen={ isMapDrawerOpen }
toggleMapDrawer={ toggleMapDrawer }
/>
2016-01-27 19:34:44 +00:00
<ToastContainer
className='toast-bottom-right'
ref='toaster'
toastMessageFactory={ toastMessageFactory } />
</div>
);
}
}
const wrapComponent = compose(
// connect Component to Redux Store
2016-06-01 22:52:08 +00:00
connect(mapStateToProps, bindableActions),
2016-01-27 19:34:44 +00:00
// handles prefetching data
contain(fetchContainerOptions)
);
2016-01-27 19:34:44 +00:00
export default wrapComponent(FreeCodeCamp);