summaryrefslogtreecommitdiffstats
path: root/frontend/components/Cart.js
blob: 78e0d88c1aabec26a49f00608d23383c198ffbf7 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Component } from 'react';
import { graphql, compose } from 'react-apollo';
import styled from 'styled-components';
import Title from './styles/Title';
import { updateUIEnhancer, userEnhancer, uiEnhancer } from '../enhancers/enhancers';
import RemoveFromCart from './RemoveFromCart';
import TakeMyMoney from './TakeMyMoney';
import ToggleCart from './ToggleCart';
import formatMoney from '../lib/formatMoney';

import CartItem from './CartItem';

const CartStyles = styled.div`
  padding: 20px;
  position: relative;
  background: white;
  position: fixed;
  height: 100%;
  top: 0;
  right: 0;
  width: 40%;
  min-width: 500px;
  bottom: 0;
  transform: translateX(100%);
  transition: all 0.3s;
  box-shadow: 0 0 10px 3px rgba(0, 0, 0, 0.2);
  z-index: 5;
  display: grid;
  grid-template-rows: auto 1fr auto;
  ${props => props.open && `transform: translateX(0);`};
  header {
    border-bottom: 5px solid ${props => props.theme.black};
    margin-bottom: 2rem;
    padding-bottom: 2rem;
  }
  footer {
    border-top: 10px double ${props => props.theme.black};
    margin-top: 2rem;
    padding-top: 2rem;
    display: grid;
    grid-template-columns: auto auto;
    align-items: center;
    font-size: 3rem;
    font-weight: 900;
    p {
      margin: 0;
    }
  }
`;

const CartUl = styled.ul`
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: scroll;
`;

const Supreme = styled.h3`
  background: ${props => props.theme.red};
  color: white;
  display: inline-block;
  padding: 4px 5px;
  transform: skew(-3deg);
  margin: 0;
  font-size: 4rem;
`;

const CloseButton = styled.button`
  background: black;
  color: white;
  font-size: 3rem;
  border: 0;
  position: absolute;
  z-index: 2;
  right: 0;
`;

class Cart extends Component {
  componentDidMount() {
    // TODO listen for escape
  }

  componentWillUnmount() {
    // TODO unlisten for escape
  }
  render() {
    const { me } = this.props.currentUser;
    if (!me) return null;
    const itemCount = me.cart.length;
    const quantityCount = me.cart.reduce((tally, cartItem) => tally + cartItem.quantity, 0);
    const totalPrice = me.cart.reduce((tally, cartItem) => tally + cartItem.quantity * cartItem.item.price, 0);

    return (
      <CartStyles open={this.props.ui.uiData.isCartOpen}>
        <header>
          <ToggleCart
            render={toggle => (
              <CloseButton title="close" onClick={toggle}>
                &times;
              </CloseButton>
            )}
          />
          <Supreme>Your Cart.</Supreme>
          <p>
            You have {itemCount} item{itemCount === 1 ? '' : 's'} in your cart. S{'I'.repeat(quantityCount)}CK
          </p>
        </header>

        <CartUl>{me.cart.map(cartItem => <CartItem key={cartItem.id} cartItem={cartItem} />)}</CartUl>
        <footer>
          <p>{formatMoney(totalPrice)}</p>
          <TakeMyMoney>
            <button>Checkout</button>
          </TakeMyMoney>
        </footer>
      </CartStyles>
    );
  }
}

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