summaryrefslogtreecommitdiffstats
path: root/frontend/components/Cart.js
blob: 72c34aed3e142f423dfba1f82d45825490414a54 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Component } from 'react';
import { graphql, compose } from 'react-apollo';
import styled from 'styled-components';
import { networkEnhancer, userEnhancer, uiEnhancer } from '../enhancers/enhancers';
import RemoveFromCart from './RemoveFromCart';
import TakeMyMoney from './TakeMyMoney';

const CartStyles = styled.div`
  padding: 20px;
  position: relative;
  background: white;
  position: fixed;
  height: 100%;
  top: 0;
  right: 0;
  bottom: 0;
  transform: translateX(100%);
  transition: all 0.3s;
  box-shadow: 0 0 10px 3px rgba(0, 0, 0, 0.2);
  z-index: 5;
  ${props => props.open && `transform: translateX(0);`};
  .toggleOpen {
    position: absolute;
    top: 0;
    left: 0;
    transform: translateX(-100%);
  }
`;

class Cart extends Component {
  state = {
    open: true,
  };
  componentDidMount() {
    console.log('refetching..');
    setTimeout(this.props.currentUser.refetch, 10);
  }
  toggleOpen = () => {
    this.setState({ open: !this.state.open });
  };
  handleClick = () => {
    console.log('CLICK');
    this.props.updateNetworkStatus(!this.props.ui.networkStatus.isConnected);
  };
  render() {
    if (!this.props.currentUser.me) {
      return null;
    }
    const { me } = this.props.currentUser;

    return (
      <CartStyles className="cart" open={this.state.open}>
        <button onClick={this.handleClick}>
          Toggle Network Status:: {this.props.ui.networkStatus.isConnected.toString()}
        </button>
        <button className="toggleOpen" onClick={this.toggleOpen}>
          Open Cart
        </button>

        <p>🛒: {me.cart.length} Items</p>
        <ul>
          {me.cart.map(cartItem => (
            <li key={cartItem.id}>
              <strong>
                {cartItem.quantity} of {cartItem.item.title}
              </strong>
              <RemoveFromCart id={cartItem.id} />
            </li>
          ))}
        </ul>

        <TakeMyMoney>
          <hr />
          <button>Checkout!!!</button>
        </TakeMyMoney>
        {/* Theren
        {user.cart.length === 1 ? ' is ' : 'are '}
        <ChaChing amount={user.cart.length} />
        {user.cart.length === 1 ? ' item ' : ' items '}
        in your cart totaling */}
        {/* <ChaChing amount={formatMoney(total)} /> */}
      </CartStyles>
    );
  }
}

export default compose(userEnhancer, networkEnhancer, uiEnhancer)(Cart);