| @@ -1,61 +1,95 @@ | ||
| 1 | -document.addEventListener('DOMContentLoaded', function() { | |
| 2 | - var form = document.getElementById('mxchat-activation-form'); | |
| 3 | - var spinner = document.getElementById('mxchat-activation-spinner'); | |
| 4 | - var submitButton = document.getElementById('activate_license_button'); | |
| 5 | - var licenseStatus = document.getElementById('mxchat-license-status'); | |
| 6 | - | |
| 7 | - form.addEventListener('submit', function(e) { | |
| 8 | - e.preventDefault(); | |
| 9 | - | |
| 10 | - // Show spinner and disable the button to prevent multiple submissions | |
| 11 | - spinner.style.display = 'inline-block'; | |
| 12 | - submitButton.disabled = true; | |
| 13 | - | |
| 14 | - // Gather form data | |
| 15 | - var formData = new FormData(form); | |
| 16 | - formData.append('action', 'mxchat_activate_license'); | |
| 17 | - formData.append('security', mxchatData.nonce); | |
| 18 | - | |
| 19 | - // Send the AJAX request | |
| 20 | - var xhr = new XMLHttpRequest(); | |
| 21 | - xhr.open('POST', mxchatData.ajax_url, true); | |
| 22 | - xhr.onload = function() { | |
| 23 | - // Hide the spinner | |
| 24 | - spinner.style.display = 'none'; | |
| 25 | - | |
| 26 | - if (xhr.status >= 200 && xhr.status < 400) { | |
| 27 | - var response = JSON.parse(xhr.responseText); | |
| 28 | - if (response.success) { | |
| 29 | - // Update the license status to active | |
| 30 | - licenseStatus.textContent = 'Active'; | |
| 31 | - licenseStatus.classList.remove('inactive'); | |
| 32 | - licenseStatus.classList.add('active'); | |
| 33 | - | |
| 34 | - // Hide the activation form | |
| 35 | - form.style.display = 'none'; | |
| 36 | - } else { | |
| 37 | - // Re-enable the button | |
| 38 | - submitButton.disabled = false; | |
| 39 | - | |
| 40 | - // Handle the error case | |
| 41 | - alert(response.data || 'There was an error activating the license.'); | |
| 42 | - } | |
| 1 | +/** | |
| 2 | + * MxChat License Activation JS | |
| 3 | + * Handles the license activation form submission with timeout handling | |
| 4 | + */ | |
| 5 | +jQuery(document).ready(function($) { | |
| 6 | + // Activation handling | |
| 7 | + const form = $('#mxchat-activation-form'); | |
| 8 | + const spinner = $('#mxchat-activation-spinner'); | |
| 9 | + const submitButton = $('#activate_license_button'); | |
| 10 | + const licenseStatus = $('#mxchat-license-status'); | |
| 11 | + | |
| 12 | + if (form.length && licenseStatus.length && submitButton.length) { | |
| 13 | + function handleActivationResponse(response) { | |
| 14 | + spinner.hide(); | |
| 15 | + if (response.success) { | |
| 16 | + licenseStatus.text('Active'); | |
| 17 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 18 | + form.hide(); | |
| 43 | 19 | } else { |
| 44 | - // Re-enable the button | |
| 45 | - submitButton.disabled = false; | |
| 46 | - | |
| 47 | - // Handle server error | |
| 48 | - alert('Server error. Please try again.'); | |
| 20 | + licenseStatus.text('Inactive'); | |
| 21 | + alert(response.data || 'Activation failed. Please check your input.'); | |
| 22 | + submitButton.prop('disabled', false); | |
| 49 | 23 | } |
| 50 | - }; | |
| 24 | + } | |
| 51 | 25 | |
| 52 | - xhr.onerror = function() { | |
| 53 | - // Hide the spinner and re-enable the button on error | |
| 54 | - spinner.style.display = 'none'; | |
| 55 | - submitButton.disabled = false; | |
| 56 | - alert('Request failed. Please check your internet connection.'); | |
| 57 | - }; | |
| 58 | - | |
| 59 | - xhr.send(formData); // Send the form data | |
| 60 | - }); | |
| 61 | -}); | |
| 26 | + function checkActivationStatus() { | |
| 27 | + const email = $('#mxchat_pro_email').val(); | |
| 28 | + const key = $('#mxchat_activation_key').val(); | |
| 29 | + | |
| 30 | + $.ajax({ | |
| 31 | + type: 'POST', | |
| 32 | + url: mxchatAdmin.ajax_url, | |
| 33 | + data: { | |
| 34 | + action: 'mxchat_check_license_status', | |
| 35 | + email: email, | |
| 36 | + key: key, | |
| 37 | + security: mxchatAdmin.license_nonce | |
| 38 | + }, | |
| 39 | + success: function(response) { | |
| 40 | + if (response.is_active) { | |
| 41 | + // License is actually active, update UI | |
| 42 | + licenseStatus.text('Active'); | |
| 43 | + licenseStatus.removeClass('inactive').addClass('active'); | |
| 44 | + form.hide(); | |
| 45 | + alert('Your license has been activated successfully!'); | |
| 46 | + } else { | |
| 47 | + // Truly not activated | |
| 48 | + alert('License activation timed out. Please try again or contact support if the problem persists.'); | |
| 49 | + submitButton.prop('disabled', false); | |
| 50 | + } | |
| 51 | + }, | |
| 52 | + error: function() { | |
| 53 | + alert('Unable to verify license status. Please refresh the page to check if activation was successful.'); | |
| 54 | + submitButton.prop('disabled', false); | |
| 55 | + } | |
| 56 | + }); | |
| 57 | + } | |
| 58 | + | |
| 59 | + form.on('submit', function(event) { | |
| 60 | + event.preventDefault(); | |
| 61 | + spinner.show(); | |
| 62 | + submitButton.prop('disabled', true); | |
| 63 | + | |
| 64 | + var formData = { | |
| 65 | + action: 'mxchat_activate_license', | |
| 66 | + mxchat_pro_email: $('#mxchat_pro_email').val(), | |
| 67 | + mxchat_activation_key: $('#mxchat_activation_key').val(), | |
| 68 | + security: mxchatAdmin.license_nonce | |
| 69 | + }; | |
| 70 | + | |
| 71 | + // Use $.ajax instead of $.post to have more control | |
| 72 | + $.ajax({ | |
| 73 | + type: 'POST', | |
| 74 | + url: mxchatAdmin.ajax_url, | |
| 75 | + data: formData, | |
| 76 | + timeout: 30000, // 30 seconds timeout | |
| 77 | + success: function(response) { | |
| 78 | + handleActivationResponse(response); | |
| 79 | + }, | |
| 80 | + error: function(jqXHR, textStatus) { | |
| 81 | + if (textStatus === 'timeout') { | |
| 82 | + // If the request times out, check if the license was actually activated | |
| 83 | + spinner.hide(); | |
| 84 | + checkActivationStatus(); | |
| 85 | + } else { | |
| 86 | + // Handle other errors | |
| 87 | + alert('Server error. Please try again.'); | |
| 88 | + spinner.hide(); | |
| 89 | + submitButton.prop('disabled', false); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + }); | |
| 93 | + }); | |
| 94 | + } | |
| 95 | +}); | |