import $ from 'jquery'; import isEmail from 'validator/lib/isEmail'; import isEmpty from 'validator/lib/isEmpty'; import isLength from 'validator/lib/isLength'; $(document).ready(function () { let auth = $('.js-auth'), type = auth.find('.js-auth-type'), tab = auth.find('.js-auth-tab'), login = auth.find('.js-auth-login'), pass = auth.find('.js-auth-pass'), goPass = auth.find('.js-auth-go-pass'), goEnter = auth.find('.js-auth-go-enter'); type.on('click', function (e) { e.preventDefault(); let _this = $(this), index = _this.index(); type.removeClass('active'); _this.addClass('active'); tab.hide(); tab.eq(index).fadeIn(); }); goPass.on('click', function (e) { e.preventDefault(); login.hide(); pass.fadeIn(); }); goEnter.on('click', function (e) { e.preventDefault(); pass.hide(); login.fadeIn(); }); $('#password-reset__success-hide').on('click', function (e) { e.preventDefault(); $('#password-reset__form-wrapper').show(); $('#password-reset__sent').hide(); }); let passwordResetForm = $('#password-reset-form'); passwordResetForm.on('submit', function (e) { e.preventDefault(); let passwordResetAllowed = true; $('#password-reset__email-field').removeClass('error'); $('.password-reset-form__field-error').text(''); const emailValue = $('#password-reset__email').val(); if (isEmpty(emailValue)) { $('#password-reset__email-field').addClass('error'); $('#password-reset-field-error__email').text('Укажите почту'); passwordResetAllowed = false; } else if (!isEmail(emailValue)) { $('#password-reset__email-field').addClass('error'); $('#password-reset-field-error__email').text('Похоже, вы допустили ошибку в почте'); passwordResetAllowed = false; } if (!passwordResetAllowed) { return; } $('.form__common-error').hide(); $.ajax(passwordResetForm.attr('action'), { method: 'POST', data: passwordResetForm.serialize(), }) .done(function (data) { if (data.success === true) { $('#password-reset__form-wrapper').hide(); $('#password-reset__sent').show(); } }) .fail(function (xhr) { console.log('error', xhr); if (xhr.status === 400) { if (xhr.responseJSON.errors) { for (let errorField in xhr.responseJSON.errors) { if (!xhr.responseJSON.errors.hasOwnProperty(errorField)) { continue; } const errorMessage = xhr.responseJSON.errors[errorField][0].message; if (errorField === '__all__') { $('#password-reset-field-error__all').text(errorMessage).show(); } else { $(`#password-reset-field-error__${errorField}`).text(errorMessage); $(`#password-reset__${errorField}-field`).addClass('error'); } } return; } } $('#learner-auth-field-error__all').text('Произошла незвестная ошибка'); }); }); let authForm = $('#learner-auth-form'); authForm.on('submit', function (e) { e.preventDefault(); let authAllowed = true; $('.learner-auth-form__field').removeClass('error'); $('.learner-auth-form__field-error').text(''); $('.form__common-error').hide(); const emailValue = $('#learner-auth-form__email').val(); if (isEmpty(emailValue)) { $('#learner-auth-field__username').addClass('error'); $('#learner-auth-field-error__username').text('Укажите почту'); authAllowed = false; } else if (!isEmail(emailValue)) { $('#learner-auth-field__username').addClass('error'); $('#learner-auth-field-error__username').text('Похоже, вы допустили ошибку в почте'); authAllowed = false; } if (!isLength($('#learner-auth-form__password').val(), {min: 5, max: undefined})) { $('#learner-auth-field__password').addClass('error'); $('#learner-auth-field-error__password').text('Наберите ваш пароль, минимум 5 символов в длину'); authAllowed = false; } if (!authAllowed) { return; } $.ajax(authForm.attr('action'), { method: 'POST', data: authForm.serialize(), }) .done(function (data) { if (data.success === true) { location.reload(); } }) .fail(function (xhr) { console.log('error', xhr); if (xhr.status === 400) { if (xhr.responseJSON.errors) { for (let errorField in xhr.responseJSON.errors) { if (!xhr.responseJSON.errors.hasOwnProperty(errorField)) { continue; } const errorMessage = xhr.responseJSON.errors[errorField][0].message; if (errorField === '__all__') { $('#learner-auth-field-error__all').text(errorMessage).show(); } else { $(`#learner-auth-field-error__${errorField}`).text(errorMessage); $(`#learner-auth-field__${errorField}`).addClass('error'); } } return; } } $('#learner-auth-field-error__all').text('Произошла незвестная ошибка'); }); }); let registrationForm = $('#learner-registration-form'); registrationForm.on('submit', function (e) { e.preventDefault(); let registrationAllowed = true; $('.learner-registration-form__field').removeClass('error'); $('.learner-registration-form__field-error').text(''); if (isEmpty($('#learner-registration-form__first-name').val())) { $('#learner-registration-field__first-name').addClass('error'); $('#learner-registration-field-error__first-name').text('Укажите имя'); registrationAllowed = false; } if (isEmpty($('#learner-registration-form__last-name').val())) { $('#learner-registration-field__last-name').addClass('error'); $('#learner-registration-field-error__last-name').text('Укажите фамилию'); registrationAllowed = false; } const emailValue = $('#learner-registration-form__email').val(); if (isEmpty(emailValue)) { $('#learner-registration-field__email').addClass('error'); $('#learner-registration-field-error__email').text('Укажите почту'); registrationAllowed = false; } else if (!isEmail(emailValue)) { $('#learner-registration-field__email').addClass('error'); $('#learner-registration-field-error__email').text('Похоже, вы допустили ошибку в почте'); registrationAllowed = false; } if (!isLength($('#learner-registration-form__password').val(), {min: 5, max: undefined})) { $('#learner-registration-field__password').addClass('error'); $('#learner-registration-field-error__password').text('Укажите пароль, минимум 5 символов в длину'); registrationAllowed = false; } if (!registrationAllowed) { return; } $('.form__common-error').hide(); $.ajax(registrationForm.attr('action'), { method: 'POST', data: registrationForm.serialize(), }) .done(function (data) { if (data.success === true) { location.reload(); } }) .fail(function (xhr) { console.log('error', xhr); if (xhr.status === 400) { if (xhr.responseJSON.errors) { for (let errorField in xhr.responseJSON.errors) { if (!xhr.responseJSON.errors.hasOwnProperty(errorField)) { continue; } const errorMessage = xhr.responseJSON.errors[errorField][0].message; if (errorField === '__all__') { $('#learner-registration-field-error__all').text(errorMessage).show(); } else { $(`#learner-registration-field-error__${errorField}`).text(errorMessage); $(`#learner-registration-field__${errorField}`).addClass('error'); } } return; } } $('#learner-registration-field-error__all').text('Произошла незвестная ошибка'); }); }); });