@tagadapay/plugin-sdk 2.7.31 → 2.7.33
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,20 +3,18 @@
|
|
|
3
3
|
* Replaces coordinator pattern with automatic cache invalidation
|
|
4
4
|
*/
|
|
5
5
|
import { useCallback, useEffect, useState } from 'react';
|
|
6
|
+
import { useTagadaContext } from '../providers/TagadaProvider';
|
|
6
7
|
import { getGlobalApiClient, useApiMutation, useInvalidateQuery } from './useApiQuery';
|
|
7
8
|
import { useCheckoutQuery } from './useCheckoutQuery';
|
|
8
9
|
export function useOrderBumpQuery(options) {
|
|
9
10
|
const { checkoutToken, offerId } = options;
|
|
10
11
|
const { invalidateCheckout, invalidatePromotions } = useInvalidateQuery();
|
|
12
|
+
const { isSessionInitialized } = useTagadaContext();
|
|
11
13
|
const client = getGlobalApiClient();
|
|
12
|
-
|
|
13
|
-
const { checkout } = useCheckoutQuery({
|
|
14
|
+
const { checkout, isLoading: isCheckoutLoading, isSuccess: isCheckoutSuccess } = useCheckoutQuery({
|
|
14
15
|
checkoutToken: checkoutToken || undefined,
|
|
15
16
|
});
|
|
16
|
-
// Use provided checkout or loaded checkout
|
|
17
|
-
// Get the actual checkout token from the checkout session
|
|
18
17
|
const actualCheckoutToken = checkoutToken || checkout?.checkoutSession?.checkoutToken || null;
|
|
19
|
-
// Function to check if order bump is selected
|
|
20
18
|
const checkOrderBumpSelection = useCallback(() => {
|
|
21
19
|
if (!checkout?.checkoutSession?.sessionLineItems) {
|
|
22
20
|
return false;
|
|
@@ -69,6 +67,19 @@ export function useOrderBumpQuery(options) {
|
|
|
69
67
|
},
|
|
70
68
|
});
|
|
71
69
|
const toggle = useCallback(async (selected) => {
|
|
70
|
+
// Check if session is initialized (required for checkout query to run)
|
|
71
|
+
if (!isSessionInitialized) {
|
|
72
|
+
return { success: false, error: 'Session is not initialized yet. Please wait...' };
|
|
73
|
+
}
|
|
74
|
+
// Wait for checkout to finish loading if it's still loading
|
|
75
|
+
if (isCheckoutLoading) {
|
|
76
|
+
return { success: false, error: 'Checkout session is still loading. Please wait...' };
|
|
77
|
+
}
|
|
78
|
+
// Check if checkout failed to load (only if we have a token)
|
|
79
|
+
if (checkoutToken && !isCheckoutSuccess && !checkout) {
|
|
80
|
+
return { success: false, error: 'Failed to load checkout session. Please refresh and try again.' };
|
|
81
|
+
}
|
|
82
|
+
// Check if checkout session is available
|
|
72
83
|
if (!checkout?.checkoutSession?.id) {
|
|
73
84
|
return { success: false, error: 'Checkout session not ready' };
|
|
74
85
|
}
|
|
@@ -81,7 +92,7 @@ export function useOrderBumpQuery(options) {
|
|
|
81
92
|
const error = err instanceof Error ? err : new Error('Failed to toggle order bump');
|
|
82
93
|
return { success: false, error: error.message };
|
|
83
94
|
}
|
|
84
|
-
}, [checkout?.checkoutSession?.id, isSelected, toggleMutation, offerId, checkoutToken, actualCheckoutToken]);
|
|
95
|
+
}, [checkout?.checkoutSession?.id, isSelected, toggleMutation, offerId, checkoutToken, actualCheckoutToken, isCheckoutLoading, isCheckoutSuccess, checkout, isSessionInitialized]);
|
|
85
96
|
return {
|
|
86
97
|
isSelected,
|
|
87
98
|
isToggling: toggleMutation.isPending,
|