client/src/components/Navbar.js
author Alexandre Segura <mex.zktk@gmail.com>
Thu, 29 Jun 2017 12:06:48 +0200
changeset 106 fffefefed507
parent 105 0a1d6560acac
child 107 e6f85e26b08c
permissions -rw-r--r--
Fix bug when there is no group.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     1
import PropTypes from 'prop-types';
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     2
import React, { Component } from 'react';
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     3
import { connect } from 'react-redux';
105
0a1d6560acac Introduce authenticated routes.
Alexandre Segura <mex.zktk@gmail.com>
parents: 104
diff changeset
     4
import { withRouter } from 'react-router';
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
     5
import { bindActionCreators } from 'redux';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
// import logo from './logo.svg';
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
     7
import { Navbar, Nav, NavItem, NavDropdown, MenuItem } from 'react-bootstrap';
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
     8
import * as authActions from '../actions/authActions';
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     9
65
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    10
const LoginNav = ({isAuthenticated, currentUser, history, authActions}) => {
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    11
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    12
  const onClickSettings = (e) => {
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    13
    e.preventDefault();
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    14
    history.push('/settings');
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    15
  }
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    16
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    17
  const onClickLogout = (e) => {
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    18
    e.preventDefault();
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    19
    authActions.logout();
104
d48a74232d22 Go back to homepage on logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 81
diff changeset
    20
    history.push('/');
65
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    21
  }
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    22
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    23
  const onClickLogin = (e) => {
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    24
    e.preventDefault();
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    25
    history.push('/login');
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    26
  }
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    27
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    28
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    29
  if (isAuthenticated) {
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    30
    return (
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    31
      <NavDropdown title={ currentUser.get('username') } id="user-dropdown">
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    32
        <MenuItem onClick={onClickSettings}>Settings</MenuItem>
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    33
        <MenuItem onClick={onClickLogout}>Logout</MenuItem>
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    34
      </NavDropdown>
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    35
    );
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    36
  }
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    37
  return (
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    38
    <NavItem onClick={onClickLogin} href="/login">Login</NavItem>
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    39
  );
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    40
}
14989b339e5d Use a fonctionnal component for the login part of the navbar
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    41
81
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    42
const Online = ({ offline }) => {
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    43
  return (
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    44
    <NavItem>
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    45
      <span className="material-icons" style={{ color: offline.online ? '#2ECC71' : '#E74C3C' }}>signal_wifi_4_bar</span>
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    46
    </NavItem>
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    47
  )
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    48
}
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    49
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    50
class AppNavbar extends Component {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    51
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    52
  onClickHome = (e) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    53
    e.preventDefault();
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    54
    this.props.history.push('/');
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    55
  }
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    56
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    57
  onClickSessions = (e) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    58
    e.preventDefault();
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    59
    this.props.history.push('/sessions');
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    60
  }
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    61
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    62
  render() {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    63
    return (
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    64
      <Navbar fluid inverse fixedTop>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    65
        <Navbar.Header>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    66
          <Navbar.Brand>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    67
            <a onClick={this.onClickHome} href="/">IRI Notes</a>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    68
          </Navbar.Brand>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    69
          <Navbar.Toggle />
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    70
        </Navbar.Header>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    71
        <Navbar.Collapse>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    72
          <Nav>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    73
            <NavItem onClick={this.onClickSessions} href="/sessions">Sessions</NavItem>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    74
          </Nav>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    75
          <Nav pullRight>
81
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    76
            <Online {...this.props} />
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    77
            <LoginNav {...this.props} />
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    78
          </Nav>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    79
        </Navbar.Collapse>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    80
      </Navbar>
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    81
    );
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    82
  }
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    83
}
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    84
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    85
AppNavbar.propTypes = {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    86
  isAuthenticated: PropTypes.bool.isRequired
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    87
};
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    88
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    89
function mapStateToProps(state, props) {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    90
  return {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    91
    isAuthenticated: state['isAuthenticated'],
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 59
diff changeset
    92
    currentUser: state['currentUser'],
81
a6bd1aaddc34 Add online/offline indicator.
Alexandre Segura <mex.zktk@gmail.com>
parents: 65
diff changeset
    93
    offline: state['offline']
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    94
  };
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    95
}
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    96
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    97
function mapDispatchToProps(dispatch) {
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    98
  return {
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    99
    authActions: bindActionCreators(authActions, dispatch),
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
   100
  }
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
   101
}
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
   102
105
0a1d6560acac Introduce authenticated routes.
Alexandre Segura <mex.zktk@gmail.com>
parents: 104
diff changeset
   103
export default connect(mapStateToProps, mapDispatchToProps)(withRouter(AppNavbar));