Files
freeCodeCamp/common/app/App.jsx

151 lines
3.3 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-07 14:51:41 -08:00
import { ToastMessage, ToastContainer } from 'react-toastr';
2016-01-27 11:34:44 -08:00
import { compose } from 'redux';
import { connect } from 'react-redux';
2016-05-04 16:46:19 -07:00
import { contain } from 'redux-epic';
2016-01-27 11:34:44 -08:00
import { createSelector } from 'reselect';
2016-03-05 21:06:04 -08:00
import {
fetchUser,
initWindowHeight,
2016-03-05 21:06:04 -08:00
updateNavHeight
} 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';
2016-06-01 15:52:08 -07:00
import { randomCompliment } from './utils/get-words';
2015-06-17 21:04:28 -07:00
2016-01-07 14:51:41 -08:00
const toastMessageFactory = React.createFactory(ToastMessage.animation);
2016-01-27 11:34:44 -08:00
const mapStateToProps = createSelector(
2016-06-01 15:52:08 -07:00
state => state.app.username,
state => state.app.points,
state => state.app.picture,
state => state.app.toast,
state => state.challengesApp.toast,
(
2016-01-27 11:34:44 -08:00
username,
points,
picture,
2016-06-01 15:52:08 -07:00
toast,
showChallengeComplete
) => ({
2016-01-27 11:34:44 -08:00
username,
points,
picture,
2016-06-01 15:52:08 -07:00
toast,
showChallengeComplete
2016-01-27 11:34:44 -08:00
})
);
2016-06-01 15:52:08 -07:00
const bindableActions = {
initWindowHeight,
updateNavHeight,
fetchUser,
submitChallenge
};
2016-01-27 11:34:44 -08:00
const fetchContainerOptions = {
fetchAction: 'fetchUser',
isPrimed({ username }) {
return !!username;
}
};
2015-06-17 21:04:28 -07:00
2016-01-27 11:34:44 -08:00
// export plain class for testing
export class FreeCodeCamp extends React.Component {
static displayName = 'FreeCodeCamp';
2016-01-07 14:51:41 -08:00
2016-01-27 11:34:44 -08:00
static propTypes = {
children: PropTypes.node,
username: PropTypes.string,
points: PropTypes.number,
picture: PropTypes.string,
2016-03-05 21:06:04 -08:00
toast: PropTypes.object,
updateNavHeight: PropTypes.func,
2016-06-01 15:52:08 -07:00
initWindowHeight: PropTypes.func,
showChallengeComplete: PropTypes.number,
submitChallenge: PropTypes.func
2016-01-27 11:34:44 -08:00
};
2016-06-01 15:52:08 -07:00
componentWillReceiveProps({
toast: nextToast = {},
showChallengeComplete: nextCC = 0
}) {
const {
toast = {},
showChallengeComplete
} = this.props;
2016-01-27 11:34:44 -08:00
if (toast.id !== nextToast.id) {
this.refs.toaster[nextToast.type || 'success'](
nextToast.message,
nextToast.title,
{
closeButton: true,
timeOut: 10000
}
);
}
2016-06-01 15:52:08 -07:00
if (nextCC !== showChallengeComplete) {
this.refs.toaster.success(
this.renderChallengeComplete(),
randomCompliment(),
{
closeButton: true,
timeOut: 0,
extendedTimeOut: 0
}
);
}
2016-01-27 11:34:44 -08:00
}
2016-03-05 21:06:04 -08:00
componentDidMount() {
this.props.initWindowHeight();
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 }
>
Submit and go to my next challenge
</Button>
);
}
2016-01-27 11:34:44 -08:00
render() {
2016-03-05 21:06:04 -08:00
const { username, points, picture, updateNavHeight } = this.props;
const navProps = { username, points, picture, updateNavHeight };
2016-01-27 11:34:44 -08:00
return (
<div>
<Nav { ...navProps }/>
<Row>
{ this.props.children }
</Row>
<ToastContainer
className='toast-bottom-right'
ref='toaster'
toastMessageFactory={ toastMessageFactory } />
</div>
);
}
}
const wrapComponent = compose(
// connect Component to Redux Store
2016-06-01 15:52:08 -07:00
connect(mapStateToProps, bindableActions),
2016-01-27 11:34:44 -08:00
// handles prefetching data
contain(fetchContainerOptions)
);
2016-01-27 11:34:44 -08:00
export default wrapComponent(FreeCodeCamp);