Skip to content

Commit

Permalink
fix(ras-acc): performance enhancements and small fixes (#1871)
Browse files Browse the repository at this point in the history
This PR makes some small fixes and performance updates.
  • Loading branch information
chickenn00dle authored Sep 8, 2024
1 parent 75290f0 commit f7c54d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
10 changes: 8 additions & 2 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ public static function enqueue_scripts() {
plugins_url( 'dist/modalCheckout.js', \NEWSPACK_BLOCKS__PLUGIN_FILE ),
$dependencies,
\NEWSPACK_BLOCKS__VERSION,
true
[
'strategy' => 'async',
'in_footer' => true,
]
);
wp_localize_script(
'newspack-blocks-modal-checkout',
Expand Down Expand Up @@ -609,7 +612,10 @@ public static function enqueue_modal( $product_id = null ) {
plugins_url( 'dist/modal.js', \NEWSPACK_BLOCKS__PLUGIN_FILE ),
[],
\NEWSPACK_BLOCKS__VERSION,
true
[
'strategy' => 'async',
'in_footer' => true,
]
);
wp_localize_script(
'newspack-blocks-modal',
Expand Down
35 changes: 14 additions & 21 deletions src/modal-checkout/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ domReady( () => {
form.classList.remove( 'modal-processing' );

// Set reader activation checkout data if available.
let data = {};
const data = { is_pending_checkout: true };
if ( element.classList.contains( 'wpbnbd--platform-wc' ) ) {
const frequency = formData.get( 'donation_frequency' );
let amount;
Expand All @@ -315,24 +315,17 @@ domReady( () => {
layout = 'frequency';
}

data = {
type: 'donate',
layout,
frequency,
amount,
other: formData.get( `donation_value_${ frequency }_other` ),
};
data.type = 'donate';
data.layout = layout;
data.frequency = frequency;
data.amount = amount;
data.other = formData.get( `donation_value_${ frequency }_other` );
} else {
data = {
type: 'checkout_button',
product_id: formData.get( 'product_id' ),
variation_id: formData.get( 'variation_id' ),
};
data.type = 'checkout_button';
data.product_id = formData.get( 'product_id' );
data.variation_id = formData.get( 'variation_id' );
}
window?.newspackReaderActivation?.setCheckoutData?.( {
is_pending_checkout: true,
...data,
} );
window?.newspackReaderActivation?.setCheckoutData?.( data );

if (
typeof newspack_ras_config !== 'undefined' &&
Expand Down Expand Up @@ -414,6 +407,10 @@ domReady( () => {
window.newspackReaderActivation.openAuthModal( {
title: newspackBlocksModal.labels.auth_modal_title,
callback: () => {
// Signal checkout registration.
form.appendChild(
createHiddenInput( newspackBlocksModal.checkout_registration_flag, '1' )
);
triggerCheckout( form );
},
skipSuccess: true,
Expand Down Expand Up @@ -492,10 +489,6 @@ domReady( () => {
* @param {HTMLFormElement} form The form element.
*/
const triggerCheckout = form => {
// Signal checkout registration.
form.appendChild(
createHiddenInput( newspackBlocksModal.checkout_registration_flag, '1' )
);
// form.submit does not trigger submit event listener, so we use requestSubmit.
form.requestSubmit( form.querySelector( 'button[type="submit"]' ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/modal-checkout/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @return {void}
*/
export function domReady( callback ) {
if ( typeof document === 'undefined' ) {
if ( typeof document === 'undefined' || typeof callback !== 'function' ) {
return;
}
if (
Expand Down

0 comments on commit f7c54d0

Please sign in to comment.