IMPORTANT: PRIVATE PROPERTY
This source code and all associated documentation, files, and assets are the exclusive property of PkSoft (or the respective private entity owning this repository).
- Unauthorized Access: Accessing this repository without explicit permission is strictly prohibited.
- Unauthorized Use: Copying, modifying, distributing, or using this code for any purpose without written consent is invalid and illegal.
- Legal Consequences: Any unauthorized use, reproduction, or dissemination of this material will be met with immediate legal action to the fullest extent of the law.
This document provides a deep dive into the technical implementation of the Cart and Wishlist flows for the three user types: Random User, Guest User, and Login User.
It focuses on the interaction between State Management (Context), Business Logic (Hooks), and API Services.
File: src/utilits/cookieUtils.ts
The foundation of the entire flow is the resolveUid() and getUserType() functions. Every API call relies on these to tell the backend who is making the request.
| Function | Type | Logic |
|---|---|---|
resolveUid() |
string |
Returns user_id (cookie) OR guest_id (cookie) OR random_user (cookie UUID). |
getUserType() |
enum |
Checks cookies in priority order: user > guest > random. |
Primary Logic File: src/logic/useCartLogic.ts
State File: src/context/CartContext.tsx
The frontend stores Item IDs locally (Cookie) but needs the Server to provide Prices & Details. This process is called "Hydration".
- Read Cookies:
getCartItems()retrieves the list of{ variationId, qty }from thecartcookie. - API Call:
fetchBagis called with:{ "uid": "UUID-1234...", "variations": [{ "variationId": "101", "qty": 1 }] } - Validation:
- The server calculates prices ephemerally (does not save to DB).
- Success: Returns fully hydrated product objects.
- Failure/Empty: If the API fails (e.g., backend ignores random UUIDs), the code FALLS BACK. It creates a "Skeleton Cart" using the cookie data (Price: 0, Name: "Unknown"). See
useCartLogic.tslines 97-113.
- Read Cookies: Optimistically reads
cartcookie. - API Call:
fetchBagis called with:{ "uid": "USER-DB-ID-555", "variations": [{ "variationId": "101", "qty": 1 }] // Pushes local items to server } - Server Action: The server merges the payload items into the user's permanent database cart.
- Response: Returns the new definitive list from the database.
- No Fallback: If the API returns empty, the cart is shown as empty. We trust the server as the detailed Source of Truth for logged-in users.
Function: handleQuantityChange & checkStockStatus
| User Type | Behavior | Utils Used |
|---|---|---|
| Random | Debounced: Updates are delayed by 800ms. logic waits for the user to stop clicking + before asking the server "Is this in stock?". This prevents API spam from anonymous bots/users. |
setTimeout, window.clearTimeout |
| Login/Guest | Immediate: Triggers checkStockStatus instantly. We assume high purchase intent and want to validate stock/price immediately. |
updateCartItem (cookie), fetchBag (API) |
Primary Logic File: src/logic/useWishlistLogic.ts
State File: src/context/WishlistContext.tsx
- Write Operations: Handled by
WishlistContext.tsx. Everything is written to thewishlistcookie first. - Read Operations: Handled by
useWishlistLogic.ts.
- Trigger: Page Load.
- Resolution: Calls
resolveUid(). - API: Calls
fetchWishlistAPI(uid).- Random: Sends UUID. If the backend doesn't support temporary storage for UUIDs, this may return an empty list even if the user just clicked "Heart".
- Login: Sends DB ID. Returns the persistent database list.
- Syncing:
WishlistContextwatches Redux/API state. If an item is removed on the server, theuseEffectat line 75 cleans it up from the local cookie to keep them in sync.
File: src/api/cartWishlistSyncService.ts
This is the most critical flow. It converts a Random User into a Login User without losing their data.
Trigger: Application Mount (App.tsx) or successful Login.
- Identify User:
- If
getUserType() === 'random', ABORT. We do not sync random users to the server (privacy/spam protection). - If
getUserType() === 'user' | 'guest', PROCEED.
- If
- Gather Local Data:
- Calls
getCartItems()->['ItemA', 'ItemB'] - Calls
getWishlistItems()->['ItemC']
- Calls
- The "Push" Payload:
Sends a specialized payload to
CUSTOMER.COUNTendpoint:{ "c_products": ["ItemA", "ItemB"], "w_products": ["ItemC"] } - Server Magic:
- The server takes these IDs and ADDs them to the user's account in the database.
- If the user already had "ItemD" in their DB cart, the result is now "ItemA, ItemB, ItemD".
- Result:
- Returns new
cartCountandwishlistCount. - The UI updates immediately to show the unified total.
- Returns new
| Feature | Random User | Guest / Login User |
|---|---|---|
| Identity | Client-generated UUID (Cookie) | Database ID (Cookie) |
| Cart Storage | Cookie (Fallback: Skeleton) | Database (Primary) |
| Wishlist Storage | Cookie (Primary) | Database (Primary) |
| Stock Checks | Debounced (800ms) | Immediate |
| Persistence | 7 Days (Browser only) | Permanent (Cross-device) |