@tagadapay/plugin-sdk 1.0.11 โ†’ 1.0.12

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.
@@ -3,7 +3,7 @@ import { useTagadaContext } from '../providers/TagadaProvider';
3
3
  import { getCheckoutToken } from '../utils/urlUtils';
4
4
  import { useCurrency } from '../hooks/useCurrency';
5
5
  export function useCheckout(options = {}) {
6
- const { apiService, updateCheckoutDebugData } = useTagadaContext();
6
+ const { apiService, updateCheckoutDebugData, refreshCoordinator } = useTagadaContext();
7
7
  const { code: currentCurrency } = useCurrency();
8
8
  const [checkout, setCheckout] = useState(null);
9
9
  const [isLoading, setIsLoading] = useState(false);
@@ -147,6 +147,13 @@ export function useCheckout(options = {}) {
147
147
  await getCheckout(currentCheckoutTokenRef.current);
148
148
  console.log('โœ… [useCheckout] Refresh completed, debug data will be updated automatically');
149
149
  }, [getCheckout]);
150
+ // Register refresh function with coordinator and cleanup on unmount
151
+ useEffect(() => {
152
+ refreshCoordinator.registerCheckoutRefresh(refresh);
153
+ return () => {
154
+ refreshCoordinator.unregisterCheckoutRefresh();
155
+ };
156
+ }, [refresh, refreshCoordinator]);
150
157
  const updateAddress = useCallback(async (data) => {
151
158
  if (!checkout?.checkoutSession.id) {
152
159
  throw new Error('No checkout session available');
@@ -250,6 +257,8 @@ export function useCheckout(options = {}) {
250
257
  });
251
258
  if (response.success) {
252
259
  await refresh();
260
+ // Notify other hooks that checkout data changed
261
+ await refreshCoordinator.notifyCheckoutChanged();
253
262
  }
254
263
  return response;
255
264
  }
@@ -257,7 +266,7 @@ export function useCheckout(options = {}) {
257
266
  const error = err instanceof Error ? err : new Error('Failed to update line items');
258
267
  throw error;
259
268
  }
260
- }, [apiService, checkout?.checkoutSession.id, refresh]);
269
+ }, [apiService, checkout?.checkoutSession.id, refresh, refreshCoordinator]);
261
270
  const addLineItems = useCallback(async (lineItems) => {
262
271
  if (!checkout?.checkoutSession.id) {
263
272
  throw new Error('No checkout session available');
@@ -1,7 +1,7 @@
1
1
  import { useState, useCallback, useEffect } from 'react';
2
2
  import { useTagadaContext } from '../providers/TagadaProvider';
3
3
  export function useOrderBump(options) {
4
- const { apiService } = useTagadaContext();
4
+ const { apiService, refreshCoordinator } = useTagadaContext();
5
5
  const { checkoutSessionId, offerId, orderBumpType = 'vip', autoPreview = true } = options;
6
6
  const [isSelected, setIsSelected] = useState(false);
7
7
  const [preview, setPreview] = useState(null);
@@ -55,6 +55,8 @@ export function useOrderBump(options) {
55
55
  if (response.success) {
56
56
  // Refresh preview to get updated savings
57
57
  await refreshPreview();
58
+ // Notify checkout hook that order bump data changed
59
+ await refreshCoordinator.notifyOrderBumpChanged();
58
60
  return { success: true };
59
61
  }
60
62
  else {
@@ -73,7 +75,7 @@ export function useOrderBump(options) {
73
75
  finally {
74
76
  setIsToggling(false);
75
77
  }
76
- }, [checkoutSessionId, offerId, isSelected, apiService, refreshPreview]);
78
+ }, [checkoutSessionId, offerId, isSelected, apiService, refreshPreview, refreshCoordinator]);
77
79
  // Auto-fetch preview on mount and when dependencies change
78
80
  useEffect(() => {
79
81
  if (autoPreview && checkoutSessionId) {
@@ -82,6 +84,13 @@ export function useOrderBump(options) {
82
84
  });
83
85
  }
84
86
  }, [autoPreview, refreshPreview]);
87
+ // Register refresh function with coordinator and cleanup on unmount
88
+ useEffect(() => {
89
+ refreshCoordinator.registerOrderBumpRefresh(refreshPreview);
90
+ return () => {
91
+ refreshCoordinator.unregisterOrderBumpRefresh();
92
+ };
93
+ }, [refreshPreview, refreshCoordinator]);
85
94
  // Calculate current savings
86
95
  const savings = isSelected && preview?.savings ? preview.savings : preview?.savings || null;
87
96
  return {
@@ -25,6 +25,14 @@ interface TagadaContextValue {
25
25
  lastUpdated: Date | null;
26
26
  };
27
27
  updateCheckoutDebugData: (data: any, error?: Error | null, isLoading?: boolean) => void;
28
+ refreshCoordinator: {
29
+ registerCheckoutRefresh: (refreshFn: () => Promise<void>) => void;
30
+ registerOrderBumpRefresh: (refreshFn: () => Promise<void>) => void;
31
+ notifyCheckoutChanged: () => Promise<void>;
32
+ notifyOrderBumpChanged: () => Promise<void>;
33
+ unregisterCheckoutRefresh: () => void;
34
+ unregisterOrderBumpRefresh: () => void;
35
+ };
28
36
  money: {
29
37
  formatMoney: typeof formatMoney;
30
38
  getCurrencyInfo: typeof getCurrencyInfo;
@@ -341,6 +341,51 @@ storeId, accountId, }) {
341
341
  session,
342
342
  });
343
343
  }, [customer, session]);
344
+ // Refresh coordinator for bidirectional hook communication
345
+ const checkoutRefreshRef = useRef(null);
346
+ const orderBumpRefreshRef = useRef(null);
347
+ const refreshCoordinator = {
348
+ registerCheckoutRefresh: (refreshFn) => {
349
+ checkoutRefreshRef.current = refreshFn;
350
+ console.log('๐Ÿ”„ [RefreshCoordinator] Checkout refresh function registered');
351
+ },
352
+ registerOrderBumpRefresh: (refreshFn) => {
353
+ orderBumpRefreshRef.current = refreshFn;
354
+ console.log('๐Ÿ”„ [RefreshCoordinator] Order bump refresh function registered');
355
+ },
356
+ notifyCheckoutChanged: async () => {
357
+ if (orderBumpRefreshRef.current) {
358
+ console.log('๐Ÿ”„ [RefreshCoordinator] Checkout changed, refreshing order bump data...');
359
+ try {
360
+ await orderBumpRefreshRef.current();
361
+ console.log('โœ… [RefreshCoordinator] Order bump refresh completed');
362
+ }
363
+ catch (error) {
364
+ console.warn('โš ๏ธ [RefreshCoordinator] Order bump refresh failed:', error);
365
+ }
366
+ }
367
+ },
368
+ notifyOrderBumpChanged: async () => {
369
+ if (checkoutRefreshRef.current) {
370
+ console.log('๐Ÿ”„ [RefreshCoordinator] Order bump changed, refreshing checkout data...');
371
+ try {
372
+ await checkoutRefreshRef.current();
373
+ console.log('โœ… [RefreshCoordinator] Checkout refresh completed');
374
+ }
375
+ catch (error) {
376
+ console.warn('โš ๏ธ [RefreshCoordinator] Checkout refresh failed:', error);
377
+ }
378
+ }
379
+ },
380
+ unregisterCheckoutRefresh: () => {
381
+ checkoutRefreshRef.current = null;
382
+ console.log('๐Ÿงน [RefreshCoordinator] Checkout refresh function unregistered');
383
+ },
384
+ unregisterOrderBumpRefresh: () => {
385
+ orderBumpRefreshRef.current = null;
386
+ console.log('๐Ÿงน [RefreshCoordinator] Order bump refresh function unregistered');
387
+ },
388
+ };
344
389
  const contextValue = {
345
390
  auth,
346
391
  session,
@@ -363,6 +408,7 @@ storeId, accountId, }) {
363
408
  lastUpdated: new Date(),
364
409
  });
365
410
  },
411
+ refreshCoordinator,
366
412
  money: {
367
413
  formatMoney,
368
414
  getCurrencyInfo,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",