summaryrefslogtreecommitdiffstats
path: root/components/AddToCart.js
blob: 0c40efe01a45276be1ec156fa20caeb3d368a3dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 <p>Loading...</p>;
    const cartIds = user.cart.map(item => item.id);
    return (
      <div>

        {
          cartIds.includes(this.props.id)
            ? <button>Added to cart ✅</button>
            : <button onClick={this.addToCart}>Add To Cart 👜</button>
        }
      </div>
    )
  }
}

const userEnhancer = graphql(CURRENT_USER_QUERY, { name: 'currentUserQuery' });
const createOrderEnhancer = graphql(ADD_TO_CART_MUTATION, { name: 'addToCart' });
export default compose(userEnhancer, createOrderEnhancer)(AddToCart);