Closed
Description
Summary:
The evolve and evolveWithMetadata functions in ShoppingCart have some code duplication and lack safety for new event types.
Suggested Improvements
- Removing duplication - Reduce duplication in event handling logic and ensure safety by adding a default case in switch statements
Same logic is used in evolve and evolveMetatadata fn, so we can extract it as following -
const updateCart = (state: ShoppingCart, productItem: PricedProductItem): ShoppingCart => ({
productItems: [...state.productItems, productItem],
totalAmount: state.totalAmount + productItem.price * productItem.quantity,
});
- Bug- Prevent discounts from causing negative amounts
Current
case 'DiscountApplied':
return {
...state,
totalAmount: state.totalAmount * (1 - data.percent / 100),
};
Suggestion
case 'DiscountApplied':
return {
...state,
totalAmount: Math.max(state.totalAmount * (1 - data.percent / 100), 0),
};
Next Steps
- Need review from maintainers on this issue.
- After that I can create a Pr for the changes.
Metadata
Metadata
Assignees
Labels
No labels