title: Redux can reducer accept multiple actions
body: <p>Both <strong>AddToCart</strong> and <strong>RemoveFromCart</strong> actions update num and qty, while both <strong>HandleClick</strong> and <strong>InputQuantity</strong> actions update qty.</p>
  
  <p>I have problem with these two lines of code: </p>
  
  <p><code>num: productsReducer(state.productsReducer.num, AddToCart)</code></p>
  
  <p><code>qty: productsReducer(state.productsReducer.qty, AddToCart)</code></p>
  
  <p>where prop num or qty is only updated by AddToCart action. </p>
  
  <p>The other 3 actions - RemoveFromCart, HandleClick, InputQuantity - do not update prop num or qty. And I have no idea how to allow the above actions update prop num and qty.</p>
  
  <p>How to write productsReducer or my actions such that num and qty are both updated by AddToCart and RemoveFromCart, while qty is updated by HandleClick and InputQuantity? </p>
  
  <p>Do I need a higher-order reducer to handle multiple actions? Please help.</p>
  
  <p><strong>CartContainer</strong></p>
  
  <pre><code>// Both AddToCart and RemoveFromCart actions update num and qty
  // Both HandleClick and InputQuantity actions update qty
  
  const mapStateToProps = state =&gt; ({
    num: productsReducer(state.productsReducer.num, AddToCart),
    qty: productsReducer(state.productsReducer.qty, AddToCart),
    products: state.productsReducer.products
  });
  </code></pre>
  
  <p><strong>ProductsReducer</strong></p>
  
  <pre><code>import * as actionTypes from '../actions/actions';
  
  const initialState = {
    products: [],
    num: [],
    qty: [],
    totalPrice: 0,
    status: ''
  };
  
  const productsReducer = (state = initialState, action) =&gt; {
    let updatedNum = state.num;
    let updatedQty = state.qty;
    let index;
  
    if (action.num !== undefined) {
      index = updatedNum.indexOf(action.num);
    }
  
    switch (action.type) {
      case actionTypes.FETCH_PRODUCTS_REQUEST:
        return {
          ...state,
          status: action.status
        }
  
      case actionTypes.FETCH_PRODUCTS_SUCCESS:
        return {
          ...state,
          status: action.status,
          products: action.products
        }
  
      case actionTypes.ADD_TO_CART:
        // if num array doesn't have that element, insert that element
        if (!state.num.includes(action.num)) {
          updatedNum = state.num.concat(action.num); 
          updatedQty = state.qty.concat(1);
  
          return {
            ...state,
            num: updatedNum,
            qty: updatedQty
          }
        // if element is present in array already, increase qty of it by 1
        } else {
          updatedQty = state.qty.slice(0);
          updatedQty[index] += 1;
  
          return {
            ...state,
            qty: updatedQty
          }
        }
  
        // After updating store, use CalculatePrice action to calculate updated price.
        this.calculateTotalPrice(updatedNum, updatedQty);
  
      case actionTypes.REMOVE_FROM_CART:
        // remove clicked item with splice, and update state
        updatedNum.splice(index, 1);
        updatedQty.splice(index, 1);
  
        return {
          ...state,
          num: updatedNum,
          qty: updatedQty
        }
  
      case actionTypes.HANDLE_CLICK:
        let event = action.eventType;
        console.log(event);
  
        if (event === 'plus') {
          updatedQty[index] += 1;
        } else if (event === 'minus') {
          updatedQty[index] -= 1;
        }
  
        return {
          ...state,
          qty: updatedQty 
        }
  
      case actionTypes.INPUT_QUANTITY:
        const regex = /^[0-9\b]+$/;
  
        if (regex.test(action.event.target.value)) {
          updatedQty[index] = Number(action.event.target.value);
  
          return {
            ...state,
            qty: updatedQty 
          }
        }
  
      case actionTypes.CALCULATE_PRICE:
        let arr = [];
        console.log('updatedNum', action.num);
  
        action.num.map(num =&gt; {
          let index = num - 1;
          arr.push(action.qty[index] * state.products[index].price);
        });
  
        if (arr.length &gt; 0) {
          let total = arr.reduce((acc, currentVal) =&gt; acc + currentVal);
          return { 
            totalPrice: total 
          }
        } 
  
        return { 
          totalPrice: 0 
        }
  
      default: 
        return state;
    }
  }
  
  export default productsReducer;
  </code></pre>