summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/42/frontend/components/Cart.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-08-17 15:51:02 -0400
committerWes Bos <wesbos@gmail.com>2018-08-17 15:51:02 -0400
commitc3bbebfcfeed7a9b8c7470d7b48e12046d834e59 (patch)
tree05c50c931d0e709c640744387fbabe870658e9e7 /stepped-solutions/42/frontend/components/Cart.js
parent124f227a8235e08bddf8376e0b3d01d33c4414f3 (diff)
suuuup everyone
Diffstat (limited to 'stepped-solutions/42/frontend/components/Cart.js')
-rwxr-xr-xstepped-solutions/42/frontend/components/Cart.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/stepped-solutions/42/frontend/components/Cart.js b/stepped-solutions/42/frontend/components/Cart.js
new file mode 100755
index 0000000..1895235
--- /dev/null
+++ b/stepped-solutions/42/frontend/components/Cart.js
@@ -0,0 +1,63 @@
+import React from 'react';
+import { Query, Mutation } from 'react-apollo';
+import gql from 'graphql-tag';
+import User from './User';
+import CartStyles from './styles/CartStyles';
+import Supreme from './styles/Supreme';
+import CloseButton from './styles/CloseButton';
+import SickButton from './styles/SickButton';
+import CartItem from './CartItem';
+import calcTotalPrice from '../lib/calcTotalPrice';
+import formatMoney from '../lib/formatMoney';
+
+const LOCAL_STATE_QUERY = gql`
+ query {
+ cartOpen @client
+ }
+`;
+
+const TOGGLE_CART_MUTATION = gql`
+ mutation {
+ toggleCart @client
+ }
+`;
+
+const Cart = () => (
+ <User>
+ {({ data: { me } }) => {
+ if (!me) return null;
+ console.log(me);
+ return (
+ <Mutation mutation={TOGGLE_CART_MUTATION}>
+ {toggleCart => (
+ <Query query={LOCAL_STATE_QUERY}>
+ {({ data }) => (
+ <CartStyles open={data.cartOpen}>
+ <header>
+ <CloseButton onClick={toggleCart} title="close">
+ &times;
+ </CloseButton>
+ <Supreme>{me.name}'s Cart</Supreme>
+ <p>
+ You Have {me.cart.length} Item{me.cart.length === 1 ? '' : 's'} in your cart.
+ </p>
+ </header>
+ <ul>
+ {me.cart.map(cartItem => <CartItem key={cartItem.id} cartItem={cartItem} />)}
+ </ul>
+ <footer>
+ <p>{formatMoney(calcTotalPrice(me.cart))}</p>
+ <SickButton>Checkout</SickButton>
+ </footer>
+ </CartStyles>
+ )}
+ </Query>
+ )}
+ </Mutation>
+ );
+ }}
+ </User>
+);
+
+export default Cart;
+export { LOCAL_STATE_QUERY, TOGGLE_CART_MUTATION };