HeaderRightMenu.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 ReportLink = styled.a`
  20. display: none;
  21. @media only screen and (max-width: 488px) {
  22. display: block;
  23. }
  24. `;
  25. const HeaderMenu = props => {
  26. const goTo = e => {
  27. e.preventDefault();
  28. const path = e.currentTarget.getAttribute('href');
  29. if (!path || window.location.pathname === path) return;
  30. props.showPageLoading();
  31. Router.push(path);
  32. };
  33. const login = !props.auth.isAuthenticated && (
  34. <HeaderMenuItem>
  35. <a href="/login" title="login / signup" onClick={goTo}>
  36. <Button>Login / Sign up</Button>
  37. </a>
  38. </HeaderMenuItem>
  39. );
  40. const logout = props.auth.isAuthenticated && (
  41. <HeaderMenuItem>
  42. <a href="/logout" title="logout" onClick={goTo}>
  43. Log out
  44. </a>
  45. </HeaderMenuItem>
  46. );
  47. const settings = props.auth.isAuthenticated && (
  48. <HeaderMenuItem>
  49. <a href="/settings" title="settings" onClick={goTo}>
  50. <Button>Settings</Button>
  51. </a>
  52. </HeaderMenuItem>
  53. );
  54. return (
  55. <List>
  56. <HeaderMenuItem>
  57. <ReportLink href="/report" title="Report" onClick={goTo}>
  58. Report
  59. </ReportLink>
  60. </HeaderMenuItem>
  61. {logout}
  62. {settings}
  63. {login}
  64. </List>
  65. );
  66. };
  67. HeaderMenu.propTypes = {
  68. auth: PropTypes.shape({
  69. isAuthenticated: PropTypes.bool.isRequired,
  70. }).isRequired,
  71. showPageLoading: PropTypes.func.isRequired,
  72. };
  73. const mapStateToProps = ({ auth }) => ({ auth });
  74. const mapDispatchToProps = dispatch => ({
  75. logoutUser: bindActionCreators(logoutUser, dispatch),
  76. showPageLoading: bindActionCreators(showPageLoading, dispatch),
  77. });
  78. export default connect(
  79. mapStateToProps,
  80. mapDispatchToProps
  81. )(HeaderMenu);