diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-04-06 16:44:50 -0400 |
|---|---|---|
| committer | Wes Bos <wesbos@gmail.com> | 2018-04-06 16:44:50 -0400 |
| commit | d22616d5a675f5a381c741784333151255713b45 (patch) | |
| tree | 51dc4a138fc4b1e8ac1b1e287e5c246f24377242 /frontend/components/Cart.js | |
| parent | d90d97337c39e1fa723ee01627fbd3f78adfcf31 (diff) | |
local data with apollo
Diffstat (limited to 'frontend/components/Cart.js')
| -rw-r--r-- | frontend/components/Cart.js | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/frontend/components/Cart.js b/frontend/components/Cart.js index 0a77754..7dbea8f 100644 --- a/frontend/components/Cart.js +++ b/frontend/components/Cart.js @@ -1,11 +1,10 @@ import React from 'react'; -import { Query } from 'react-apollo'; +import { Mutation, Query } from 'react-apollo'; import styled from 'styled-components'; import TakeMyMoney from './TakeMyMoney'; import formatMoney from '../lib/formatMoney'; -import { UIContext } from './UIContext'; import CartItem from './CartItem'; -import { CURRENT_USER_QUERY } from '../queries/index'; +import { CURRENT_USER_QUERY, LOCAL_STATE_QUERY, TOGGLE_CART_MUTATION } from '../queries/index'; import calcTotalPrice from '../lib/calcTotalPrice'; const CartStyles = styled.div` @@ -74,37 +73,43 @@ const CloseButton = styled.button` `; const Cart = props => ( - <UIContext.Consumer> - {context => ( - <Query query={CURRENT_USER_QUERY}> - {({ data: { me }, error, loading }) => { - if (loading || error || !me) return null; - return ( - <CartStyles open={context.state.isCartOpen}> - <header> - <CloseButton title="close" onClick={context.toggle}> - × - </CloseButton> + <Mutation mutation={TOGGLE_CART_MUTATION}> + {toggle => ( + <Query query={LOCAL_STATE_QUERY}> + {({ data }) => ( + <Query query={CURRENT_USER_QUERY}> + {({ data: { me }, error, loading }) => { + if (loading || error || !me) return null; + return ( + <CartStyles open={data.cartOpen}> + <header> + <CloseButton title="close" onClick={toggle}> + × + </CloseButton> - <Supreme>{me.name}'s Cart.</Supreme> - <p> - You have {me.cart.length} item{me.cart.length === 1 ? '' : 's'} in your cart. - </p> - </header> + <Supreme>{me.name}'s Cart.</Supreme> + <p> + You have {me.cart.length} item{me.cart.length === 1 ? '' : 's'} in your cart. + </p> + </header> - <CartUl>{me.cart.map(cartItem => <CartItem key={cartItem.id} cartItem={cartItem} />)}</CartUl> - <footer> - <p>{formatMoney(calcTotalPrice(me.cart))}</p> - <TakeMyMoney> - <button>Checkout</button> - </TakeMyMoney> - </footer> - </CartStyles> - ); - }} + <CartUl> + {me.cart.map(cartItem => <CartItem key={cartItem.id} cartItem={cartItem} />)} + </CartUl> + <footer> + <p>{formatMoney(calcTotalPrice(me.cart))}</p> + <TakeMyMoney> + <button>Checkout</button> + </TakeMyMoney> + </footer> + </CartStyles> + ); + }} + </Query> + )} </Query> )} - </UIContext.Consumer> + </Mutation> ); export default Cart; |
