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

87 lines
2.8 KiB
React
Raw Normal View History

2018-01-18 19:58:34 +01:00
import React from 'react';
import { Link, 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 () {
// check for whether a channel is already logged in
const params = {
credentials: 'include',
}
request('/user', params)
2018-01-24 04:36:09 +01:00
.then(({success, message}) => {
if (success) {
this.props.onChannelLogin(message.channelName, message.shortChannelId, message.channelClaimId);
} else {
console.log('user was not logged in');
}
})
.catch(error => {
console.log('authenticate user errored:', error);
});
}
logoutUser () {
// send logout request to server
window.location.href = '/logout'; // NOTE: replace with a call to the server
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;
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
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">
<Link className="nav-bar-link link--nav-active" to="/">Publish</Link>
<Link className="nav-bar-link link--nav" to="/about">About</Link>
2018-01-18 21:43:02 +01:00
{ this.props.channelName ? (
<NavBarChannelDropdown
channelName={this.props.channelName}
handleSelection={this.handleSelection}
VIEW={VIEW}
LOGOUT={LOGOUT}
/>
2018-01-18 19:58:34 +01:00
) : (
<Link id="nav-bar-login-link" className="nav-bar-link link--nav" to="/login">Channel</Link>
2018-01-18 19:58:34 +01:00
)}
</div>
</div>
</div>
);
}
}
export default withRouter(NavBar);