From a6af043686e4375d7944e91cae3d5b63c59edab0 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 22 Mar 2018 16:29:31 -0400 Subject: Migrate to render props --- frontend/components/Cart.js | 76 ++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 39 deletions(-) (limited to 'frontend/components/Cart.js') diff --git a/frontend/components/Cart.js b/frontend/components/Cart.js index 26a11d4..fee757e 100644 --- a/frontend/components/Cart.js +++ b/frontend/components/Cart.js @@ -1,12 +1,11 @@ import React from 'react'; -import { compose } from 'react-apollo'; +import { Query } from 'react-apollo'; import styled from 'styled-components'; -import PropTypes from 'prop-types'; -import { userEnhancer } from '../enhancers/enhancers'; import TakeMyMoney from './TakeMyMoney'; import formatMoney from '../lib/formatMoney'; import { UIContext } from './UIContext'; import CartItem from './CartItem'; +import { CURRENT_USER_QUERY } from '../queries/index'; const CartStyles = styled.div` padding: 20px; @@ -73,43 +72,42 @@ const CloseButton = styled.button` right: 0; `; -const Cart = props => { - const { me } = props.currentUser; - if (!me) return null; - const itemCount = me.cart.length; - const totalPrice = me.cart.reduce((tally, cartItem) => tally + cartItem.quantity * cartItem.item.price, 0); +function calcTotalPrice(cart) { + return cart.reduce((tally, cartItem) => tally + cartItem.quantity * cartItem.item.price, 0); +} - return ( - - {context => ( - -
- - × - +const Cart = props => ( + + {context => ( + + {({ data: { me }, error, loading }) => { + if (loading || error || !me) return null; + return ( + +
+ + × + - {me.name}'s Cart. -

- You have {itemCount} item{itemCount === 1 ? '' : 's'} in your cart. -

-
+ {me.name}'s Cart. +

+ You have {me.cart.length} item{me.cart.length === 1 ? '' : 's'} in your cart. +

+
- {me.cart.map(cartItem => )} -
-

{formatMoney(totalPrice)}

- - - -
-
- )} -
- ); -}; + {me.cart.map(cartItem => )} + + + ); + }} + + )} + +); -Cart.propTypes = { - currentUser: PropTypes.object.isRequired, -}; - -export default compose(userEnhancer)(Cart); -export { Cart }; +export default Cart; -- cgit v1.3