diff --git a/app/static/css/main.css b/app/static/css/main.css index 95af38a..d3cb7af 100644 --- a/app/static/css/main.css +++ b/app/static/css/main.css @@ -343,7 +343,7 @@ input:-webkit-autofill:focus { .flashed-message { color: var(--dark-grey-text); - background: var(--light-grey-bg); + background: white; border: none; z-index: 99; margin: 0; @@ -355,3 +355,7 @@ input:-webkit-autofill:focus { text-align: left; box-shadow: var(--my-box-shadow); } + +.sidebar-layout .flashed-message { + left: var(--sidebar-width); +} \ No newline at end of file diff --git a/app/static/js/forms.js b/app/static/js/forms.js index ed10f03..f4153fd 100644 --- a/app/static/js/forms.js +++ b/app/static/js/forms.js @@ -1,47 +1,55 @@ -// Toggles loading button -function showLoadingBtn(isLoading) { - if (isLoading == true) { +function handleSubmitForm(formID, endpoint) { + + $('#' + formID).on('submit', function(event) { + + event.preventDefault(); + + // Show loading button $(":input[type='submit']").prop('disabled', true); $('.btn-text').hide(); $('.spinner-border').show(); - } else { - $(":input[type='submit']").prop('disabled', false); - $('.btn-text').show(); - $('.spinner-border').hide(); - } -} -// Function to display error messages -function displayFormErrors(errors) { - for (const key in errors) { - const inputField = $('#' + key); - const errorMessage = $( - '
' - ); - inputField.after(errorMessage); - inputField.addClass('input-error'); - } + var formData = $(this).serialize(); + formData += "&form-id=" + $(this).attr('id'); + + $.post(endpoint, formData, function(data) { + + // Hide loading button + $(":input[type='submit']").prop('disabled', false); + $('.btn-text').show(); + $('.spinner-border').hide(); + + // Clear previous errors + $('.error-message').remove(); + $('.input-error').removeClass('input-error'); + + if (response.success) { + + // Redirect if url returned + if (response.url) { + window.location = response.url; + } + } else { + + // Display error messages for each input field + if (response.errors) { + for (const key in response.errors) { + const inputField = $('#' + key); + const errorMessage = $( + ' ' + ); + inputField.after(errorMessage); + inputField.addClass('input-error'); + } + } + } + }); + }); } -function ajaxFormResponseHandler(response) { - - // Clear previous errors - $('.error-message').remove(); - $('.input-error').removeClass('input-error'); - - if (response.success) { - if (response.url) { - window.location = response.url; - } - } else { - if (response.errors) { - displayFormErrors(response.errors); - } - } -} $(document).ready(function() { @@ -61,99 +69,10 @@ $(document).ready(function() { } }); - - - // Registration handler using AJAX - $('#register-form').on('submit', function(event) { - - event.preventDefault(); - showLoadingBtn(true); - - $.post('/register', { - 'role': $('input[name="role"]:checked').attr('id'), - 'first_name': $('#first_name').val(), - 'last_name': $('#last_name').val(), - 'email': $('#email').val(), - 'password': $('#password').val() - }, - function(data) { - showLoadingBtn(false); - ajaxFormResponseHandler(data); - }); - }); - - - // Login handler using AJAX - $('#login-form').on('submit', function(event) { - - event.preventDefault(); - showLoadingBtn(true); - - $.post( - '/login', { - 'email': $('#email').val(), - 'password': $('#password').val() - }, - function(data) { - showLoadingBtn(false); - ajaxFormResponseHandler(data); - } - ); - }); - - - // Resend email verification handler using AJAX - $('#verify-email-form').on('submit', function(event) { - - event.preventDefault(); - showLoadingBtn(true); - - $.post( - '/verify-email', {}, - function(data) { - showLoadingBtn(false); - ajaxFormResponseHandler(data); - } - ); - }); - - - // Password reset request handler using AJAX - $('#initiate-password-reset-form').on('submit', function(event) { - - event.preventDefault(); - showLoadingBtn(true); - - $.post( - '/reset-password', { - 'form-type': 'initiate_password_reset', - 'email': $('#email').val() - }, - function(data) { - showLoadingBtn(false); - ajaxFormResponseHandler(data); - } - ); - }); - - - // Password reset handler using AJAX - $('#reset-password-form').on('submit', function(event) { - - event.preventDefault(); - showLoadingBtn(true) - - $.post( - '/reset-password', { - 'form-type': 'reset_password', - 'email': $('#email').val(), - 'password': $('#password').val(), - 'password_confirmation': $('#password_confirmation').val() - }, - function(data) { - showLoadingBtn(false); - ajaxFormResponseHandler(data); - } - ); - }); + // Form handlers using AJAX + handleSubmitForm('register-form', '/register'); + handleSubmitForm('login-form', '/login'); + handleSubmitForm('verify-email-form', '/verify-email'); + handleSubmitForm('initiate-password-reset-form', '/reset-password'); + handleSubmitForm('reset-password-form', '/reset-password'); }); diff --git a/app/templates/login.html b/app/templates/login.html index 61ac985..1f08d0d 100644 --- a/app/templates/login.html +++ b/app/templates/login.html @@ -11,14 +11,14 @@Don't have an account? Register