robotstxt-manager/admin/js/robotstxt-manager-settings.js
2026-08-18 10:47:06 +00:00

92 lines
2.6 KiB
JavaScript

/**
* Settings page JS — "Test connection" and "Delete API key" handlers.
*
* @global RobotstxtManagerSettings
* @global jQuery
* @package Robotstxt_Manager
*/
( function ( $ ) {
'use strict';
// Test connection.
$( document ).on(
'click',
'#robotstxt-manager-test-connection',
function () {
var $btn = $( this );
var $result = $( '#robotstxt-manager-test-result' );
$btn.prop( 'disabled', true ).text( RobotstxtManagerSettings.i18n.testing );
$result.text( '' ).css( 'color', '' );
var apiKey = $( '#robotstxt_manager_api_key' ).val();
$.post(
RobotstxtManagerSettings.ajaxUrl,
{
action: 'robotstxt_manager_test_connection',
nonce: RobotstxtManagerSettings.nonce,
robotstxt_manager_api_key: apiKey,
},
function ( response ) {
if ( response.success ) {
$result.text( response.data.message ).css( 'color', '#00a32a' );
} else {
$result.text( response.data.message ).css( 'color', '#d63638' );
}
}
).fail(
function () {
$result.text( RobotstxtManagerSettings.i18n.ajaxError ).css( 'color', '#d63638' );
}
).always(
function () {
$btn.prop( 'disabled', false ).text( RobotstxtManagerSettings.i18n.testLabel );
}
);
}
);
// Delete API key.
$( document ).on(
'click',
'#robotstxt-manager-delete-key',
function () {
var $btn = $( this );
var $result = $( '#robotstxt-manager-test-result' );
if ( ! window.confirm( RobotstxtManagerSettings.i18n.deleteConfirm ) ) { /* eslint-disable-line no-alert */
return;
}
$btn.prop( 'disabled', true ).text( RobotstxtManagerSettings.i18n.deleting );
$result.text( '' ).css( 'color', '' );
$.post(
RobotstxtManagerSettings.ajaxUrl,
{
action: 'robotstxt_manager_delete_key',
nonce: RobotstxtManagerSettings.deleteNonce,
},
function ( response ) {
if ( response.success ) {
$result.text( RobotstxtManagerSettings.i18n.deleted ).css( 'color', '#00a32a' );
// Hide the delete button + test button; update description.
$btn.hide();
$( '#robotstxt-manager-test-connection' ).prop( 'disabled', true );
$( '#robotstxt_manager_api_key' ).next( '.description' ).html( RobotstxtManagerSettings.i18n.deleted );
} else {
$result.text( response.data.message ).css( 'color', '#d63638' );
$btn.prop( 'disabled', false ).text( RobotstxtManagerSettings.i18n.deleteLabel );
}
}
).fail(
function () {
$result.text( RobotstxtManagerSettings.i18n.ajaxError ).css( 'color', '#d63638' );
$btn.prop( 'disabled', false ).text( RobotstxtManagerSettings.i18n.deleteLabel );
}
);
}
);
}( jQuery ) );