I've searched for solutions on Google and SO but still cannot find an answer. They all stop at "Add item to cart" or "increase/decrease quantity" but never on calculating the Total, which is annoying!
In my app, there's a list of items where the user can enter the quantity if an item, which updates the its price. All I want to know is how do you sum up all the prices of all items in a cart into a Total price, with Redux and display it in my React app?
Also, if you can point me to any good shopping cart tutorial that actually goes beyond listing products in a cart, I'll be glad.
Action:
/**
* @description UPDATE SINGLE ITEM PRICE WHEN USER ENTER QUANTITY
*
* @param{number} price
* @param{number} quantity - enter by user from input
*/
export const updateItemPrice = (price, quantity) => dispatch => {
const result = price * quantity;
return dispatch({
type: UPDATE_ITEM_PRICE,
subtotal: result
});
};
Reducer:
const INITIAL_STATE = {
total: 0,
subtotal: 0
};
const productsReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
// Update single item price
case Types.UPDATE_ITEM_PRICE:
return {
...state,
subtotal: action.subtotal,
total: // --> Here is where I'm stuck!!
};
default:
return state;
}
};