Modifying state array in reducer – calculation gets executed twice

const addToCartReducer = (state, action)=>{

    console.log('executing reducer')
    // const ItemsFilter = (itemIndexer) =>{
    //     if ((itemIndexer.restaurant == action.item.restaurant) && (itemIndexer.menu == action.item.menu)){
    //         return itemIndexer
    //     }
    // }

    const existingItemIndex = state.items.findIndex(
        (item) =>
          item.restaurant === action.item.restaurant &&
          item.menu === action.item.menu
      );

    let newState = {...state}
    let newStateItems = {...newState.items}
    if (action.type === 'ITEM_ADDED'){
        console.log('executing ITEM_ADDED')
        // let existingItem = newState.items.filter(ItemsFilter)
        if (existingItemIndex >= 0){
            // newState.items[existingItemIndex].quantity += parseInt(action.item.quantity)
            newStateItems[existingItemIndex].quantity += parseInt(action.item.quantity)
            newState.items = newStateItems
        }
        else {
            newState.items = newState.items.concat({...action.item, quantity: parseInt(action.item.quantity)})
        }
         // Update the totalAmount and totalItems using the updated quantity of the item

    newState.totalItems = parseInt(newState.totalItems) + parseInt(action.item.quantity);
        
    }
    console.log(newState)
    return newState
}

This is a code of a React component that provides the state and dispatch functions for updating the state of a shopping cart using the useReducer hook. The state of the shopping cart contains two properties: totalItems, and items. The items property is an array of objects representing the items in the cart, each with the properties: restaurant, menu, quantity, price.

The component exports a provider component, AddToCartProvider, that wraps the children components and provides the state and dispatch functions for adding and removing items from the cart. The dispatch functions are triggered by the addItemToCart and removeItemFromCart handlers.

When an ITEM_ADDED action is dispatched, the addToCartReducer function is executed, and it updates the state by either increasing the quantity of an existing item in the cart (if it already exists) or adding a new item to the cart. The totalAmount and totalItems are also updated accordingly.

When this code tries to update the quantity of an existing item in the cart (if it already exists), the quantity calculation gets executed twice with action.item.quantity

Case 1:

newState = {totalItems:1, items:[{restaurant: 'R1', menu: 'F1', quantity: 1}]

//Now, if I dispatch an object {type:'ITEM_ADDED', item:{restaurant:'R1', menu:'F1', quantity:2}}

//I am expecting newState to be changed to
//Expected value for newState
newState = {totalItems:3, items:[{restaurant: 'R1', menu: 'F1', quantity: 3}]

//Actual value for newState
newState = {totalItems:3, items:[{restaurant: 'R1', menu: 'F1', quantity: 5}]