Delayed and incorrect updates from react context to backend
I am creating a food ordering application as project. Here, user can add items from a single restaurant only. If they tried adding items from another restaurant too, they will be shown an alert regarding, and given a choice, 1) Either cancel adding current item, or 2) It will clear your existing cart and add current item.
Suppose I have existing item A from restaurant A in cart and try adding item B from restaurant B in cart now.
Issue I am facing is that when I select option 2), it updates the itemID from A -> B, but restaurantID remains same as restaurant A only, it doesn’t update. In order to update restaurantID, I had to increase/ decrease the item quantity. It would update the restaurantID, but request send to backend will have stale quantity only. Attached associated codes and screenshot below, any help would be appreciated.