client/src/components/Navbar.js
author Alexandre Segura <mex.zktk@gmail.com>
Fri, 16 Jun 2017 18:36:39 +0200
changeset 44 3b20e2b584fe
parent 12 48ddaa42b810
child 52 96f8a4a59bd9
permissions -rw-r--r--
Introduce authentication through API.

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { connect } from 'react-redux';
// import logo from './logo.svg';
import { Navbar, Nav, NavItem } from 'react-bootstrap';

class AppNavbar extends Component {

  onClickHome = (e) => {
    e.preventDefault();
    this.props.history.push('/');
  }

  onClickSessions = (e) => {
    e.preventDefault();
    this.props.history.push('/sessions');
  }

  onClickLogin = (e) => {
    e.preventDefault();
    this.props.history.push('/login');
  }

  renderLogin() {

    if (this.props.currentUser) {
      return (
        <NavItem>{ this.props.currentUser.username }</NavItem>
      );
    }

    return (
      <NavItem onClick={this.onClickLogin} href="/login">Login</NavItem>
    );
  }

  render() {
    return (
      <Navbar fluid inverse fixedTop>
        <Navbar.Header>
          <Navbar.Brand>
            <a onClick={this.onClickHome} href="/">IRI Notes</a>
          </Navbar.Brand>
          <Navbar.Toggle />
        </Navbar.Header>
        <Navbar.Collapse>
          <Nav>
            <NavItem onClick={this.onClickSessions} href="/sessions">Sessions</NavItem>
          </Nav>
          <Nav pullRight>
            {this.renderLogin()}
          </Nav>
        </Navbar.Collapse>
      </Navbar>
    );
  }
}

AppNavbar.propTypes = {
  isAuthenticated: PropTypes.bool.isRequired
};

function mapStateToProps(state, props) {
  return {
    isAuthenticated: state.get('isAuthenticated'),
    currentUser: state.get('currentUser'),
  };
}

export default connect(mapStateToProps)(AppNavbar);