HeaderRightMenu.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React from 'react';
  2. import { bindActionCreators } from 'redux';
  3. import { connect } from 'react-redux';
  4. import PropTypes from 'prop-types';
  5. import Router from 'next/router';
  6. import styled from 'styled-components';
  7. import HeaderMenuItem from './HeaderMenuItem';
  8. import { logoutUser, showPageLoading } from '../../actions';
  9. import Button from '../Button';
  10. const List = styled.ul`
  11. display: flex;
  12. float: right;
  13. justify-content: flex-end;
  14. align-items: center;
  15. margin: 0;
  16. padding: 0;
  17. list-style: none;
  18. `;
  19. const HeaderMenu = props => {
  20. const goTo = e => {
  21. e.preventDefault();
  22. const path = e.currentTarget.getAttribute('href');
  23. if (!path || window.location.pathname === path) return;
  24. props.showPageLoading();
  25. Router.push(path);
  26. };
  27. const login = !props.auth.isAuthenticated && (
  28. <HeaderMenuItem>
  29. <a href="/login" title="login / signup" onClick={goTo}>
  30. <Button>Login / Signup</Button>
  31. </a>
  32. </HeaderMenuItem>
  33. );
  34. const logout = props.auth.isAuthenticated && (
  35. <HeaderMenuItem>
  36. <a href="/logout" title="logout" onClick={goTo}>
  37. Log out
  38. </a>
  39. </HeaderMenuItem>
  40. );
  41. const settings = props.auth.isAuthenticated && (
  42. <HeaderMenuItem>
  43. <a href="/settings" title="settings" onClick={goTo}>
  44. <Button>Settings</Button>
  45. </a>
  46. </HeaderMenuItem>
  47. );
  48. return (
  49. <List>
  50. {logout}
  51. {settings}
  52. {login}
  53. </List>
  54. );
  55. };
  56. HeaderMenu.propTypes = {
  57. auth: PropTypes.shape({
  58. isAuthenticated: PropTypes.bool.isRequired,
  59. }).isRequired,
  60. showPageLoading: PropTypes.func.isRequired,
  61. };
  62. const mapStateToProps = ({ auth }) => ({ auth });
  63. const mapDispatchToProps = dispatch => ({
  64. logoutUser: bindActionCreators(logoutUser, dispatch),
  65. showPageLoading: bindActionCreators(showPageLoading, dispatch),
  66. });
  67. export default connect(mapStateToProps, mapDispatchToProps)(HeaderMenu);