Vuex actions are where we should place any logic or asynchronous operations, such as API requests, that don't belong in mutations. As such, they are perfect for enforcing the business rules of our application, such as whether to add a new cart item or update the quantity of an existing item. In the ClientApp/store/actions.js file, add the following exported function:
export const addProductToCart = ({ state, commit }, product) => { const index = state.cart.findIndex( i => i.productId === product.productId && i.colourId === product.colourId && i.storageId === product.storageId ); if (index >= 0) { commit("updateProductQuantity", index); } else { commit("addProductToCart", product); }};
Actions always receive a context ...