"use strict"; // Class Definition var KTAuthResetPassword = function () { // Elements var form; var submitButton; var validator; var handleForm = function (e) { // Init form validation rules. For more info check the FormValidation plugin's official documentation:https://formvalidation.io/ validator = FormValidation.formValidation( form, { fields: { 'email': { validators: { regexp: { regexp: /^[^\s@]+@[^\s@]+\.[^\s@]+$/, message: 'The value is not a valid email address', }, notEmpty: { message: 'Email address is required' } } } }, plugins: { trigger: new FormValidation.plugins.Trigger(), bootstrap: new FormValidation.plugins.Bootstrap5({ rowSelector: '.fv-row', eleInvalidClass: '', // comment to enable invalid state icons eleValidClass: '' // comment to enable valid state icons }) } } ); } var handleSubmitDemo = function (e) { submitButton.addEventListener('click', function (e) { e.preventDefault(); // Validate form validator.validate().then(function (status) { if (status == 'Valid') { // Show loading indication submitButton.setAttribute('data-kt-indicator', 'on'); // Disable button to avoid multiple click submitButton.disabled = true; // Simulate ajax request setTimeout(function () { // Hide loading indication submitButton.removeAttribute('data-kt-indicator'); // Enable button submitButton.disabled = false; // Show message popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/ Swal.fire({ text: "We have send a password reset link to your email.", icon: "success", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }).then(function (result) { if (result.isConfirmed) { form.querySelector('[name="email"]').value = ""; //form.submit(); var redirectUrl = form.getAttribute('data-kt-redirect-url'); if (redirectUrl) { location.href = redirectUrl; } } }); }, 1500); } else { // Show error popup. For more info check the plugin's official documentation: https://sweetalert2.github.io/ Swal.fire({ text: "Sorry, looks like there are some errors detected, please try again.", icon: "error", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }); } }); }); } var handleSubmitAjax = function (e) { // Handle form submit form.addEventListener('submit', function (e) { // Prevent form default action e.preventDefault(); console.log('Form submit event triggered'); // Validate form validator.validate().then(function (status) { if (status == 'Valid') { // Show loading SweetAlert instead of button indicator Swal.fire({ title: 'Sending Reset Link...', text: 'Please wait while we send your password reset email.', icon: 'info', allowOutsideClick: false, allowEscapeKey: false, showConfirmButton: false, didOpen: () => { Swal.showLoading(); } }); // Make fetch request fetch(form.getAttribute('action'), { method: 'POST', body: new FormData(form), headers: { 'X-Requested-With': 'XMLHttpRequest' } }) .then(response => response.json()) .then(data => { if (data.success) { form.reset(); // Show success message Swal.fire({ title: 'Email Sent!', text: data.message || 'We have sent a password reset link to your email.', icon: 'success', buttonsStyling: false, confirmButtonText: 'Ok, got it!', customClass: { confirmButton: 'btn btn-primary' } }).then(function (result) { if (result.isConfirmed) { const redirectUrl = form.getAttribute('data-kt-redirect-url'); if (redirectUrl) { location.href = redirectUrl; } } }); } else { // Show error message Swal.fire({ title: 'Error', text: data.message || 'Sorry, the email is incorrect, please try again.', icon: 'error', buttonsStyling: false, confirmButtonText: 'Try Again', customClass: { confirmButton: 'btn btn-primary' } }); } }) .catch(error => { console.error('Error:', error); let errorMessage = 'Sorry, looks like there are some errors detected, please try again.'; // Show network error Swal.fire({ title: 'Network Error', text: 'Unable to connect to the server. Please check your internet connection and try again.', icon: 'error', buttonsStyling: false, confirmButtonText: 'Try Again', customClass: { confirmButton: 'btn btn-primary' } }); }); } else { // Show validation error popup Swal.fire({ text: "Sorry, looks like there are some errors detected, please try again.", icon: "error", buttonsStyling: false, confirmButtonText: "Ok, got it!", customClass: { confirmButton: "btn btn-primary" } }); } }); }); } var isValidUrl = function(url) { try { new URL(url); return true; } catch (e) { return false; } } // Public Functions return { // public functions init: function () { form = document.querySelector('#kt_password_reset_form'); submitButton = document.querySelector('#kt_password_reset_submit'); console.log('Reset password form initialized:', form ? 'Found' : 'Not found'); console.log('Submit button initialized:', submitButton ? 'Found' : 'Not found'); if (form && submitButton) { handleForm(); handleSubmitAjax(); // Always use ajax submit for real functionality } else { console.error('Reset password form or submit button not found!'); } } }; }(); // On document ready KTUtil.onDOMContentLoaded(function () { KTAuthResetPassword.init(); });