|
@@ -1,59 +1,109 @@
|
|
|
-import React from 'react';
|
|
|
-import { Link } from "react-router-dom";
|
|
|
-import * as routes from '../../constants/routes'
|
|
|
+import React, { PureComponent } from 'react';
|
|
|
+import { Link, withRouter, Redirect } from "react-router-dom";
|
|
|
+import { connect } from 'react-redux';
|
|
|
+import { bindActionCreators } from 'redux';
|
|
|
+import axios from 'axios';
|
|
|
+
|
|
|
+import storageKey from './../../utils/storageKey';
|
|
|
+import { signInStateZeroing } from './../../actions/auth/signIn';
|
|
|
+import { LOG_OUT_URL } from './../../constants/auth';
|
|
|
+
|
|
|
+import * as routes from '../../constants/routes';
|
|
|
+
|
|
|
+//pure component + make storage check an action -> connect router
|
|
|
+class Header extends PureComponent {
|
|
|
+ handleLogOut = () => {
|
|
|
+ console.log(this.props)
|
|
|
+ const { history, zeroState } = this.props;
|
|
|
+
|
|
|
+ axios.get(LOG_OUT_URL, {})
|
|
|
+ .then(() => {
|
|
|
+ console.log('request was succesfull')
|
|
|
+ localStorage.removeItem(storageKey);
|
|
|
+ zeroState();
|
|
|
+ history.push('/sign-in');
|
|
|
+ })
|
|
|
+ .catch(e => alert('Sorry, something bad happened, try log out later \n' + e.message));
|
|
|
+ }
|
|
|
|
|
|
-export default class Header extends React.Component {
|
|
|
render() {
|
|
|
+ const { role } = this.props;
|
|
|
+
|
|
|
return (
|
|
|
- <nav class="navbar navbar-expand-lg navbar-dark bg-dark p-3">
|
|
|
- <a class="navbar-brand" href="#">Test.io</a>
|
|
|
- <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
|
|
- <span class="navbar-toggler-icon"></span>
|
|
|
+ <nav className="navbar navbar-expand-lg navbar-dark bg-dark p-3">
|
|
|
+ <Link to={routes.LANDING} className="navbar-brand" href="#">Test.io</Link>
|
|
|
+ <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
|
|
|
+ <span className="navbar-toggler-icon" />
|
|
|
</button>
|
|
|
|
|
|
- <div class="collapse navbar-collapse" id="navbarSupportedContent">
|
|
|
- <ul class="navbar-nav mr-auto">
|
|
|
- <li class="nav-item active">
|
|
|
- <Link onClick={this.handleClick} class="nav-link font-ci font-ci-bold" to={routes.HOME}>Home <span class="sr-only">(current)</span></Link>
|
|
|
+ <div className="collapse navbar-collapse" id="navbarSupportedContent">
|
|
|
+ <ul className="navbar-nav mr-auto">
|
|
|
+ <li className="nav-item active">
|
|
|
+ <Link onClick={this.handleClick} className="nav-link" to={routes.HOME}>Home <span className="sr-only">(current)</span></Link>
|
|
|
</li>
|
|
|
- <li class="nav-item">
|
|
|
- <Link onClick={this.handleClick} class="nav-link font-ci font-ci-bold" to={routes.PROFILE}>Profile</Link>
|
|
|
+ <li className="nav-item">
|
|
|
+ <Link onClick={this.handleClick} className="nav-link" to={routes.PROFILE}>Profile</Link>
|
|
|
</li>
|
|
|
- <li class="nav-item">
|
|
|
- <Link onClick={this.handleClick} class="nav-link font-ci font-ci-bold " to={routes.TESTS}>Tests</Link>
|
|
|
+ <li className="nav-item">
|
|
|
+ <Link onClick={this.handleClick} className="nav-link" to={routes.TESTS}>Tests</Link>
|
|
|
</li>
|
|
|
- <li class="nav-item">
|
|
|
- <Link onClick={this.handleClick} class="nav-link font-ci font-ci-bold " to={routes.CATEGORIES}>Catigories</Link>
|
|
|
+ <li className="nav-item">
|
|
|
+ <Link onClick={this.handleClick} className="nav-link" to={routes.CATEGORIES}>Catigories</Link>
|
|
|
</li>
|
|
|
{/* TODO: admin-only ! */}
|
|
|
- <li class="nav-item dropdown">
|
|
|
- <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
|
|
- Menu Admina
|
|
|
- </a>
|
|
|
- <div class="dropdown-menu" aria-labelledby="navbarDropdown">
|
|
|
- <Link onClick={this.handleClick} class="nav-link font-ci font-ci-bold " to={routes.DELETE_USER}>Delete</Link>
|
|
|
- <a class="dropdown-item" href="#">Action</a>
|
|
|
- <a class="dropdown-item" href="#">Another action</a>
|
|
|
- <div class="dropdown-divider"></div>
|
|
|
- <a class="dropdown-item" href="#">Something else here</a>
|
|
|
- </div>
|
|
|
- </li>
|
|
|
|
|
|
+ {
|
|
|
+ role
|
|
|
+ ?
|
|
|
+ <li className="nav-item dropdown">
|
|
|
+ {/* eslint-disable-next-line */}
|
|
|
+ <a className="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
|
|
+ Dropdown
|
|
|
+ </a>
|
|
|
+ <div className="dropdown-menu" aria-labelledby="navbarDropdown">
|
|
|
+ <Link to={routes.CREATE_TEST} className="nav-link text-secondary">Create Test</Link>
|
|
|
+ <Link to={routes.CREATE_CATEGORY} className="nav-link text-secondary">Create Category</Link>
|
|
|
+
|
|
|
+ <div className="dropdown-divider"></div>
|
|
|
+ {/* eslint-disable-next-line */}
|
|
|
+ <a className="nav-link text-secondary" href="#">Something else here</a>
|
|
|
+ </div>
|
|
|
+ </li>
|
|
|
+ :
|
|
|
+ null
|
|
|
+ }
|
|
|
</ul>
|
|
|
- <form class="form-inline my-2 my-lg-0">
|
|
|
- <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" />
|
|
|
- <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
|
|
|
+ <form className="form-inline my-lg-0">
|
|
|
+ <input className="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" />
|
|
|
+ <button className="btn btn-outline-success my-2 my-sm-0 mr-2" type="submit">Search</button>
|
|
|
</form>
|
|
|
- <div className="pl-2">
|
|
|
- <Link to={routes.SIGN_IN}>
|
|
|
- <button className="btn btn-outline-primary">Sign In</button>
|
|
|
- </Link>
|
|
|
- <Link to={routes.SIGN_UP}>
|
|
|
- <button className="btn btn-outline-primary ml-2">Sign Up</button>
|
|
|
- </Link>
|
|
|
+ <div className="controls">
|
|
|
+ {
|
|
|
+ role !== null
|
|
|
+ ?
|
|
|
+ <button onClick={this.handleLogOut} className="btn btn-outline-primary">Log Out</button>
|
|
|
+ :
|
|
|
+ <React.Fragment>
|
|
|
+ <Link to={routes.SIGN_IN}>
|
|
|
+ <button className="btn btn-outline-primary">Sign In</button>
|
|
|
+ </Link>
|
|
|
+ <Link to={routes.SIGN_UP}>
|
|
|
+ <button className="btn btn-outline-primary mx-2">Sign Up</button>
|
|
|
+ </Link>
|
|
|
+ </React.Fragment>
|
|
|
+ }
|
|
|
</div>
|
|
|
</div>
|
|
|
</nav>
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+const mapStateToProps = state => ({
|
|
|
+ role: state.tokenCheckout.role,
|
|
|
+});
|
|
|
+const mapDispatchToProps = dispatch => bindActionCreators({
|
|
|
+ zeroState: signInStateZeroing
|
|
|
+}, dispatch)
|
|
|
+
|
|
|
+export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Header));
|