import { PureComponent } from 'react'; import { cartAutomat } from '../automats/cartAutomat.js'; /** * CartManager — left column of the Partial Subscription example. * * Full subscriber to cartAutomat. * Allows adding/removing items (changing list length) AND modifying * existing item content (quantity, name, coupon) without changing list length. */ class CartManager extends PureComponent { _renderCount = 0; constructor(props) { super(props); this.state = cartAutomat.state; } componentDidMount() { this.unsubscribe = cartAutomat.subscribe(this); } componentWillUnmount() { if (this.unsubscribe) this.unsubscribe(); } handleAddItem = (name, price) => { cartAutomat.actions.addItem(name, price); }; handleRemoveItem = (id) => { cartAutomat.actions.removeItem(id); }; handleQuantityChange = (id, delta) => { cartAutomat.actions.updateQuantity(id, delta); }; handleCouponChange = (e) => { cartAutomat.actions.setCouponCode(e.target.value); }; handleReset = () => { cartAutomat.actions.resetCart(); }; render() { this._renderCount++; const { items, couponCode } = this.state; return (
🛒

Shopping Cart Manager

💾 IndexedDB Full Subscriber
Component Renders: #{this._renderCount}
{/* Quick Add Buttons (Changes length) */}
Add Item (Changes List Length):
{/* Items List */}
Cart Items ({items.length}): {items.length === 0 ? (
Cart is empty. Click an add button above.
) : (
{items.map((item) => (
{item.name}
${item.price} each
{/* Quantity controls (Changes content, NOT length!) */}
Qty: {item.quantity} {/* Remove item (Changes length!) */}
))}
)}
{/* Unrelated field: Coupon Code */}
Typing here calls cartAutomat.setState({`{ couponCode }`}). Notice how the badge on the right ignores this completely!
); } } export default CartManager;