Skip to content

Instantly share code, notes, and snippets.

@ericstumper
Created July 3, 2016 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericstumper/1323088d58ada3a93b5925cf7ac2d03c to your computer and use it in GitHub Desktop.
Save ericstumper/1323088d58ada3a93b5925cf7ac2d03c to your computer and use it in GitHub Desktop.
Navigation component handling logout
import React, { Component } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-apollo';
import { push } from 'react-router-redux';
import { Link, IndexLink } from 'react-router';
import * as authActionCreators from 'actions/auth';
import { deleteToken } from 'mutations/auth';
import styles from './styles.css';
import dashboardIcon from './images/dashboard.svg';
import profileIcon from './images/profile.svg';
import coursesIcon from './images/courses.svg';
class Navigation extends Component {
constructor() {
super();
this.onLogoutClick = this.onLogoutClick.bind(this);
}
onLogoutClick() {
this.props.mutations.deleteToken(this.props.token)
.then(({ errors, data }) => {
if (data.deleteToken) {
localStorage.removeItem('languafy.token');
this.props.actions.logout();
this.props.actions.push('/login');
} else if (errors) {
// TODO: Error Handling
}
});
}
render() {
return (
<nav className={styles.navigation} role="navigation">
<ul className={styles.navigationList}>
<li className={styles.navigationItem}>
<IndexLink className={styles.navigationLink} activeClassName={styles.isActive} to="/">
<svg className={styles.navigationLinkIcon}>
<use xlinkHref={dashboardIcon} />
</svg>
Dashboard
</IndexLink>
</li>
<li className={styles.navigationItem}>
<Link className={styles.navigationLink} activeClassName={styles.isActive} to="/courses">
<svg className={styles.navigationLinkIcon}>
<use xlinkHref={coursesIcon} />
</svg>
Courses
</Link>
</li>
<li className={styles.navigationItem}>
<Link className={styles.navigationLink} activeClassName={styles.isActive} to="/profile">
<svg className={styles.navigationLinkIcon}>
<use xlinkHref={profileIcon} />
</svg>
Profile
</Link>
</li>
<li className={styles.navigationItem}>
{this.props.isAuthenticated
? <button className={styles.navigationLink} onClick={this.onLogoutClick}>
Logout
</button>
: ''
}
</li>
</ul>
</nav>
);
}
}
Navigation.propTypes = {
actions: React.PropTypes.object,
isAuthenticated: React.PropTypes.bool,
isAuthenticating: React.PropTypes.bool,
location: React.PropTypes.object,
mutations: React.PropTypes.object,
token: React.PropTypes.string,
};
const mapStateToProps = (state) => ({
isAuthenticating: state.auth.isAuthenticating,
isAuthenticated: state.auth.isAuthenticated,
statusText: state.auth.statusText,
token: state.auth.token,
});
const mapDispatchToProps = (dispatch) => ({
actions: bindActionCreators({ ...authActionCreators, push }, dispatch),
});
const mapMutationsToProps = () => ({
deleteToken,
});
const NavigationWithData = connect({
mapStateToProps,
mapDispatchToProps,
mapMutationsToProps,
options: { pure: false },
})(Navigation);
export default NavigationWithData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment