thorsten-stripe / ecommerce-gatsby-tutorial

A Gatsby eCommercer starter example
https://gatsby-ecommerce-stripe.netlify.com/
MIT License
88 stars 37 forks source link

Adding additional item doesn't update cart #11

Closed MirelesCloud closed 4 years ago

MirelesCloud commented 5 years ago

I've been following this tutorial for a small project that I'm working on and have noticed that I can add an item only once, when I click the same item twice it won't update the cart. Then when I hit the checkout button I get this error:

(index):1 Uncaught (in promise) IntegrationError: Invalid stripe.redirectToCheckout parameter: items.0.sku is not an accepted parameter.

Everything seems to work fine if I add only one of each item. Still working it out on my own, but any help is appreciated. The bug is presumably somewhere in the Cart component.

`const Cart = class extends React.Component {
    state = {
    cart: [],
   }

  componentDidMount() {
    const existingCart = JSON.parse(
    localStorage.getItem('stripe_checout_items')
    )
   if (existingCart && existingCart.length) {
   this.setState({ cart: existingCart })
   }
 }

 getCart() {
   return this.state.cart
 }

addToCart(newItem) {
  let itemExisted = false
  let updatedCart = this.state.cart.map(item => {
    if (newItem === item.sku) {
      itemExisted = true
      return { sku: item.sku, quanity: ++item.quantity }
    } else {
      return item
    }
 })
if (!itemExisted) {
  updatedCart = [...updatedCart, { sku: newItem, quantity: 1 }]
}
  this.setState({ cart: updatedCart })
  localStorage.setItem('stripe_checkout_items', JSON.stringify(updatedCart))
 }
  render() {
    return (
    <div>
      <Checkout cart={this.state.cart} />
    <div>
      Items in Cart: {this.state.cart.length}
    </div>
    {React.cloneElement(this.props.children, {
      addToCart: this.addToCart.bind(this),
      cart: this.state.cart
    })}
  </div>
   )
 }
}`
thorsten-stripe commented 4 years ago

@MirelesCloud very sorry for the crazy delay, I wasn't monitoring this repo 🙈

I'm not able to reproduce this, setting the quantities works fine for me: image

Were you able to resolve this? Feel free to reopen if not. Sorry again!