diff options
| author | Wes Bos <wesbos@gmail.com> | 2018-05-24 09:03:04 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-24 09:03:04 -0400 |
| commit | b294a6fa500b165aae86afbad5fce49a02314b0d (patch) | |
| tree | f538c8b01f1ee41ea0c7cd0f58fcf7746b8accb8 /frontend/components/AddToCart.js | |
| parent | ee068a81a5a8e71663c78135e5e865621dba53cb (diff) | |
| parent | e040b70da6a48b1eb2f2b32132fa9b2244e92780 (diff) | |
Merge pull request #8 from peggyrayzis/apollo-feedback
Apollo feedback
Diffstat (limited to 'frontend/components/AddToCart.js')
| -rw-r--r-- | frontend/components/AddToCart.js | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/frontend/components/AddToCart.js b/frontend/components/AddToCart.js index 105f558..7cb0856 100644 --- a/frontend/components/AddToCart.js +++ b/frontend/components/AddToCart.js @@ -1,18 +1,23 @@ import { Component } from 'react'; -import { Mutation, Query } from 'react-apollo'; +import { Mutation } from 'react-apollo'; import PropTypes from 'prop-types'; -import { ADD_TO_CART_MUTATION, CURRENT_USER_QUERY } from '../queries/queries.graphql'; +import { + ADD_TO_CART_MUTATION, + CURRENT_USER_QUERY, +} from '../queries/queries.graphql'; class AddToCart extends Component { static propTypes = { id: PropTypes.string.isRequired, }; - update = (proxy, payload) => { + update = (cache, payload) => { const newCartItem = payload.data.addToCart; - const data = proxy.readQuery({ query: CURRENT_USER_QUERY }); + const data = cache.readQuery({ query: CURRENT_USER_QUERY }); - const existingIndex = data.me.cart.findIndex(cartItem => cartItem.id === newCartItem.id); + const existingIndex = data.me.cart.findIndex( + cartItem => cartItem.id === newCartItem.id, + ); if (existingIndex >= 0) { // already in cache, just replace it data.me.cart = [ @@ -23,23 +28,23 @@ class AddToCart extends Component { } else { data.me.cart = [...data.me.cart, newCartItem]; } - proxy.writeQuery({ query: CURRENT_USER_QUERY, data }); + cache.writeQuery({ query: CURRENT_USER_QUERY, data }); }; render() { const { id } = this.props; return ( - <Query query={CURRENT_USER_QUERY}> - {() => ( - <Mutation mutation={ADD_TO_CART_MUTATION} variables={{ id }} update={this.update}> - {(addToCart, { loading }) => ( - <button disabled={loading} onClick={addToCart}> - 🛒 Add{loading && 'ing'} To Cart - </button> - )} - </Mutation> + <Mutation + mutation={ADD_TO_CART_MUTATION} + variables={{ id }} + update={this.update} + > + {(addToCart, { loading }) => ( + <button disabled={loading} onClick={addToCart}> + 🛒 Add{loading && 'ing'} To Cart + </button> )} - </Query> + </Mutation> ); } } |
