Refactor to use me from redux store

pull/616/head
Will Piers 2016-11-28 14:19:40 -07:00
parent b17b06850f
commit 8bf9b69daa
2 changed files with 16 additions and 25 deletions

View File

@ -62,10 +62,10 @@ const Root = React.createClass({
store.dispatch(receiveMe(me));
this.setState({loggedIn: true});
}).catch((err) => {
const ImATeapot = 418;
if (err.response.status === ImATeapot) { // This means authentication is not set up!
const AUTH_DISABLED = 418;
if (err.response.status === AUTH_DISABLED) {
return this.setState({loggedIn: true});
// may be good to store this info somewhere. So that pages know whether they can use me or not
// Could store a boolean indicating auth is not set up
}
this.setState({loggedIn: false});

View File

@ -1,36 +1,21 @@
import React, {PropTypes} from 'react';
import {connect} from 'react-redux';
import SideNav from '../components/SideNav';
import {getMe} from 'src/shared/apis';
const {func, string} = PropTypes;
const {func, string, shape} = PropTypes;
const SideNavApp = React.createClass({
propTypes: {
currentLocation: string.isRequired,
addFlashMessage: func.isRequired,
sourceID: string.isRequired,
explorationID: string,
},
getInitialState() {
return {
me: null,
};
},
componentDidMount() {
getMe().then(({data: me}) => {
this.setState({me});
}).catch(({response: {status}}) => {
const NO_AUTH_ENABLED = 418;
if (status !== NO_AUTH_ENABLED) {
this.props.addFlashMessage({type: 'error', text: 'There was a network problem while retrieving the user'});
}
});
me: shape({
email: string.isRequired,
}),
},
render() {
const {currentLocation, sourceID, explorationID} = this.props;
const {me} = this.state;
const {me, currentLocation, sourceID, explorationID} = this.props;
return (
<SideNav
@ -44,4 +29,10 @@ const SideNavApp = React.createClass({
});
export default SideNavApp;
function mapStateToProps(state) {
return {
me: state.me,
};
}
export default connect(mapStateToProps)(SideNavApp);