Ria Choi
← Portfolio

Shopping Cart

A React and Redux Toolkit project about sharing cart state across the shop, product cards, and checkout summary.

Shopping Cart

I built Shopping Cart to practice managing state that belongs to an entire React application. A product can be added from the catalog, changed or removed on the cart page, and included in the checkout summary. I wanted every view to show the same selection immediately, with one place to maintain the rules for changing it.

Putting the cart rules in a Redux Toolkit slice

The configured store registers a cart reducer. Product cards dispatch addItem, and cart rows dispatch updateQuantity or removeItem. The slice checks whether a product name already exists. Adding it again increases the quantity instead of creating a duplicate line. Redux Toolkit uses Immer to turn the draft updates into immutable state changes.

JavaScript
import { createSlice } from '@reduxjs/toolkit';

export const CartSlice = createSlice({
    name: 'cart',
    initialState: {
        items:[],
    },
    reducers: {
        addItem: (state, action) => {
            const { name, image, price, description } = action.payload;

            const existingItem = state.items.find(item => item.name === name);

            if (existingItem) {
                existingItem.quantity++
            } else {
                state.items.push({ name, image, price, description, quantity: 1 });
            }
        },
        removeItem: (state, action) => {
            state.items = state.items.filter(item => item.name !== action.payload);
        },
        updateQuantity: (state, action) => {
            const { name, quantity } = action.payload;

            const itemToUpdate = state.items.find(item => item.name === name);
            if(itemToUpdate) {
                itemToUpdate.quantity = quantity;
            }
        },

Deriving the order summary from the same store

OrderSummary reads cart.items with useSelector and derives the subtotal by multiplying each unit price by its quantity. Keeping the subtotal derived lets the summary follow changes in the same items that the cart renders. The empty cart, product page, and order summary can stay separate without duplicating the cart state.

JSX
const OrderSummary = () => {
    const items = useSelector((state) => state.cart.items ?? []);
    const calculateTotalAmount = () => {
        return items
            .reduce((sum, item) => sum + parseFloat(item.price) * item.quantity, 0)
            .toFixed(2);
    };

Passing the selection to checkout

The Vite client sends the cart to an Express route for creation of a Stripe Checkout session. The response gives the client the URL for the hosted payment step. This project is a portfolio prototype. A production store would resolve prices from a server-side catalog and confirm order fulfillment from Stripe webhooks.

What I learned

This project helped me decide what belongs in shared state. Cart lines and quantities need to be available across views. Totals can be calculated from those lines, and transient controls can stay local to their components.