spee.ch/react/containers/NavBar/view.jsx

86 lines
2.8 KiB
React
Raw Normal View History

2018-01-18 19:58:34 +01:00
import React from 'react';
2018-01-31 19:52:57 +01:00
import { NavLink, withRouter } from 'react-router-dom';
import Logo from 'components/Logo';
import NavBarChannelDropdown from 'components/NavBarChannelOptionsDropdown';
import request from 'utils/request';
2018-01-18 19:58:34 +01:00
const VIEW = 'VIEW';
const LOGOUT = 'LOGOUT';
class NavBar extends React.Component {
constructor (props) {
super(props);
2018-01-18 21:43:02 +01:00
this.checkForLoggedInUser = this.checkForLoggedInUser.bind(this);
2018-01-24 04:36:09 +01:00
this.logoutUser = this.logoutUser.bind(this);
2018-01-18 19:58:34 +01:00
this.handleSelection = this.handleSelection.bind(this);
}
componentDidMount () {
// check to see if the user is already logged in
2018-01-18 21:43:02 +01:00
this.checkForLoggedInUser();
}
checkForLoggedInUser () {
const params = {credentials: 'include'};
request('/user', params)
.then(({ data }) => {
this.props.onChannelLogin(data.channelName, data.shortChannelId, data.channelClaimId);
2018-01-24 04:36:09 +01:00
})
.catch(error => {
console.log('/user error:', error.message);
2018-01-24 04:36:09 +01:00
});
}
logoutUser () {
const params = {credentials: 'include'};
request('/logout', params)
.then(() => {
this.props.onChannelLogout();
})
.catch(error => {
console.log('/logout error', error.message);
});
2018-01-18 19:58:34 +01:00
}
handleSelection (event) {
const value = event.target.selectedOptions[0].value;
switch (value) {
2018-01-18 21:43:02 +01:00
case LOGOUT:
2018-01-24 04:36:09 +01:00
this.logoutUser();
2018-01-18 19:58:34 +01:00
break;
2018-01-18 21:43:02 +01:00
case VIEW:
// redirect to channel page
this.props.history.push(`/${this.props.channelName}:${this.props.channelLongId}`);
2018-01-18 19:58:34 +01:00
break;
default:
break;
}
}
render () {
const { siteDescription } = this.props;
2018-01-18 19:58:34 +01:00
return (
<div className='row row--wide nav-bar'>
<div className='row row--padded row--short flex-container--row flex-container--space-between-center'>
2018-01-18 21:43:02 +01:00
<Logo />
<div className='nav-bar--center'>
<span className='nav-bar-tagline'>{siteDescription}</span>
2018-01-18 19:58:34 +01:00
</div>
<div className='nav-bar--right'>
<NavLink className='nav-bar-link link--nav' activeClassName='link--nav-active' to='/' exact>Publish</NavLink>
<NavLink className='nav-bar-link link--nav' activeClassName='link--nav-active' to='/about'>About</NavLink>
2018-01-18 21:43:02 +01:00
{ this.props.channelName ? (
<NavBarChannelDropdown
channelName={this.props.channelName}
handleSelection={this.handleSelection}
defaultSelection={this.props.channelName}
VIEW={VIEW}
LOGOUT={LOGOUT}
/>
2018-01-18 19:58:34 +01:00
) : (
<NavLink id='nav-bar-login-link' className='nav-bar-link link--nav' activeClassName='link--nav-active' to='/login'>Channel</NavLink>
2018-01-18 19:58:34 +01:00
)}
</div>
</div>
</div>
);
}
}
export default withRouter(NavBar);