Shopify / storefront-api-examples

Example custom storefront applications built on Shopify's Storefront API
https://help.shopify.com/api/storefront-api
MIT License
1.12k stars 329 forks source link

LineItem added everytime you go to the cart #85

Closed dcharua closed 5 years ago

dcharua commented 5 years ago

Hi, there seems to be an issue with the cart in the angular app. Every time you navigate to another route and go back to the cart all the lineitems are added again and duplicate the existing ones. This line of code is triggered twice with the lineItem in NgOnInit cart.component, then three times if you navigate out and in again

    this.globalService.newlineItemObs.subscribe(lineItem => {
      if (lineItem) {
        this.addItem(lineItem);
      }
    })

I tried to unsubnscribe onDestroy() it does not duplicate items any more but than i can not add new items to the cart.

any ideas? Thank you

dcharua commented 5 years ago

There seem to be many solutions to this problem, I ended up implementing the simplest one, avoiding the resubscription my checking if already subscribed, like this

if(this.globalService.newlineItemObs.observers.length == 0) { this.globalService.newlineItemObs.subscribe(lineItem => { if (lineItem) { console.log("new line item"); this.addItem(lineItem); } }) }