From 1450e579c67e3d969cb099dfe6c1cefd1e313fb5 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Fri, 15 Sep 2017 14:26:13 -0400 Subject: yep --- components/AddToCart.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 components/AddToCart.js (limited to 'components/AddToCart.js') diff --git a/components/AddToCart.js b/components/AddToCart.js new file mode 100644 index 0000000..0c40efe --- /dev/null +++ b/components/AddToCart.js @@ -0,0 +1,39 @@ +import { Component } from 'react'; +import { ADD_TO_CART_MUTATION, CURRENT_USER_QUERY } from '../queries'; +import { graphql, compose } from 'react-apollo'; + +class AddToCart extends Component { + componentDidMount() { + this.props.currentUserQuery.refetch(); + } + + addToCart = async () => { + const res = await this.props.addToCart({ + variables: { + userId: this.props.currentUserQuery.user.id, + itemId: this.props.id, + } + }); + this.props.currentUserQuery.refetch(); + console.log(res) + } + render() { + const user = this.props.currentUserQuery.user; + if (!user) return

Loading...

; + const cartIds = user.cart.map(item => item.id); + return ( +
+ + { + cartIds.includes(this.props.id) + ? + : + } +
+ ) + } +} + +const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUserQuery' }); +const createOrderEnhancer = graphql(ADD_TO_CART_MUTATION, { name: 'addToCart' }); +export default compose(userEnhancer, createOrderEnhancer)(AddToCart); -- cgit v1.3