question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor out cartItems (Discussion)

See original GitHub issue

increaseItemByOne for the same reason we have reduceItemByOne

As the code currently stands, we have a reduceItemByOne(sku) that handles removing one item from the cartItems subsequently causing cartDetails to update. This makes decreasing the number of items in a cart very intuitive from a developer standpoint. By contrast, it’s not very intuitive that you’re supposed to re-use addItem(product) since at this point in the code you don’t have access to your original product object and instead you’re likely operating on a cartEntry (entry in cartDetails).

This could be easily implemented as it’s only to improve DX, however, I also have another proposal:

Replace cartItems with cartDetails entirely?

Currently, cartItems is only used to generate cartDetails, totalPrice, and cartCount. I’m proposing that we do away with cartItems in favor of a more event based approach to creating and modifying the above pieces of data.

For example, instead of adding an item to the cart and generating our data from cartItems with formatDetailedCart, calculateTotalValue, and cartCount we could use reducers that handle the events in different ways.

For the calculation of cartCount it would be something similar to the following:

function cartCountReducer(value, action) {
  switch (action.type) {
    case 'addItemToCart':
    case 'incrementItemByOne':
      return value + 1
    case 'reduceItemByOne':
      return value - 1
    default:
       return value
  }
}

And of course, we need to handle certain things like if reduceItemByOne or `incrementItemByOne are called with an invalid sku. This can be handled inside of the combined dispatch function like so:

function dispatch(action) {
  // increment or reduce item by one only if there's a matching `sku` in `cartDetails`
  if ((action.type === 'incrementItemByOne' || action.type === 'reduceItemByOne') && !(action.sku in cartDetails)) {
    return
  }

  cartCountDispatch(action)
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
thorsten-stripecommented, Apr 29, 2020

Yes, that’s a good call, thanks @ChrisBrownie55 👍

1reaction
andria-devcommented, Apr 28, 2020

@thorsten-stripe is the following also an acceptable format?

function incrementItem(sku: string, count?: number = 1) {}

I find that it’s easier when you only have two arguments to just keep them separate because their names and types show up in most editors as soon as you begin using the function making dev much easier.

Also I believe it reads much better when analyzing the source code:

increment the item with sku by count

Whereas objects don’t require a specific order so you might end up with:

increment the item by count with sku

Read more comments on GitHub >

github_iconTop Results From Across the Web

Checkout: Refactor shopping cart plan items creation ... - GitHub
We should refactor this code to hide that slug, and generate objects that can be compared (currently it's possible to do cartItems.
Read more >
Refactor to Visitors - Digitalact
In this post, I will demonstrate how to refactor a business logic outside of the main object by utilizing a visitor pattern.
Read more >
Refactoring Code: When to do what? - Stack Overflow
Obviously clean code is subjective, but I'm talking about when to use things (not how to use them) such as polymorphism, inheritance, interfaces,...
Read more >
Refactoring Tools: Tactic Git for Smoother Refactoring
Today we will discuss how to use Git to make refactoring easier and simplify future work with the code. Use Small Atomic Commits....
Read more >
Refactoring: This class is too large - Martin Fowler
Refactoring : This class is too large. An example of refactoring from a real (flawed) code base. In this article I walk through...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found