@tagadapay/plugin-sdk 2.4.17 → 2.4.20

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,17 +1,48 @@
1
1
  'use client';
2
- import { useState } from 'react';
2
+ import { useEffect, useState } from 'react';
3
3
  /**
4
4
  * useCustomer - Hook to access current customer data
5
5
  */
6
6
  import { useTagadaContext } from '../providers/TagadaProvider';
7
- export function useCustomer({ autoFetch = false }) {
8
- const [customerDatas, setCustomerDatas] = useState(null);
9
- const { customer, isLoading, session, 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
+ };
10
40
  return {
11
41
  customer,
12
42
  isAuthenticated: customer?.isAuthenticated || false,
13
43
  isLoading,
14
44
  isAnonymous: customer?.role === 'anonymous',
15
- refreshSession,
45
+ refreshCustomer,
46
+ error,
16
47
  };
17
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;
@@ -422,50 +422,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
422
422
  session,
423
423
  });
424
424
  }, [customer, session]);
425
- // Session refresh functionality
426
- const refreshSession = useCallback(async (providedToken) => {
427
- if (!storeId) {
428
- console.warn('[SDK] Cannot refresh session: No store ID available');
429
- return;
430
- }
431
- console.log('[SDK] 🔄 Refreshing session...', providedToken ? 'with provided token' : 'with new anonymous token');
432
- try {
433
- // Clear existing token and session data
434
- clearClientToken();
435
- setToken(null);
436
- setSession(null);
437
- setCustomer(null);
438
- setStore(null);
439
- setIsSessionInitialized(false);
440
- setHasAttemptedAnonymousToken(false);
441
- // Reset initialization flag to allow re-initialization
442
- isInitializing.current = false;
443
- if (providedToken) {
444
- // Use the provided token
445
- console.log('[SDK] Using provided token for session refresh');
446
- // Validate the token first
447
- const decodedSession = decodeJWTClient(providedToken);
448
- if (!decodedSession) {
449
- throw new Error('Invalid token provided');
450
- }
451
- // Set the provided token
452
- setToken(providedToken);
453
- setClientToken(providedToken);
454
- apiService.updateToken(providedToken);
455
- // Initialize session with the provided token
456
- await initializeSession(decodedSession);
457
- }
458
- else {
459
- // Create new anonymous token
460
- await createAnonymousToken(storeId);
461
- }
462
- console.log('[SDK] ✅ Session refreshed successfully');
463
- }
464
- catch (error) {
465
- console.error('[SDK] ❌ Failed to refresh session:', error);
466
- throw error;
467
- }
468
- }, [storeId, createAnonymousToken, initializeSession, apiService]);
469
425
  // Refresh coordinator for bidirectional hook communication
470
426
  const checkoutRefreshRef = useRef(null);
471
427
  const orderBumpRefreshRef = useRef(null);
@@ -537,7 +493,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
537
493
  lastUpdated: new Date(),
538
494
  });
539
495
  },
540
- refreshSession,
541
496
  refreshCoordinator,
542
497
  money: {
543
498
  formatMoney,
@@ -564,7 +519,6 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
564
519
  pluginConfig,
565
520
  configLoading,
566
521
  debugCheckout,
567
- refreshSession,
568
522
  refreshCoordinator,
569
523
  ]);
570
524
  // Determine if we should show loading
@@ -572,8 +526,8 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
572
526
  // Phase 3 (session initialization) is optional/non-blocking by default
573
527
  const shouldShowLoading = configLoading || (!storeId && configLoading);
574
528
  const canRenderChildren = blockUntilSessionReady
575
- ? (!configLoading && storeId && isInitialized) // Old behavior: wait for all phases
576
- : (!configLoading && storeId); // New behavior: render after phases 1 & 2
529
+ ? !configLoading && storeId && isInitialized // Old behavior: wait for all phases
530
+ : !configLoading && storeId; // New behavior: render after phases 1 & 2
577
531
  return (_jsxs(TagadaContext.Provider, { value: contextValue, children: [shouldShowLoading && _jsx(InitializationLoader, {}), finalDebugMode && canRenderChildren && (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => setIsDebugDrawerOpen(true), style: {
578
532
  position: 'fixed',
579
533
  bottom: '16px',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tagadapay/plugin-sdk",
3
- "version": "2.4.17",
3
+ "version": "2.4.20",
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
- }