Skip to content

Commit 0bd86d6

Browse files
committed
Delegate spinner unblock to WooCommerce events
1 parent 60371e5 commit 0bd86d6

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

modules/ppcp-blocks/resources/js/Components/paypal.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
onApproveSavePayment,
2222
} from '../paypal-config';
2323
import { useRef } from 'react';
24+
import Spinner from '../../../../ppcp-button/resources/js/modules/Helper/Spinner';
2425

2526
const PAYPAL_GATEWAY_ID = 'ppcp-gateway';
2627

@@ -52,8 +53,11 @@ export const PayPalComponent = ( {
5253
useState( false );
5354

5455
const [ paypalScriptLoaded, setPaypalScriptLoaded ] = useState( false );
56+
const [ isFullPageSpinnerActive, setIsFullPageSpinnerActive ] =
57+
useState( false );
5558

5659
const paypalButtonRef = useRef( null );
60+
const spinnerRef = useRef( null );
5761

5862
if ( ! paypalScriptLoaded ) {
5963
if ( ! paypalScriptPromise ) {
@@ -70,6 +74,18 @@ export const PayPalComponent = ( {
7074
? `${ config.id }-${ fundingSource }`
7175
: config.id;
7276

77+
// Full-page spinner used to block UI interactions during flows like AppSwitch.
78+
useEffect( () => {
79+
if ( isFullPageSpinnerActive ) {
80+
if ( ! spinnerRef.current ) {
81+
spinnerRef.current = Spinner.fullPage();
82+
}
83+
spinnerRef.current.block();
84+
} else if ( spinnerRef.current ) {
85+
spinnerRef.current.unblock();
86+
}
87+
}, [ isFullPageSpinnerActive ] );
88+
7389
useEffect( () => {
7490
// fill the form if in continuation (for product or mini-cart buttons)
7591
if ( continuationFilled || ! config.scriptData.continuation?.order ) {
@@ -286,12 +302,21 @@ export const PayPalComponent = ( {
286302
};
287303
}, [ onPaymentSetup, paypalOrder, activePaymentMethod ] );
288304

305+
useEffect( () => {
306+
const unsubscribe = onCheckoutFail( () => {
307+
setIsFullPageSpinnerActive( false );
308+
} );
309+
310+
return unsubscribe;
311+
}, [ onCheckoutFail ] );
312+
289313
useEffect( () => {
290314
if ( activePaymentMethod !== methodId ) {
291315
return;
292316
}
293317
const unsubscribe = onCheckoutFail( ( { processingResponse } ) => {
294318
console.error( processingResponse );
319+
setIsFullPageSpinnerActive( false );
295320
if ( onClose ) {
296321
onClose();
297322
}
@@ -336,7 +361,8 @@ export const PayPalComponent = ( {
336361
setGotoContinuationOnError,
337362
onSubmit,
338363
onError,
339-
onClose
364+
onClose,
365+
setIsFullPageSpinnerActive
340366
);
341367
},
342368
}
@@ -491,7 +517,8 @@ export const PayPalComponent = ( {
491517
setGotoContinuationOnError,
492518
onSubmit,
493519
onError,
494-
onClose
520+
onClose,
521+
setIsFullPageSpinnerActive
495522
)
496523
}
497524
onShippingOptionsChange={ getOnShippingOptionsChange(

modules/ppcp-blocks/resources/js/paypal-config.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
paypalSubscriptionToWcAddresses,
44
} from './Helper/Address';
55
import ResumeFlowHelper from '../../../ppcp-button/resources/js/modules/Helper/ResumeFlowHelper';
6-
import Spinner from '../../../ppcp-button/resources/js/modules/Helper/Spinner';
76

87
export const createOrder = async ( data, config, onError, onClose ) => {
98
try {
@@ -65,11 +64,10 @@ export const handleApprove = async (
6564
setGotoContinuationOnError,
6665
onSubmit,
6766
onError,
68-
onClose
67+
onClose,
68+
setIsFullPageSpinnerActive
6969
) => {
70-
// Block the entire page during approval process
71-
const spinner = Spinner.fullPage();
72-
spinner.block();
70+
setIsFullPageSpinnerActive( true );
7371

7472
try {
7573
let order;
@@ -173,13 +171,13 @@ export const handleApprove = async (
173171
} catch ( err ) {
174172
console.error( err );
175173

174+
setIsFullPageSpinnerActive( false );
175+
176176
onError( err.message );
177177

178178
onClose();
179179

180180
throw err;
181-
} finally {
182-
spinner.unblock();
183181
}
184182
};
185183

modules/ppcp-button/resources/js/modules/Helper/Spinner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Spinner {
1414
background: '#fff',
1515
opacity: 0.6,
1616
},
17+
baseZ: 10000,
1718
} );
1819
}
1920

0 commit comments

Comments
 (0)