@tagadapay/plugin-sdk 2.4.16 → 2.4.17
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/react/hooks/useCustomer.d.ts +5 -1
- package/dist/react/hooks/useCustomer.js +5 -2
- package/dist/react/hooks/useSessionRefresh.d.ts +6 -0
- package/dist/react/hooks/useSessionRefresh.js +16 -0
- package/dist/react/providers/TagadaProvider.d.ts +4 -3
- package/dist/react/providers/TagadaProvider.js +54 -8
- package/package.json +1 -1
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { Customer } from '../types';
|
|
2
|
-
export
|
|
2
|
+
export interface UseCustomerOptions {
|
|
3
|
+
autoFetch?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export declare function useCustomer({ autoFetch }: UseCustomerOptions): {
|
|
3
6
|
customer: Customer | null;
|
|
4
7
|
isAuthenticated: boolean;
|
|
5
8
|
isLoading: boolean;
|
|
6
9
|
isAnonymous: boolean;
|
|
10
|
+
refreshSession: (token?: string) => Promise<void>;
|
|
7
11
|
};
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
'use client';
|
|
2
|
+
import { 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() {
|
|
7
|
-
const
|
|
7
|
+
export function useCustomer({ autoFetch = false }) {
|
|
8
|
+
const [customerDatas, setCustomerDatas] = useState(null);
|
|
9
|
+
const { customer, isLoading, session, refreshSession } = useTagadaContext();
|
|
8
10
|
return {
|
|
9
11
|
customer,
|
|
10
12
|
isAuthenticated: customer?.isAuthenticated || false,
|
|
11
13
|
isLoading,
|
|
12
14
|
isAnonymous: customer?.role === 'anonymous',
|
|
15
|
+
refreshSession,
|
|
13
16
|
};
|
|
14
17
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* TagadaProvider - Main provider component for the Tagada Pay React SDK
|
|
3
3
|
*/
|
|
4
4
|
import { ReactNode } from 'react';
|
|
5
|
-
import { Customer, Session, AuthState, Locale, Currency, Store, Environment, EnvironmentConfig } from '../types';
|
|
6
|
-
import { ApiService } from '../services/apiService';
|
|
7
5
|
import { PluginConfig } from '../hooks/usePluginConfig';
|
|
8
|
-
import {
|
|
6
|
+
import { ApiService } from '../services/apiService';
|
|
7
|
+
import { AuthState, Currency, Customer, Environment, EnvironmentConfig, Locale, Session, Store } from '../types';
|
|
8
|
+
import { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits } from '../utils/money';
|
|
9
9
|
interface TagadaContextValue {
|
|
10
10
|
auth: AuthState;
|
|
11
11
|
session: Session | null;
|
|
@@ -29,6 +29,7 @@ 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>;
|
|
32
33
|
refreshCoordinator: {
|
|
33
34
|
registerCheckoutRefresh: (refreshFn: () => Promise<void>) => void;
|
|
34
35
|
registerOrderBumpRefresh: (refreshFn: () => Promise<void>) => void;
|
|
@@ -3,15 +3,15 @@ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-run
|
|
|
3
3
|
/**
|
|
4
4
|
* TagadaProvider - Main provider component for the Tagada Pay React SDK
|
|
5
5
|
*/
|
|
6
|
-
import { createContext,
|
|
7
|
-
import { getEnvironmentConfig, detectEnvironment } from '../config/environment';
|
|
8
|
-
import { ApiService } from '../services/apiService';
|
|
9
|
-
import { setClientToken, getClientToken, clearClientToken } from '../utils/tokenStorage';
|
|
10
|
-
import { decodeJWTClient, isTokenExpired } from '../utils/jwtDecoder';
|
|
11
|
-
import { collectDeviceInfo, getBrowserLocale, getUrlParams } from '../utils/deviceInfo';
|
|
6
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, } from 'react';
|
|
12
7
|
import DebugDrawer from '../components/DebugDrawer';
|
|
8
|
+
import { detectEnvironment, getEnvironmentConfig } from '../config/environment';
|
|
13
9
|
import { loadPluginConfig } from '../hooks/usePluginConfig';
|
|
14
|
-
import {
|
|
10
|
+
import { ApiService } from '../services/apiService';
|
|
11
|
+
import { collectDeviceInfo, getBrowserLocale, getUrlParams } from '../utils/deviceInfo';
|
|
12
|
+
import { decodeJWTClient, isTokenExpired } from '../utils/jwtDecoder';
|
|
13
|
+
import { convertCurrency, formatMoney, formatMoneyWithoutSymbol, formatSimpleMoney, getCurrencyInfo, minorUnitsToMajorUnits, moneyStringOrNumberToMinorUnits, } from '../utils/money';
|
|
14
|
+
import { clearClientToken, getClientToken, setClientToken } from '../utils/tokenStorage';
|
|
15
15
|
// Professional, subtle loading component for initialization
|
|
16
16
|
const InitializationLoader = () => (_jsxs("div", { style: {
|
|
17
17
|
position: 'fixed',
|
|
@@ -422,6 +422,50 @@ 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]);
|
|
425
469
|
// Refresh coordinator for bidirectional hook communication
|
|
426
470
|
const checkoutRefreshRef = useRef(null);
|
|
427
471
|
const orderBumpRefreshRef = useRef(null);
|
|
@@ -493,6 +537,7 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
|
|
|
493
537
|
lastUpdated: new Date(),
|
|
494
538
|
});
|
|
495
539
|
},
|
|
540
|
+
refreshSession,
|
|
496
541
|
refreshCoordinator,
|
|
497
542
|
money: {
|
|
498
543
|
formatMoney,
|
|
@@ -519,6 +564,7 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
|
|
|
519
564
|
pluginConfig,
|
|
520
565
|
configLoading,
|
|
521
566
|
debugCheckout,
|
|
567
|
+
refreshSession,
|
|
522
568
|
refreshCoordinator,
|
|
523
569
|
]);
|
|
524
570
|
// Determine if we should show loading
|
|
@@ -527,7 +573,7 @@ localConfig, blockUntilSessionReady = false, // Default to new non-blocking beha
|
|
|
527
573
|
const shouldShowLoading = configLoading || (!storeId && configLoading);
|
|
528
574
|
const canRenderChildren = blockUntilSessionReady
|
|
529
575
|
? (!configLoading && storeId && isInitialized) // Old behavior: wait for all phases
|
|
530
|
-
: (!configLoading && storeId); //
|
|
576
|
+
: (!configLoading && storeId); // New behavior: render after phases 1 & 2
|
|
531
577
|
return (_jsxs(TagadaContext.Provider, { value: contextValue, children: [shouldShowLoading && _jsx(InitializationLoader, {}), finalDebugMode && canRenderChildren && (_jsxs(_Fragment, { children: [_jsx("button", { onClick: () => setIsDebugDrawerOpen(true), style: {
|
|
532
578
|
position: 'fixed',
|
|
533
579
|
bottom: '16px',
|