import { request } from "./helpers"; export function renderForgotPasswordForm() { const modal = document.createElement('section'); modal.classList.add('modal'); modal.innerHTML = `
`; document.body.appendChild(modal); setupStartResetForm(); setupInfoModal(modal); } function setupInfoModal(modal) { const closeElements = modal.querySelectorAll('.modal-background, .close-button'); Array.from(closeElements).forEach(close => { close.addEventListener('click', () => { modal.parentElement.removeChild(modal); }); }); } function setupStartResetForm() { document.getElementById('forgotPasswordSubmit').addEventListener('click', startPasswordReset); } function startPasswordReset() { const email = document.getElementById('forgotPasswordEmailField').value.trim(); const errorMessageElement = document.getElementById('forgotPasswordErrorMessages'); let errorMessage = ''; if (email === '') { errorMessage += 'Please enter an email address.
'; } errorMessageElement.innerHTML = errorMessage; if (errorMessage === '') { request({ action: 'initiate-password-reset', email, }, success => { console.log(success); }, error => { errorMessage += '' + error + '
'; }).then(() => { errorMessageElement.innerHTML = errorMessage; if (errorMessage === '') { document.getElementById('forgotPasswordForm').innerHTML = `Go check your email for the password reset link.
Note that it may be sent to your spam/junk folder by mistake.
`; } }); } } function setupPasswordResetForm() { const submitButton = document.getElementById('newPasswordSubmit'); if (submitButton) { submitButton.addEventListener('click', submitPasswordReset); } } function submitPasswordReset() { const password = document.getElementById('newPassword').value; const confirm = document.getElementById('newConfirm').value; const account = document.getElementById('account').value; const errorMessageElement = document.getElementById('newPasswordErrorMessages'); let errorMessage = ''; if (password === '') { errorMessage += 'Please enter a password.
'; } else if (password !== confirm) { errorMessage += 'The passwords do not match.
'; } errorMessageElement.innerHTML = errorMessage; if (errorMessage === '') { request({ action: 'password-reset', account, password, }, success => { console.log(success); }, error => { errorMessage += '' + error + '
'; }).then(() => { errorMessageElement.innerHTML = errorMessage; if (errorMessage === '') { document.getElementById('detailsPanel').innerHTML = `You can now Return to Lexiconga and log in using your new password.
`; } }); } } window.onload = (function (oldLoad) { oldLoad && oldLoad(); setupPasswordResetForm(); })(window.onload);