Skip to content

[FIX] Persist cart state to localStorage on page refresh - #274

Open
Siddh2024 wants to merge 1 commit into
rdodiya:gssoc_developfrom
Siddh2024:fix/268-cart-persistence
Open

Siddh2024 wants to merge 1 commit into
rdodiya:gssoc_developfrom
Siddh2024:fix/268-cart-persistence

Conversation

@Siddh2024

Copy link
Copy Markdown
Contributor

Description

Fixes #268

The cart state was previously held only in memory and lost on page refresh. This PR adds localStorage persistence so cart items survive page reloads.

Changes Made

CartContext (src/context/CartContext.jsx)

  • Created CartContext with localStorage persistence using 'restrohub-cart' key
  • Provides addItem, removeItem, updateQuantity, clearCart operations
  • Exposes itemCount and totalAmount derived values
  • Auto-saves to localStorage on every state change
  • On mount, restores cart from localStorage
  • Uses useRef hydration guard to prevent SSR issues

CartDrawer (src/components/customer/CartDrawer.jsx)

  • Slide-in drawer showing cart items with name, price, quantity controls
  • Quantity increment/decrement and remove buttons per item
  • Clear cart and total display
  • Empty state with helpful message
  • Overlay backdrop closes drawer on click

Navigation & MenuSection Updates

  • Added cart button with SVG icon and item count badge to Navigation
  • Added "Add to Cart" button on each menu item in MenuSection
  • Wrapped CustomerLayout with CartProvider

Testing

  1. Open restaurant menu page
  2. Add items to cart via "Add to Cart" buttons
  3. Open cart drawer via cart button in navigation
  4. Verify items, quantities, total display correctly
  5. Refresh the page - cart items should persist from localStorage
  6. Clear cart - items should be removed from localStorage

…rdodiya#268)

- Created CartContext with localStorage persistence for cart items
- CartProvider wraps CustomerLayout so cart is available on all customer pages
- Added CartDrawer component with item list, quantity controls, remove, and clear
- Added cart button with badge in Navigation showing item count
- Added "Add to Cart" buttons on menu items in MenuSection
- Cart state auto-saves to localStorage on every change
- On page load, cart is restored from localStorage
@rdodiya
rdodiya requested review from kashyapRathod03 and a lite review from Copilot August 23, 2026 10:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There is a persistence-breaking bug with falsy foodId values (e.g., 0) and a couple of user-facing issues (currency inconsistency and missing accessible labels) that should be fixed before merge.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR introduces a customer cart feature backed by a new CartContext that persists cart contents to localStorage, allowing the cart to survive page refreshes (addressing issue #268).

Changes:

  • Added a CartProvider/useCart context with localStorage load/save behavior.
  • Introduced a slide-in CartDrawer UI and wired it into the restaurant menu page via a cart button in Navigation.
  • Added “Add to Cart” actions in MenuSection and wrapped customer routes with CartProvider in CustomerLayout.
File summaries
File Description
RestroHub-FrontEnd/src/pages/customer/RestaurantMenu.jsx Adds cart drawer open/close state and wires Navigation cart button to open the drawer.
RestroHub-FrontEnd/src/layouts/CustomerLayout.jsx Wraps customer pages with CartProvider so cart state is available throughout the customer area.
RestroHub-FrontEnd/src/context/CartContext.jsx Implements persisted cart state, cart operations, and derived totals/counts.
RestroHub-FrontEnd/src/components/customer/Navigation.jsx Adds cart button with badge showing item count and triggers drawer open via callback prop.
RestroHub-FrontEnd/src/components/customer/MenuSection.jsx Adds “Add to Cart” button per menu item and uses cart context to add items.
RestroHub-FrontEnd/src/components/customer/CartDrawer.jsx New drawer UI for viewing/updating/removing items and clearing the cart.
Review details

Suppressed comments (1)

RestroHub-FrontEnd/src/components/customer/MenuSection.jsx:74

  • Menu prices are rendered with a $ prefix here, but the rest of the app (including the new cart drawer) uses , leading to inconsistent currency display for the same items.
                            <p className="menu-item-price font-heading">
                                {item.price === 'varies' ? item.price : `$${item.price}`}
                            </p>
  • Files reviewed: 6/6 changed files
  • Comments generated: 4
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +11 to +14
const parsed = JSON.parse(raw);
if (!Array.isArray(parsed)) return [];
return parsed.filter(item => item && item.foodId);
} catch {
Comment on lines +59 to +66
<button
onClick={() => addItem({
foodId: item.foodId || item.id || index,
name: item.name,
price: item.price === 'varies' ? 0 : parseFloat(item.price) || 0,
imageUrl: item.imageUrl || '',
isVeg: item.isVeg ?? true,
})}
Comment on lines +138 to +140
<button
onClick={() => removeItem(item.foodId)}
style={{
Comment on lines +46 to +48
<button
onClick={onClose}
style={{
@rdodiya

rdodiya commented Aug 23, 2026

Copy link
Copy Markdown
Owner

Hi @Siddh2024 ,
Please review above points and resolve conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[UX] Cart state not preserved on page refresh

4 participants