rl-warmup-plugin/js/domain-form.js

150 lines
No EOL
5.6 KiB
JavaScript

/**
* Domain form AJAX handling
*/
jQuery(document).ready(function($) {
// console.log("Domain form loaded");
// Handle domain form submission
$('#domain-form').on('submit', function(e) {
e.preventDefault();
// Show loading state
const $submitButton = $(this).find('input[type="submit"]');
const originalText = $submitButton.val();
$submitButton.val('Processing...').prop('disabled', true);
// Get form data
const formData = new FormData(this);
formData.append('action', 'rl_mailwarmer_save_domain');
formData.append('security', domain_form_vars.nonce);
// Process checkbox fields
formData.set('domain_in_use', $('#domain_in_use').is(':checked') ? '1' : '');
formData.set('update_dns', $('#update_dns').is(':checked') ? '1' : '');
// AJAX request
$.ajax({
url: domain_form_vars.ajax_url,
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function(response) {
if (response.success) {
console.log("Domain added");
// Display success message
$('#form-messages')
.removeClass('notice-error')
.addClass('notice notice-success is-dismissible')
.html('<p>' + response.data.message + '</p>')
.show();
// Add a dismiss button to the notice
addNoticeDismissButton();
// Reset form or redirect if not staying on page
if (!$('#stay_on_page').is(':checked') && response.data.permalink) {
// window.location.href = response.data.permalink;
return;
} else if (!$('#stay_on_page').is(':checked')) {
window.location.href = domain_form_vars.domains_page;
return;
}
// Reset form for new submission if staying on page
if (!$('#post_id').val()) {
$('#domain-form')[0].reset();
}
} else {
// Display error message
$('#form-messages')
.removeClass('notice-success')
.addClass('notice notice-error is-dismissible')
.html('<p>' + response.data.message + '</p>')
.show();
// Add a dismiss button to the notice
addNoticeDismissButton();
}
},
error: function(xhr, status, error) {
// Display error message
$('#form-messages')
.removeClass('notice-success')
.addClass('notice notice-error is-dismissible')
.html('<p>An error occurred: ' + error + '</p>')
.show();
// Add a dismiss button to the notice
addNoticeDismissButton();
},
complete: function() {
// Restore button state
$submitButton.val(originalText).prop('disabled', false);
}
});
});
// Handle delete domain button click
$('.delete-domain-btn').on('click', function(e) {
e.preventDefault();
if (!confirm('Are you sure you want to delete this domain? This action cannot be undone.')) {
return;
}
const postId = $(this).data('id');
// Show loading state
$(this).text('Deleting...').prop('disabled', true);
// AJAX request to delete domain
$.ajax({
url: domain_form_vars.ajax_url,
type: 'POST',
data: {
action: 'rl_mailwarmer_delete_domain',
security: domain_form_vars.delete_nonce,
post_id: postId
},
success: function(response) {
if (response.success) {
// Redirect to domains list page
window.location.href = domain_form_vars.domains_page;
} else {
alert(response.data.message || 'Error deleting domain');
}
},
error: function() {
alert('An error occurred while deleting the domain');
}
});
});
// Toggle advanced settings
$('.advanced-toggle').on('click', function() {
$(this).parent().find('.advanced-content').slideToggle();
});
// Helper function to add dismiss button to notices
function addNoticeDismissButton() {
$('.notice.is-dismissible').each(function() {
const $notice = $(this);
const $button = $('<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>');
// Only add the button if it doesn't exist already
if ($notice.find('.notice-dismiss').length === 0) {
$notice.append($button);
// Handle the dismiss button click
$button.on('click', function() {
$notice.slideUp('fast', function() {
$notice.remove();
});
});
}
});
}
// Initialize dismiss buttons
addNoticeDismissButton();
});