// features/cart/cartSlice.js import { createSlice } from '@reduxjs/toolkit'; const cartSlice = createSlice({ name: 'cart', initialState: { items: [], total: 0 }, reducers: { addItem: (state, action) => { const existingItem = state.items.find( item => item.id === action.payload.id ); if (existingItem) { existingItem.quantity += 1; } else { state.items.push({ ...action.payload, quantity: 1 }); } state.total = state.items.reduce( (sum, item) => sum + item.price * item.quantity, 0 ); }, removeItem: (state, action) => { state.items = state.items.filter(item => item.id !== action.payload); state.total = state.items.reduce( (sum, item) => sum + item.price * item.quantity, 0 ); }, updateQuantity: (state, action) => { const { id, quantity } = action.payload; const item = state.items.find(item => item.id === id); if (item) { item.quantity = quantity; if (item.quantity <= 0) { state.items = state.items.filter(i => i.id !== id); } } state.total = state.items.reduce( (sum, item) => sum + item.price * item.quantity, 0 ); }, clearCart: (state) => { state.items = []; state.total = 0; } } }); export const { addItem, removeItem, updateQuantity, clearCart } = cartSlice.actions; export default cartSlice.reducer;