woocommerce-paypal-payments/modules/ppcp-settings/resources/js/switchSettingsUi.js
Narek Zakarian ef86785d43
Some checks failed
CI / PHP 7.4 (push) Has been cancelled
CI / PHP 8.0 (push) Has been cancelled
CI / PHP 8.1 (push) Has been cancelled
CI / PHP 8.2 (push) Has been cancelled
CI / PHP 8.3 (push) Has been cancelled
CI / PHP 8.4 (push) Has been cancelled
PR Playground Demo / prepare_version (push) Has been cancelled
PR Playground Demo / build_plugin (push) Has been cancelled
PR Playground Demo / create_archive (push) Has been cancelled
PR Playground Demo / Comment on PR with Playground details (push) Has been cancelled
Remove the unnecessary debug logs
2025-08-15 20:34:30 +04:00

56 lines
1.2 KiB
JavaScript

document.addEventListener( 'DOMContentLoaded', () => {
const config = ppcpSwitchSettingsUi;
const button = document.querySelector(
'.button.button-settings-switch-ui'
);
const link = document.querySelector(
'.ppcp-notice-wrapper:not(.inline) a.settings-switch-ui'
);
if ( typeof config === 'undefined' || ( ! button && ! link ) ) {
return;
}
const handleClick = ( event ) => {
event.preventDefault();
const confirmed = confirm( config.confirmMessage );
if ( ! confirmed ) {
return;
}
fetch( config.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify( {
nonce: config.nonce,
} ),
} )
.then( ( response ) => {
if ( ! response.ok ) {
throw new Error( 'Network response was not ok' );
}
return response.json();
} )
.then( ( data ) => {
window.location.reload();
} )
.catch( ( error ) => {
console.error( 'Error:', error );
} );
};
if ( button ) {
button.addEventListener( 'click', handleClick );
}
document.addEventListener( 'click', ( event ) => {
const linkElement = event.target.closest( 'a.settings-switch-ui' );
if ( linkElement ) {
handleClick( event );
}
} );
} );