@diffsome/react 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -98,6 +98,10 @@ var getEnvVar = (key) => {
98
98
  };
99
99
  function DiffsomeProvider({ children, config = {} }) {
100
100
  const [isReady, setIsReady] = (0, import_react.useState)(false);
101
+ const [cart, setCart] = (0, import_react.useState)(null);
102
+ const [cartLoading, setCartLoading] = (0, import_react.useState)(true);
103
+ const [wishlistItems, setWishlistItems] = (0, import_react.useState)([]);
104
+ const [wishlistLoading, setWishlistLoading] = (0, import_react.useState)(true);
101
105
  const tenantId = config.tenantId || getEnvVar("DIFFSOME_TENANT_ID");
102
106
  const apiKey = config.apiKey || getEnvVar("DIFFSOME_API_KEY");
103
107
  const baseUrl = config.baseUrl || getEnvVar("DIFFSOME_BASE_URL");
@@ -115,13 +119,59 @@ function DiffsomeProvider({ children, config = {} }) {
115
119
  storageType: config.storageType ?? "localStorage"
116
120
  });
117
121
  }, [tenantId, baseUrl, apiKey, config.persistToken, config.storageType]);
122
+ const refreshCart = (0, import_react.useCallback)(async () => {
123
+ setCartLoading(true);
124
+ try {
125
+ const cartData = await client.shop.getCart();
126
+ setCart(cartData);
127
+ } catch (err) {
128
+ setCart(null);
129
+ } finally {
130
+ setCartLoading(false);
131
+ }
132
+ }, [client]);
133
+ const refreshWishlist = (0, import_react.useCallback)(async () => {
134
+ if (!client.isAuthenticated()) {
135
+ setWishlistItems([]);
136
+ setWishlistLoading(false);
137
+ return;
138
+ }
139
+ setWishlistLoading(true);
140
+ try {
141
+ const response = await client.shop.getWishlist({ per_page: 100 });
142
+ setWishlistItems(response.data);
143
+ } catch (err) {
144
+ setWishlistItems([]);
145
+ } finally {
146
+ setWishlistLoading(false);
147
+ }
148
+ }, [client]);
118
149
  (0, import_react.useEffect)(() => {
119
150
  setIsReady(true);
120
- }, []);
151
+ refreshCart();
152
+ refreshWishlist();
153
+ }, [refreshCart, refreshWishlist]);
154
+ (0, import_react.useEffect)(() => {
155
+ const handleAuthChange = () => {
156
+ refreshWishlist();
157
+ };
158
+ if (typeof window !== "undefined") {
159
+ window.addEventListener("storage", handleAuthChange);
160
+ return () => window.removeEventListener("storage", handleAuthChange);
161
+ }
162
+ }, [refreshWishlist]);
121
163
  const value = (0, import_react.useMemo)(() => ({
122
164
  client,
123
- isReady
124
- }), [client, isReady]);
165
+ isReady,
166
+ cart,
167
+ cartLoading,
168
+ setCart,
169
+ refreshCart,
170
+ wishlistItems,
171
+ wishlistLoading,
172
+ setWishlistItems,
173
+ refreshWishlist
174
+ }), [client, isReady, cart, cartLoading, refreshCart, wishlistItems, wishlistLoading, refreshWishlist]);
125
175
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DiffsomeContext.Provider, { value, children });
126
176
  }
127
177
  function useDiffsome() {
@@ -280,25 +330,7 @@ function useSocialAuth() {
280
330
  // src/hooks/useCart.ts
281
331
  var import_react4 = require("react");
282
332
  function useCart() {
283
- const client = useClient();
284
- const [cart, setCart] = (0, import_react4.useState)(null);
285
- const [loading, setLoading] = (0, import_react4.useState)(true);
286
- const [error, setError] = (0, import_react4.useState)(null);
287
- const fetchCart = (0, import_react4.useCallback)(async () => {
288
- try {
289
- const cartData = await client.shop.getCart();
290
- setCart(cartData);
291
- setError(null);
292
- } catch (err) {
293
- setCart(null);
294
- setError(err instanceof Error ? err : new Error("Failed to fetch cart"));
295
- } finally {
296
- setLoading(false);
297
- }
298
- }, [client]);
299
- (0, import_react4.useEffect)(() => {
300
- fetchCart();
301
- }, [fetchCart]);
333
+ const { client, cart, cartLoading, setCart, refreshCart } = useDiffsome();
302
334
  const addToCart = (0, import_react4.useCallback)(async (productId, quantity = 1, variantId, options) => {
303
335
  const data = {
304
336
  product_id: productId,
@@ -309,25 +341,21 @@ function useCart() {
309
341
  const updatedCart = await client.shop.addToCart(data);
310
342
  setCart(updatedCart);
311
343
  return updatedCart;
312
- }, [client]);
344
+ }, [client, setCart]);
313
345
  const updateItem = (0, import_react4.useCallback)(async (itemId, quantity) => {
314
346
  const updatedCart = await client.shop.updateCartItem(itemId, { quantity });
315
347
  setCart(updatedCart);
316
348
  return updatedCart;
317
- }, [client]);
349
+ }, [client, setCart]);
318
350
  const removeItem = (0, import_react4.useCallback)(async (itemId) => {
319
351
  const updatedCart = await client.shop.removeFromCart(itemId);
320
352
  setCart(updatedCart);
321
353
  return updatedCart;
322
- }, [client]);
354
+ }, [client, setCart]);
323
355
  const clearCart = (0, import_react4.useCallback)(async () => {
324
356
  await client.shop.clearCart();
325
357
  setCart(null);
326
- }, [client]);
327
- const refresh = (0, import_react4.useCallback)(async () => {
328
- setLoading(true);
329
- await fetchCart();
330
- }, [fetchCart]);
358
+ }, [client, setCart]);
331
359
  const isInCart = (0, import_react4.useCallback)((productId, variantId) => {
332
360
  if (!cart?.items) return false;
333
361
  return cart.items.some(
@@ -349,13 +377,12 @@ function useCart() {
349
377
  subtotal: cart?.subtotal ?? 0,
350
378
  shippingFee: cart?.shipping_fee ?? 0,
351
379
  total: cart?.total ?? 0,
352
- loading,
353
- error,
380
+ loading: cartLoading,
354
381
  addToCart,
355
382
  updateItem,
356
383
  removeItem,
357
384
  clearCart,
358
- refresh,
385
+ refresh: refreshCart,
359
386
  isInCart,
360
387
  getItemQuantity
361
388
  };
@@ -364,102 +391,63 @@ function useCart() {
364
391
  // src/hooks/useWishlist.ts
365
392
  var import_react5 = require("react");
366
393
  function useWishlist() {
367
- const client = useClient();
368
- const [items, setItems] = (0, import_react5.useState)([]);
369
- const [loading, setLoading] = (0, import_react5.useState)(true);
394
+ const { client, wishlistItems, wishlistLoading, setWishlistItems, refreshWishlist, setCart } = useDiffsome();
370
395
  const [error, setError] = (0, import_react5.useState)(null);
371
- const [wishlistMap, setWishlistMap] = (0, import_react5.useState)({});
372
- const fetchWishlist = (0, import_react5.useCallback)(async () => {
373
- if (!client.isAuthenticated()) {
374
- setItems([]);
375
- setWishlistMap({});
376
- setLoading(false);
377
- return;
378
- }
379
- try {
380
- const response = await client.shop.getWishlist({ per_page: 100 });
381
- setItems(response.data);
382
- const map = {};
383
- response.data.forEach((item) => {
384
- const key = item.variant_id ? `${item.product_id}_${item.variant_id}` : String(item.product_id);
385
- map[key] = true;
386
- });
387
- setWishlistMap(map);
388
- setError(null);
389
- } catch (err) {
390
- setItems([]);
391
- setWishlistMap({});
392
- setError(err instanceof Error ? err : new Error("Failed to fetch wishlist"));
393
- } finally {
394
- setLoading(false);
395
- }
396
- }, [client]);
397
- (0, import_react5.useEffect)(() => {
398
- fetchWishlist();
399
- }, [fetchWishlist]);
396
+ const wishlistMap = (0, import_react5.useMemo)(() => {
397
+ const map = {};
398
+ wishlistItems.forEach((item) => {
399
+ const key = item.variant_id ? `${item.product_id}_${item.variant_id}` : String(item.product_id);
400
+ map[key] = true;
401
+ });
402
+ return map;
403
+ }, [wishlistItems]);
400
404
  const isInWishlist = (0, import_react5.useCallback)((productId, variantId) => {
401
405
  const key = variantId ? `${productId}_${variantId}` : String(productId);
402
406
  return wishlistMap[key] || false;
403
407
  }, [wishlistMap]);
404
408
  const toggleWishlist = (0, import_react5.useCallback)(async (productId, variantId) => {
405
409
  const result = await client.shop.toggleWishlist(productId, variantId);
406
- const key = variantId ? `${productId}_${variantId}` : String(productId);
407
- setWishlistMap((prev) => ({
408
- ...prev,
409
- [key]: result.in_wishlist
410
- }));
411
410
  if (result.action === "added") {
412
- await fetchWishlist();
411
+ await refreshWishlist();
413
412
  } else {
414
- setItems((prev) => prev.filter(
413
+ setWishlistItems(wishlistItems.filter(
415
414
  (item) => !(item.product_id === productId && item.variant_id === variantId)
416
415
  ));
417
416
  }
418
417
  return result;
419
- }, [client, fetchWishlist]);
418
+ }, [client, refreshWishlist, setWishlistItems, wishlistItems]);
420
419
  const addToWishlist = (0, import_react5.useCallback)(async (productId, variantId, note) => {
421
420
  const item = await client.shop.addToWishlist({
422
421
  product_id: productId,
423
422
  variant_id: variantId,
424
423
  note
425
424
  });
426
- await fetchWishlist();
425
+ await refreshWishlist();
427
426
  return item;
428
- }, [client, fetchWishlist]);
427
+ }, [client, refreshWishlist]);
429
428
  const removeFromWishlist = (0, import_react5.useCallback)(async (wishlistId) => {
430
- const item = items.find((i) => i.id === wishlistId);
431
429
  await client.shop.removeFromWishlist(wishlistId);
432
- if (item) {
433
- const key = item.variant_id ? `${item.product_id}_${item.variant_id}` : String(item.product_id);
434
- setWishlistMap((prev) => {
435
- const next = { ...prev };
436
- delete next[key];
437
- return next;
438
- });
439
- }
440
- setItems((prev) => prev.filter((i) => i.id !== wishlistId));
441
- }, [client, items]);
430
+ setWishlistItems(wishlistItems.filter((i) => i.id !== wishlistId));
431
+ }, [client, setWishlistItems, wishlistItems]);
442
432
  const moveToCart = (0, import_react5.useCallback)(async (wishlistIds) => {
443
433
  const result = await client.shop.moveWishlistToCart(wishlistIds);
444
- await fetchWishlist();
434
+ await refreshWishlist();
435
+ const cartData = await client.shop.getCart();
436
+ setCart(cartData);
445
437
  return result;
446
- }, [client, fetchWishlist]);
438
+ }, [client, refreshWishlist, setCart]);
447
439
  const updateNote = (0, import_react5.useCallback)(async (wishlistId, note) => {
448
440
  const item = await client.shop.updateWishlistNote(wishlistId, note);
449
- setItems((prev) => prev.map((i) => i.id === wishlistId ? item : i));
441
+ setWishlistItems(wishlistItems.map((i) => i.id === wishlistId ? item : i));
450
442
  return item;
451
- }, [client]);
443
+ }, [client, setWishlistItems, wishlistItems]);
452
444
  const getProductWishlistCount = (0, import_react5.useCallback)(async (productSlug) => {
453
445
  return await client.shop.getProductWishlistCount(productSlug);
454
446
  }, [client]);
455
- const refresh = (0, import_react5.useCallback)(async () => {
456
- setLoading(true);
457
- await fetchWishlist();
458
- }, [fetchWishlist]);
459
447
  return {
460
- items,
461
- count: items.length,
462
- loading,
448
+ items: wishlistItems,
449
+ count: wishlistItems.length,
450
+ loading: wishlistLoading,
463
451
  error,
464
452
  isInWishlist,
465
453
  toggleWishlist,
@@ -468,7 +456,7 @@ function useWishlist() {
468
456
  moveToCart,
469
457
  updateNote,
470
458
  getProductWishlistCount,
471
- refresh
459
+ refresh: refreshWishlist
472
460
  };
473
461
  }
474
462