summaryrefslogtreecommitdiffstats
path: root/finished-application/frontend/components/Nav.js
blob: 5f3495a12cfa27681c070632f2c73c88734eed16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { Fragment } from 'react';
import Link from 'next/link';
import { Query, Mutation } from 'react-apollo';
import { TOGGLE_CART_MUTATION } from './Cart';
import User from './User';
import CartCount from './CartCount';
import Signout from './Signout';
import NavStyles from './styles/NavStyles';

class Nav extends React.Component {
  render() {
    // below we set the fetchPolicy to network only so it forces re-fetch on the server
    return (
      <User>
        {({ data: { me } }) => (
          <NavStyles data-test="nav">
            <Link href="/items">
              <a>Shop</a>
            </Link>
            <Link href="/sell">
              <a>Sell</a>
            </Link>

            {!me && (
              <Link href="/signup">
                <a>Sign In</a>
              </Link>
            )}

            {me && (
              <Fragment>
                <Link href="/orders">
                  <a>Orders</a>
                </Link>
                <Link href="/me">
                  <a>My Account</a>
                </Link>
                <Signout />
                <Mutation mutation={TOGGLE_CART_MUTATION}>
                  {toggleCart => (
                    <button onClick={toggleCart}>
                      My Cart
                      <CartCount
                        className="cart-count"
                        count={me.cart.reduce((tally, cartItem) => tally + cartItem.quantity, 0)}
                      />
                    </button>
                  )}
                </Mutation>
              </Fragment>
            )}
          </NavStyles>
        )}
      </User>
    );
  }
}

export default Nav;