|
@@ -1,67 +1,96 @@
|
|
|
-import React from 'react';
|
|
|
-import { Link } from "react-router-dom";
|
|
|
+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 * as routes from '../../constants/routes'
|
|
|
-import logOut from './../../actions/auth/logOut';
|
|
|
+import storageKey from './../../utils/storageKey';
|
|
|
+import { signInStateZeroing } from './../../actions/auth/signIn';
|
|
|
+import { LOG_OUT_URL } from './../../constants/auth';
|
|
|
|
|
|
-class Header extends React.Component {
|
|
|
+import * as routes from '../../constants/routes';
|
|
|
+
|
|
|
+//pure component + make storage check an action -> connect router
|
|
|
+class Header extends PureComponent {
|
|
|
handleLogOut = () => {
|
|
|
- const { logOut } = this.props;
|
|
|
- logOut()
|
|
|
- //TODO: REDIRECT
|
|
|
+ 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));
|
|
|
}
|
|
|
|
|
|
render() {
|
|
|
+ const { role } = this.props;
|
|
|
+
|
|
|
return (
|
|
|
- <nav class="navbar navbar-expand-lg navbar-dark bg-dark p-3">
|
|
|
- <Link to={routes.LANDING} class="navbar-brand" href="#">Test.io</Link>
|
|
|
- <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" />
|
|
|
+ <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" 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" 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" 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" 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">
|
|
|
- Dropdown
|
|
|
+ {
|
|
|
+ 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 class="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 class="dropdown-divider"></div>
|
|
|
- <a class="nav-link text-secondary" href="#">Something else here</a>
|
|
|
- </div>
|
|
|
- </li>
|
|
|
+ <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-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 mr-2" 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="">
|
|
|
- <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>
|
|
|
- <button onClick={this.handleLogOut} className="btn btn-outline-primary">Log Out</button>
|
|
|
+ <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>
|
|
@@ -70,8 +99,10 @@ class Header extends React.Component {
|
|
|
}
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
- state: state.logOut
|
|
|
+ role: state.tokenCheckout.role,
|
|
|
});
|
|
|
-const mapDispatchToProps = dispatch => bindActionCreators({ logOut }, dispatch)
|
|
|
+const mapDispatchToProps = dispatch => bindActionCreators({
|
|
|
+ zeroState: signInStateZeroing
|
|
|
+}, dispatch)
|
|
|
|
|
|
-export default connect(mapStateToProps, mapDispatchToProps)(Header);
|
|
|
+export default withRouter(connect(mapStateToProps, mapDispatchToProps)(Header));
|