From 3b45dd7b593825721ec9ee9f6a49c732601b1ae0 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 21 Sep 2017 15:39:12 -0400 Subject: Cha Ching --- components/CartList.js | 54 ++++++++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) (limited to 'components/CartList.js') diff --git a/components/CartList.js b/components/CartList.js index d0456aa..d485907 100644 --- a/components/CartList.js +++ b/components/CartList.js @@ -5,31 +5,30 @@ import Signup from '../components/Signup'; import LoginAuth0 from '../components/LoginAuth0'; import Page from '../components/Page'; import { USER_ORDERS_QUERY } from '../queries'; -import { graphql, compose } from 'react-apollo' +import { graphql, compose } from 'react-apollo'; import has from 'lodash.has'; import get from 'lodash.get'; import formatMoney from '../lib/formatMoney'; import makeImage from '../lib/image'; -import TakeMyMoney from './TakeMyMoney' - -import { CURRENT_USER_QUERY, REMOVE_FROM_CART_MUTATION } from '../queries'; +import TakeMyMoney from './TakeMyMoney'; +import { removeFromCartEnhancer } from '../enhancers'; +import { CURRENT_USER_QUERY } from '../queries'; class CartList extends Component { componentDidMount() { setTimeout(this.props.currentUserQuery.refetch, 1); } render() { - - if(this.props.loading) { - return

Loading...

+ if (this.props.loading) { + return

Loading...

; } - if(this.props.error) { - return

Error...

+ if (this.props.error) { + return

Error...

; } - if(!has(this.props, 'currentUserQuery.user.cart')) { - return

Don't have it yet!

+ if (!has(this.props, 'currentUserQuery.user.cart')) { + return

Don't have it yet!

; } const cart = this.props.currentUserQuery.user.cart; @@ -43,32 +42,27 @@ class CartList extends Component { {cart.map(item => (
  • {item.title} - +
  • ))} - + - ) + ); } } const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUserQuery' }); -const removeItemEnhancer = graphql(REMOVE_FROM_CART_MUTATION, { name: 'removeFromCart', options: { - update: (proxy, payload) => { - const data = proxy.readQuery({ query: CURRENT_USER_QUERY }); - const cartItemId = payload.data.removeFromCartItems.cartItem.id; - data.user.cart = data.user.cart.filter(item => item.id !== cartItemId); - proxy.writeQuery({ query: CURRENT_USER_QUERY, data }); - }, - } }); -export default compose(userEnhancer, removeItemEnhancer)(CartList) +export default compose(userEnhancer, removeFromCartEnhancer)(CartList); -- cgit v1.3