summaryrefslogtreecommitdiffstats
path: root/finished-application/frontend/components/Cart.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-06-14 16:44:21 -0400
committerWes Bos <wesbos@gmail.com>2018-06-14 16:44:21 -0400
commitb1600adec47a04f60e1da07d94a3ed3906ff5aee (patch)
tree828f786da6806d7237ee5cbc5df61e552353b85b /finished-application/frontend/components/Cart.js
parenta95e08cd2dd5d7ec0a0412adae02515a5f9cec4f (diff)
starter files
Diffstat (limited to 'finished-application/frontend/components/Cart.js')
-rw-r--r--finished-application/frontend/components/Cart.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/finished-application/frontend/components/Cart.js b/finished-application/frontend/components/Cart.js
new file mode 100644
index 0000000..7e8a9c5
--- /dev/null
+++ b/finished-application/frontend/components/Cart.js
@@ -0,0 +1,74 @@
+import React from 'react';
+import { Mutation, Query } from 'react-apollo';
+import { adopt } from 'react-adopt';
+import gql from 'graphql-tag';
+import TakeMyMoney from './TakeMyMoney';
+import formatMoney from '../lib/formatMoney';
+import CartItem from './CartItem';
+import { CURRENT_USER_QUERY } from './User';
+import calcTotalPrice from '../lib/calcTotalPrice';
+import Error from './ErrorMessage';
+import CartStyles from './styles/CartStyles';
+import Supreme from './styles/Supreme';
+import CloseButton from './styles/CloseButton';
+import SickButton from './styles/SickButton';
+
+const LOCAL_STATE_QUERY = gql`
+ query {
+ cartOpen @client
+ }
+`;
+
+const TOGGLE_CART_MUTATION = gql`
+ mutation {
+ toggleCart @client
+ }
+`;
+
+const Composed = adopt({
+ toggleCart: ({ render }) => (
+ <Mutation mutation={TOGGLE_CART_MUTATION}>
+ {(mutate, result) => render({ mutate, result })}
+ </Mutation>
+ ),
+ localState: ({ render }) => <Query query={LOCAL_STATE_QUERY} children={render} />,
+ currentUser: ({ render }) => (
+ <Query children={render} query={CURRENT_USER_QUERY} data-test="cart" />
+ ),
+});
+
+const Cart = () => (
+ <Composed>
+ {({ toggleCart, localState, currentUser }) => {
+ const { data: { me }, error, loading } = currentUser;
+ if (loading) return <p>Loading...</p>;
+ if (error) return <Error error={error} />;
+ if (!me) return null;
+ return (
+ <CartStyles open={localState.data.cartOpen}>
+ <header>
+ <CloseButton title="close" onClick={toggleCart.mutate}>
+ &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>
+ <TakeMyMoney>
+ <SickButton>Checkout</SickButton>
+ </TakeMyMoney>
+ </footer>
+ </CartStyles>
+ );
+ }}
+ </Composed>
+);
+
+export default Cart;
+export { LOCAL_STATE_QUERY, TOGGLE_CART_MUTATION };