Skip to content

Commit 2b841a1

Browse files
committed
removed variants feature
1 parent 3e14ab1 commit 2b841a1

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

frontend/src/lib/cart.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { Cart, AddToCartInput } from "@/lib/types"
2+
import { emitCartUpdated } from "@/lib/cart-events" // 1. Import the event emitter
23

34
// FIX: Direct Backend URL to bypass Next.js Proxy (port 3000)
45
const CART_URL = "http://localhost:8081/v1/cart"
56

67
// --- SESSION MANAGEMENT ---
78
function getSessionId(): string {
8-
if (typeof window === 'undefined') return "" // Server-side fallback
9+
if (typeof window === 'undefined') return ""
910

1011
let sid = localStorage.getItem("sabhyatam_session")
1112
if (!sid) {
12-
// Generate a new UUID if one doesn't exist
1313
sid = crypto.randomUUID()
1414
localStorage.setItem("sabhyatam_session", sid)
1515
}
@@ -20,14 +20,10 @@ function getHeaders() {
2020
const headers: Record<string, string> = {
2121
"Content-Type": "application/json",
2222
}
23-
24-
// Attach the Session ID to every request
25-
// This ensures the backend identifies the user correctly even without cookies
2623
const sid = getSessionId()
2724
if (sid) {
2825
headers["X-SESSION-ID"] = sid
2926
}
30-
3127
return headers
3228
}
3329

@@ -52,7 +48,10 @@ export async function addToCart(item: AddToCartInput): Promise<Cart> {
5248
})
5349

5450
await handleResponse(res)
55-
// Fetch fresh cart to ensure UI sync
51+
52+
// 2. Emit event so Header updates immediately
53+
emitCartUpdated()
54+
5655
return getCart()
5756
}
5857

@@ -63,8 +62,7 @@ export async function getCart(): Promise<Cart> {
6362
})
6463

6564
if (!res.ok) {
66-
// Return empty cart structure on error/404
67-
return { id: "", items: [], subtotal: 0, tax: 0, total: 0 } as any
65+
return { id: "", items: [], subtotal: 0, item_count: 0, currency: "INR" } as any
6866
}
6967
return res.json()
7068
}
@@ -75,7 +73,13 @@ export async function removeFromCart(productId: string) {
7573
headers: getHeaders(),
7674
body: JSON.stringify({ product_id: productId }),
7775
})
78-
return handleResponse(res)
76+
77+
const data = await handleResponse(res)
78+
79+
// 3. Emit event on remove
80+
emitCartUpdated()
81+
82+
return data
7983
}
8084

8185
export async function updateCartItem(productId: string, quantity: number) {
@@ -87,13 +91,25 @@ export async function updateCartItem(productId: string, quantity: number) {
8791
quantity: quantity
8892
}),
8993
})
90-
return handleResponse(res)
94+
95+
const data = await handleResponse(res)
96+
97+
// 4. Emit event on update
98+
emitCartUpdated()
99+
100+
return data
91101
}
92102

93103
export async function clearCart() {
94104
const res = await fetch(`${CART_URL}/clear`, {
95105
method: "POST",
96106
headers: getHeaders(),
97107
})
98-
return handleResponse(res)
108+
109+
const data = await handleResponse(res)
110+
111+
// 5. Emit event on clear
112+
emitCartUpdated()
113+
114+
return data
99115
}

0 commit comments

Comments
 (0)