From 8a5825d52271d712ec7c8348447d433067e13ef2 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 13 Sep 2018 14:18:56 -0400 Subject: DONE --- .../frontend/components/AddToCart.js | 68 ---------------------- 1 file changed, 68 deletions(-) delete mode 100644 finished-application/frontend/components/AddToCart.js (limited to 'finished-application/frontend/components/AddToCart.js') diff --git a/finished-application/frontend/components/AddToCart.js b/finished-application/frontend/components/AddToCart.js deleted file mode 100644 index 03242a4..0000000 --- a/finished-application/frontend/components/AddToCart.js +++ /dev/null @@ -1,68 +0,0 @@ -import { Component } from 'react'; -import { Mutation } from 'react-apollo'; -import PropTypes from 'prop-types'; -import gql from 'graphql-tag'; -import User, { CURRENT_USER_QUERY } from './User'; - -const ADD_TO_CART_MUTATION = gql` - mutation addToCart($id: ID!) { - addToCart(id: $id) { - id - quantity - item { - id - price - description - image - title - } - } - } -`; - -class AddToCart extends Component { - static propTypes = { - id: PropTypes.string.isRequired, - }; - - update = (cache, payload) => { - const newCartItem = payload.data.addToCart; - const data = cache.readQuery({ query: CURRENT_USER_QUERY }); - - const existingIndex = data.me.cart.findIndex(cartItem => cartItem.id === newCartItem.id); - if (existingIndex >= 0) { - // already in cache, just replace it - data.me.cart = [ - ...data.me.cart.slice(0, existingIndex), - newCartItem, - ...data.me.cart.slice(existingIndex + 1), - ]; - } else { - data.me.cart = [...data.me.cart, newCartItem]; - } - cache.writeQuery({ query: CURRENT_USER_QUERY, data }); - }; - - render() { - const { id } = this.props; - return ( - - {({ data: { me }, loading }) => { - if (!me || loading) return null; - return ( - - {(addToCart, { loading }) => ( - - )} - - ); - }} - - ); - } -} - -export default AddToCart; -export { ADD_TO_CART_MUTATION }; -- cgit v1.3