Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createEntityAdapter.updateOne removes product from state #4886

Open
SoftwareDesign-Solution opened this issue Mar 10, 2025 · 0 comments
Open

Comments

@SoftwareDesign-Solution

In a training project, I use Redux Toolkit with createEntityAdapter for a shopping cart function.

In the "addToCart" action, the item is added to the state using setOne or the quantity is increased using setOne.

In the "removeFromCart" action, it is first checked whether the item is contained in state.entities. If so, the quantity should be reduced by 1 using updateOne if the quantity is greater than 1. Otherwise, the item should be removed from the state using removeOne.

Everything works fine using npm run dev. Only when I test the shopping cart using Cypress Component Test is the item deleted from the shopping cart using the "removeFromCart" action, even if the quantity is greater than 1. The problem only occurs when I use updateOne.

removeFromCart: (state, action: PayloadAction<Product>) => {

	const { id: productId } = action.payload;

	console.log(`productId: ${productId}`);

	const existingItem = state.entities[productId] as CartItemEntity;

	if (existingItem) {

		console.log(existingItem);

		const { quantity } = existingItem;

		console.log(`quantity: ${quantity}`);

		if (quantity > 1) {
			
			console.log('updateOne');

			cartItemAdapter.updateOne(state, {
				id: productId,
				changes: { quantity: existingItem.quantity - 1 }
			});

		} else {
			console.log('removeOne')
			cartItemAdapter.removeOne(state, action.payload.id);
		}
		
	}
	
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant