Skip to content

Optimize CartContext: memoize derived values, stable action callbacks, and add loading state#19

Open
Samyukta04 wants to merge 10 commits into
MU-Enigma:mainfrom
Samyukta04:Samyukta
Open

Optimize CartContext: memoize derived values, stable action callbacks, and add loading state#19
Samyukta04 wants to merge 10 commits into
MU-Enigma:mainfrom
Samyukta04:Samyukta

Conversation

@Samyukta04

Copy link
Copy Markdown

What

  • Improves performance and user experience by memoizing expensive cart calculations and stabilizing action callbacks.
  • Adds a loading flag to provide clear UI feedback during cart operations and order submission.

##Changes

  1. CartContext.jsx
  • Switched to useReducer for items state (existing).
  • Added useState(loading) and a simulateDelay helper.
  • Wrapped addItem, removeItem, and clearCart with useCallback (returning promises).
  • Used useMemo for totalQuantity and totalAmount.
  • Exposed contextValue via useMemo, including items, callbacks, loading, and totals.
  1. OrderForm.jsx
  • Integrated CartContext.loading and local isLoading to disable inputs/buttons.
  • Mapped customer.postal → "postal-code" for backend compatibility.
  • Read server response message and displayed it through SuccessModal.
  1. SuccessModal.jsx
  • Added a short loading spinner/message before showing the final success message.
  • Included small optional debug logs to verify memoization (removable later)

@Aneeshie

Copy link
Copy Markdown
Collaborator

There are merge conflicts please resolve them.

(there are changes in main branch do your changes on top of them.)

@Samyukta04

Copy link
Copy Markdown
Author

resolved them

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

Successfully merging this pull request may close these issues.

3 participants