2017-08-29 22:55:44 +00:00
|
|
|
import React, { Component } from "react";
|
2017-09-03 16:41:10 +00:00
|
|
|
import { connect } from "react-redux";
|
2017-08-29 22:55:44 +00:00
|
|
|
import { Link } from "react-router-dom";
|
2017-09-03 16:41:10 +00:00
|
|
|
import { Dropdown, Menu } from "semantic-ui-react";
|
|
|
|
|
2017-09-04 06:30:18 +00:00
|
|
|
import { getSelfUserRequest } from "../actions/user/saga.actions";
|
2017-09-03 16:41:10 +00:00
|
|
|
import { sendLogoutRequest } from "../actions/auth/saga.actions";
|
2017-08-29 22:55:44 +00:00
|
|
|
|
|
|
|
class Navbar extends Component {
|
2017-09-04 03:47:18 +00:00
|
|
|
componentWillMount() {
|
|
|
|
const { dispatch, userToken, selfUser } = this.props;
|
|
|
|
// If the user token exists and the self user object isn't loaded, dispatch
|
|
|
|
if (userToken && Object.keys(selfUser).length === 0) {
|
2017-09-04 06:30:18 +00:00
|
|
|
dispatch(getSelfUserRequest());
|
2017-09-04 03:47:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 16:41:10 +00:00
|
|
|
dispatchLogoutRequest = () => {
|
|
|
|
this.props.dispatch(sendLogoutRequest());
|
|
|
|
};
|
|
|
|
|
2017-08-29 22:55:44 +00:00
|
|
|
render() {
|
2017-09-03 16:41:10 +00:00
|
|
|
const { userToken } = this.props;
|
2017-08-29 22:55:44 +00:00
|
|
|
return (
|
2017-09-03 16:41:10 +00:00
|
|
|
<NavbarView
|
|
|
|
isAuthenticated={!!userToken}
|
|
|
|
dispatchLogoutRequest={this.dispatchLogoutRequest}
|
|
|
|
/>
|
2017-08-29 22:55:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 16:41:10 +00:00
|
|
|
function mapStateToProps(state) {
|
2017-09-04 03:47:18 +00:00
|
|
|
return {
|
|
|
|
userToken: state.auth.userToken,
|
|
|
|
selfUser: state.user.selfUser
|
|
|
|
};
|
2017-09-03 16:41:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const NavbarView = ({ isAuthenticated, dispatchLogoutRequest }) => (
|
|
|
|
<Menu>
|
|
|
|
<Menu.Item as={Link} to="/">
|
|
|
|
Caremyway
|
|
|
|
</Menu.Item>
|
|
|
|
<Menu.Item as={Link} to="/about">
|
|
|
|
About
|
|
|
|
</Menu.Item>
|
|
|
|
{!isAuthenticated &&
|
|
|
|
<Menu.Menu position="right">
|
|
|
|
<Menu.Item as={Link} to="/auth/login">
|
|
|
|
Login
|
|
|
|
</Menu.Item>
|
|
|
|
<Menu.Item as={Link} to="/auth/register">
|
|
|
|
Register
|
|
|
|
</Menu.Item>
|
|
|
|
</Menu.Menu>}
|
|
|
|
{!!isAuthenticated &&
|
|
|
|
<Menu.Menu position="right">
|
|
|
|
<Dropdown item text="Account">
|
|
|
|
<Dropdown.Menu>
|
2017-09-03 22:24:43 +00:00
|
|
|
<Dropdown.Item as={Link} to="/user/profile">
|
|
|
|
My Profile
|
|
|
|
</Dropdown.Item>
|
2017-09-03 16:41:10 +00:00
|
|
|
<Dropdown.Item as={Link} to="/auth/settings">
|
|
|
|
Settings
|
|
|
|
</Dropdown.Item>
|
2017-09-03 22:24:43 +00:00
|
|
|
<Dropdown.Divider />
|
2017-09-03 16:41:10 +00:00
|
|
|
<Dropdown.Item onClick={dispatchLogoutRequest}>
|
|
|
|
Logout
|
|
|
|
</Dropdown.Item>
|
|
|
|
</Dropdown.Menu>
|
|
|
|
</Dropdown>
|
|
|
|
</Menu.Menu>}
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(Navbar);
|