@tagadapay/plugin-sdk 2.4.18 → 2.4.21

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.
@@ -1,11 +1,9 @@
1
1
  import { Customer } from '../types';
2
- export interface UseCustomerOptions {
3
- autoFetch?: boolean;
4
- }
5
- export declare function useCustomer({ autoFetch }: UseCustomerOptions): {
2
+ export declare function useCustomer(): {
6
3
  customer: Customer | null;
7
4
  isAuthenticated: boolean;
8
5
  isLoading: boolean;
9
6
  isAnonymous: boolean;
10
- refreshSession: (token?: string) => Promise<void>;
7
+ refreshCustomer: () => Promise<Customer | null>;
8
+ error: Error | null;
11
9
  };
@@ -1,15 +1,48 @@
1
1
  'use client';
2
+ import { useEffect, useState } from 'react';
2
3
  /**
3
4
  * useCustomer - Hook to access current customer data
4
5
  */
5
6
  import { useTagadaContext } from '../providers/TagadaProvider';
6
- export function useCustomer({ autoFetch = false }) {
7
- const { customer, isLoading, refreshSession } = useTagadaContext();
7
+ import { useSession } from './useSession';
8
+ export function useCustomer() {
9
+ const { customerId } = useSession();
10
+ const [customer, setCustomer] = useState(null);
11
+ const { apiService } = useTagadaContext();
12
+ const [isLoading, setIsLoading] = useState(false);
13
+ const [error, setError] = useState(null);
14
+ const fetchCustomer = async (customerId) => {
15
+ try {
16
+ setIsLoading(true);
17
+ const customer = await apiService.getCustomerProfile(customerId);
18
+ setCustomer(customer);
19
+ return customer;
20
+ }
21
+ catch (error) {
22
+ setError(error);
23
+ console.error('Error fetching customer:', error);
24
+ }
25
+ finally {
26
+ setIsLoading(false);
27
+ }
28
+ };
29
+ useEffect(() => {
30
+ if (customerId) {
31
+ fetchCustomer(customerId);
32
+ }
33
+ }, [customerId]);
34
+ const refreshCustomer = async () => {
35
+ if (!customerId) {
36
+ throw new Error('No customer ID available');
37
+ }
38
+ return await fetchCustomer(customerId);
39
+ };
8
40
  return {
9
41
  customer,
10
42
  isAuthenticated: customer?.isAuthenticated || false,
11
43
  isLoading,
12
44
  isAnonymous: customer?.role === 'anonymous',
13
- refreshSession,
45
+ refreshCustomer,
46
+ error,
14
47
  };
15
48
  }
@@ -29,7 +29,6 @@ interface TagadaContextValue {
29
29
  lastUpdated: Date | null;
30
30
  };
31
31
  updateCheckoutDebugData: (data: any, error?: Error | null, isLoading?: boolean) => void;
32
- refreshSession: (token?: string) => Promise<void>;
33
32
  refreshCoordinator: {
34
33
  registerCheckoutRefresh: (refreshFn: () => Promise<void>) => void;
35
34
  registerOrderBumpRefresh: (refreshFn: () => Promise<void>) => void;
@@ -271,7 +271,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
271
271
  }
272
272
  }
273
273
  // Update customer data if available
274
- console.log('RESPONSE CUSTOMER', response.customer);
275
274
  if (response.customer) {
276
275
  setCustomer(response.customer);
277
276
  if (finalDebugMode) {
@@ -423,50 +422,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
423
422
  session,
424
423
  });
425
424
  }, [customer, session]);
426
- // Session refresh functionality
427
- const refreshSession = useCallback(async (providedToken) => {
428
- if (!storeId) {
429
- console.warn('[SDK] Cannot refresh session: No store ID available');
430
- return;
431
- }
432
- console.log('[SDK] 🔄 Refreshing session...', providedToken ? 'with provided token' : 'with new anonymous token');
433
- try {
434
- // Clear existing token and session data
435
- clearClientToken();
436
- setToken(null);
437
- setSession(null);
438
- setCustomer(null);
439
- setStore(null);
440
- setIsSessionInitialized(false);
441
- setHasAttemptedAnonymousToken(false);
442
- // Reset initialization flag to allow re-initialization
443
- isInitializing.current = false;
444
- if (providedToken) {
445
- // Use the provided token
446
- console.log('[SDK] Using provided token for session refresh');
447
- // Validate the token first
448
- const decodedSession = decodeJWTClient(providedToken);
449
- if (!decodedSession) {
450
- throw new Error('Invalid token provided');
451
- }
452
- // Set the provided token
453
- setToken(providedToken);
454
- setClientToken(providedToken);
455
- apiService.updateToken(providedToken);
456
- // Initialize session with the provided token
457
- await initializeSession(decodedSession);
458
- }
459
- else {
460
- // Create new anonymous token
461
- await createAnonymousToken(storeId);
462
- }
463
- console.log('[SDK] ✅ Session refreshed successfully');
464
- }
465
- catch (error) {
466
- console.error('[SDK] ❌ Failed to refresh session:', error);
467
- throw error;
468
- }
469
- }, [storeId, createAnonymousToken, initializeSession, apiService]);
470
425
  // Refresh coordinator for bidirectional hook communication
471
426
  const checkoutRefreshRef = useRef(null);
472
427
  const orderBumpRefreshRef = useRef(null);
@@ -538,7 +493,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
538
493
  lastUpdated: new Date(),
539
494
  });
540
495
  },
541
- refreshSession,
542
496
  refreshCoordinator,
543
497
  money: {
544
498
  formatMoney,
@@ -565,7 +519,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
565
519
  pluginConfig,
566
520
  configLoading,
567
521
  debugCheckout,
568
- refreshSession,
569
522
  refreshCoordinator,
570
523
  ]);
571
524
  // Determine if we should show loading
@@ -574,7 +527,7 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
574
527
  const shouldShowLoading = configLoading || (!storeId && configLoading);
575
528
  const canRenderChildren = blockUntilSessionReady
576
529
  ? !configLoading && storeId && isInitialized // Old behavior: wait for all phases
577
- : !configLoading && storeId; // New behavior: render after phases 1 & 2
530
+ : !configLoading && storeId; // New behavior: render after phases 1 & 2
578
531
  return (_jsxs(TagadaContext.Provider, { value: contextValue, children: [shouldShowLoading && _jsx(InitializationLoader, {}), finalDebugMode && canRenderChildren && (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => setIsDebugDrawerOpen(true), style: {
579
532
  position: 'fixed',
580
533
  bottom: '16px',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.4.18",
3
+ "version": "2.4.21",
4
4
  "description": "Modern React SDK for building Tagada Pay plugins",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,6 +0,0 @@
1
- export interface UseSessionRefreshResult {
2
- refreshSession: (token?: string) => Promise<void>;
3
- isLoading: boolean;
4
- isSessionInitialized: boolean;
5
- }
6
- export declare function useSessionRefresh(): UseSessionRefreshResult;
@@ -1,16 +0,0 @@
1
- 'use client';
2
- /**
3
- * useSessionRefresh - Hook to refresh the current session
4
- *
5
- * This hook provides a convenient way to refresh the current session,
6
- * which will create a new anonymous token and reinitialize the session.
7
- */
8
- import { useTagadaContext } from '../providers/TagadaProvider';
9
- export function useSessionRefresh() {
10
- const { refreshSession, isLoading, isSessionInitialized } = useTagadaContext();
11
- return {
12
- refreshSession,
13
- isLoading,
14
- isSessionInitialized,
15
- };
16
- }