var custom_alert = function (alertType, title, message, onCloseCallback = null) { var modalElement = $('#customAlertModal'); var modalHeader = modalElement.find('.modal-header'); // Clear previous alert classes and add the new one modalHeader.removeClass('alert-success alert-danger alert-warning alert-primary alert-info').addClass(`alert-${alertType}`); // Set the title and message if (title) { $('#customAlertModalLabel').text(title); } $('#customAlertModal .modal-body').html(message); // Show the modal var modalElement = $('#customAlertModal'); var modal = new bootstrap.Modal(modalElement.get(0)); modal.show(); // One-time event listener for close button modalElement.find('.btn-for-close').on('click', function () { // Call the callback function if provided if (typeof onCloseCallback === 'function') { onCloseCallback(); } // Hide the modal modal.hide(); }); } var error_alert = function (error_code, title = '', message = '', onCloseCallback = null) { var alertType = 'danger'; if (!title && error_codes[error_code]) { title = error_codes[error_code].title; } if (!message && error_codes[error_code]) { message = error_codes[error_code].message; } // Special treat for user_not_login (error_code = 1051) if (error_code == 1051) { showLoginForm(message); return; } custom_alert(alertType, title, message, onCloseCallback); } var success_alert = function (error_code, title = '', message = '', onCloseCallback = null) { var alertType = 'success'; if (!title && error_codes[error_code]) { title = error_codes[error_code].title; } if (!message && error_codes[error_code]) { message = error_codes[error_code].message; } custom_alert(alertType, title, message, onCloseCallback); } function showLoginForm(message) { // Set the message in the modal, if provided if (message) { $('#loginMessage').text(message).show(); } setTimezoneInput(); // hide all other modal $('.modal').modal('hide'); // Show the modal $('#loginModal').modal('show'); } $('#loginModal').on('shown.bs.modal', function () { $('#usernameOrEmail').focus(); }); // When submitting the login form via AJAX $('#loginSubmitBtn').on('click', function () { var $loginBtn = $(this); // Cache the button selector var originalButtonText = $loginBtn.text(); // Save the original button text var usernameOrEmail = $('#usernameOrEmail').val(); var password = $('#password').val(); // Set loading text and disable the button $loginBtn.text('Logging in...').prop('disabled', true); $.ajax({ type: 'POST', url: '/login_api', // Your Flask login route data: JSON.stringify({ username_or_email: usernameOrEmail, password: password }), contentType: 'application/json;charset=UTF-8', headers: { 'X-CSRFToken': csrf_token, }, success: function (response) { if (response.result == 0) { // Redirect to the next page or reload the current page if (response.redirect) { window.location.href = response.redirect; } else if (response.action == 'refresh') { window.location.reload(); } } else { // Display error message $('#loginMessage').text(response.message); // Consider using a more user-friendly way to display the message } }, error: function (jqXHR, textStatus, errorThrown) { // Handle errors console.error('Error:', textStatus, errorThrown); var response = JSON.parse(jqXHR.responseText); $('#loginMessage').text(response.message); // Consider using a more user-friendly way to display the message // $loginBtn.text(originalButtonText).prop('disabled', false); // Reset the button text and enable the button }, complete: function () { // Restore the button's original state $loginBtn.text(originalButtonText).prop('disabled', false); } }); }); function setTimezoneInput() { const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; $('#userTimezone').val(timezone); }