54 lines
No EOL
2.1 KiB
JavaScript
54 lines
No EOL
2.1 KiB
JavaScript
/**
|
|
* Handles the "Send Invite" metabox functionality on the Job edit screen.
|
|
*/
|
|
(function($) {
|
|
'use strict';
|
|
|
|
$(document).ready(function() {
|
|
const $metabox = $('#quiztech_send_invite_metabox');
|
|
if (!$metabox.length) {
|
|
return; // Metabox not found
|
|
}
|
|
|
|
const $button = $metabox.find('#quiztech_send_invite_button');
|
|
const $emailInput = $metabox.find('#quiztech_applicant_email');
|
|
const $statusSpan = $metabox.find('#quiztech_invite_status');
|
|
|
|
$button.on('click', function() {
|
|
const email = $emailInput.val().trim();
|
|
|
|
// Basic email validation (more robust validation can be added)
|
|
if (!email || !/\S+@\S+\.\S+/.test(email)) {
|
|
$statusSpan.text('Please enter a valid email address.').css('color', 'red').show();
|
|
$emailInput.focus();
|
|
return;
|
|
}
|
|
|
|
// Disable button and show status
|
|
$button.prop('disabled', true);
|
|
$statusSpan.text(quiztechJobMetabox.sending_text).css('color', '').show();
|
|
|
|
// AJAX request
|
|
$.post(quiztechJobMetabox.ajax_url, {
|
|
action: 'quiztech_send_job_invite',
|
|
nonce: quiztechJobMetabox.nonce,
|
|
job_id: quiztechJobMetabox.job_id, // Passed via wp_localize_script
|
|
email: email
|
|
}, function(response) {
|
|
if (response.success) {
|
|
$statusSpan.text(response.data.message).css('color', 'green');
|
|
$emailInput.val(''); // Clear email field on success
|
|
} else {
|
|
const errorMsg = response.data && response.data.message ? response.data.message : 'Unknown error.';
|
|
$statusSpan.text(quiztechJobMetabox.error_text + ' ' + errorMsg).css('color', 'red');
|
|
}
|
|
}).fail(function() {
|
|
$statusSpan.text(quiztechJobMetabox.ajax_fail_text).css('color', 'red');
|
|
}).always(function() {
|
|
// Re-enable button
|
|
$button.prop('disabled', false);
|
|
});
|
|
});
|
|
});
|
|
|
|
})(jQuery); |