freeCodeCamp/common/app/App.jsx

90 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-06-18 04:04:28 +00:00
import React, { PropTypes } from 'react';
2015-07-04 15:16:42 +00:00
import { 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';
import { createSelector } from 'reselect';
import { fetchUser } from './redux/actions';
import contain from './utils/professor-x';
2015-06-18 04:04:28 +00:00
2016-01-06 17:33:55 +00:00
import Nav from './components/Nav';
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(
state => state.app,
({
username,
points,
picture,
toast
}) => ({
username,
points,
picture,
toast
})
);
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,
toast: PropTypes.object
};
2016-01-27 19:34:44 +00:00
componentWillReceiveProps({ toast: nextToast = {} }) {
const { toast = {} } = this.props;
if (toast.id !== nextToast.id) {
this.refs.toaster[nextToast.type || 'success'](
nextToast.message,
nextToast.title,
{
closeButton: true,
timeOut: 10000
}
);
}
2016-01-27 19:34:44 +00:00
}
render() {
const { username, points, picture } = this.props;
const navProps = { username, points, picture };
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
connect(mapStateToProps, { fetchUser }),
// handles prefetching data
contain(fetchContainerOptions)
);
2016-01-27 19:34:44 +00:00
export default wrapComponent(FreeCodeCamp);