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';
|
2018-01-20 02:24:21 +01:00
|
|
|
import Logo from 'components/Logo';
|
2018-01-25 00:42:47 +01:00
|
|
|
import NavBarChannelDropdown from 'components/NavBarChannelOptionsDropdown';
|
2018-01-29 21:27:22 +01:00
|
|
|
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 () {
|
2018-01-20 02:24:21 +01:00
|
|
|
// check to see if the user is already logged in
|
2018-01-18 21:43:02 +01:00
|
|
|
this.checkForLoggedInUser();
|
|
|
|
}
|
|
|
|
checkForLoggedInUser () {
|
2018-02-03 00:14:57 +01:00
|
|
|
const params = {credentials: 'include'};
|
2018-01-25 22:37:59 +01:00
|
|
|
request('/user', params)
|
2018-02-03 00:14:57 +01:00
|
|
|
.then(({success, message, data}) => {
|
2018-01-24 04:36:09 +01:00
|
|
|
if (success) {
|
2018-02-03 00:14:57 +01:00
|
|
|
this.props.onChannelLogin(data.channelName, data.shortChannelId, data.channelClaimId);
|
2018-01-24 04:36:09 +01:00
|
|
|
} else {
|
2018-02-03 00:14:57 +01:00
|
|
|
console.log(message);
|
2018-01-24 04:36:09 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2018-02-03 00:14:57 +01:00
|
|
|
console.log('request encountered an error', error);
|
2018-01-24 04:36:09 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
logoutUser () {
|
2018-02-03 00:14:57 +01:00
|
|
|
const params = {credentials: 'include'};
|
|
|
|
request('/logout', params)
|
|
|
|
.then(({success, message}) => {
|
|
|
|
if (success) {
|
|
|
|
this.props.onChannelLogout();
|
|
|
|
} else {
|
|
|
|
console.log(message);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log('request encountered an error', error);
|
|
|
|
});
|
2018-01-18 19:58:34 +01:00
|
|
|
}
|
|
|
|
handleSelection (event) {
|
2018-01-24 04:36:09 +01:00
|
|
|
console.log('handling selection', event);
|
2018-01-18 19:58:34 +01:00
|
|
|
const value = event.target.selectedOptions[0].value;
|
2018-01-20 02:24:21 +01:00
|
|
|
console.log('value', value);
|
2018-01-18 19:58:34 +01:00
|
|
|
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
|
2018-01-29 21:27:22 +01:00
|
|
|
this.props.history.push(`/${this.props.channelName}:${this.props.channelLongId}`);
|
2018-01-18 19:58:34 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
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 />
|
2018-01-18 19:58:34 +01:00
|
|
|
<div className="nav-bar--center">
|
|
|
|
<span className="nav-bar-tagline">Open-source, decentralized image and video sharing.</span>
|
|
|
|
</div>
|
|
|
|
<div className="nav-bar--right">
|
2018-01-31 19:52:57 +01:00
|
|
|
<NavLink className="nav-bar-link link--nav" activeClassName="link--nav-active" to="/" exact={true}>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 ? (
|
2018-01-20 02:24:21 +01:00
|
|
|
<NavBarChannelDropdown
|
|
|
|
channelName={this.props.channelName}
|
|
|
|
handleSelection={this.handleSelection}
|
2018-02-02 23:48:04 +01:00
|
|
|
defaultSelection={this.props.channelName}
|
2018-01-20 02:24:21 +01:00
|
|
|
VIEW={VIEW}
|
|
|
|
LOGOUT={LOGOUT}
|
|
|
|
/>
|
2018-01-18 19:58:34 +01:00
|
|
|
) : (
|
2018-01-31 19:52:57 +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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-29 21:27:22 +01:00
|
|
|
export default withRouter(NavBar);
|