You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
329 lines
12 KiB
329 lines
12 KiB
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');
|
|
|
|
$('.js-header-enter').on('click', function () {
|
|
pass.hide();
|
|
login.show();
|
|
});
|
|
|
|
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();
|
|
window.location.reload();
|
|
});
|
|
|
|
$('#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();
|
|
|
|
let passwordResetButton = $('#password-reset__button');
|
|
passwordResetButton.addClass('loading');
|
|
|
|
$.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('Произошла незвестная ошибка');
|
|
})
|
|
.always(() => {
|
|
passwordResetButton.removeClass('loading');
|
|
});
|
|
});
|
|
|
|
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;
|
|
}
|
|
|
|
let authButton = $('#learner-auth__button');
|
|
authButton.addClass('loading');
|
|
|
|
$.ajax(authForm.attr('action'), {
|
|
method: 'POST',
|
|
data: authForm.serialize(),
|
|
})
|
|
.done(function (data) {
|
|
if (data.success === true) {
|
|
location.reload();
|
|
} else {
|
|
authButton.removeClass('loading');
|
|
}
|
|
})
|
|
.fail(function (xhr) {
|
|
console.log('error', xhr);
|
|
authButton.removeClass('loading');
|
|
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();
|
|
let registrationButton = $('#learner-registration-form__submit-button');
|
|
registrationButton.addClass('loading');
|
|
|
|
$.ajax(registrationForm.attr('action'), {
|
|
method: 'POST',
|
|
data: registrationForm.serialize(),
|
|
})
|
|
.done(function (data) {
|
|
if (data.success === true) {
|
|
location.reload();
|
|
} else {
|
|
registrationButton.removeClass('loading');
|
|
}
|
|
})
|
|
.fail(function (xhr) {
|
|
console.log('error', xhr);
|
|
registrationButton.removeClass('loading');
|
|
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('Произошла незвестная ошибка');
|
|
});
|
|
});
|
|
|
|
$.ajaxSetup({cache: true});
|
|
load_facebook();
|
|
|
|
const facebookButton = $('button.btn_fb');
|
|
facebookButton.on('click', function () {
|
|
$('.auth-register__common-error').hide();
|
|
facebookButton.addClass('loading');
|
|
|
|
if (facebookResponse) {
|
|
if (facebookResponse.status === 'connected') {
|
|
login_with_facebook(facebookResponse.authResponse.accessToken);
|
|
return;
|
|
}
|
|
}
|
|
|
|
FB.login(function (response) {
|
|
if (response.status === 'connected') {
|
|
login_with_facebook(response.authResponse.accessToken);
|
|
} else {
|
|
facebookButton.removeClass('loading');
|
|
$('.auth-register__common-error').text('Не удалось авторизоваться через Facebook');
|
|
}
|
|
}, {scope: 'public_profile,email'});
|
|
});
|
|
});
|
|
|
|
let facebookResponse;
|
|
|
|
function load_facebook() {
|
|
$.getScript('https://connect.facebook.net/en_US/sdk.js', function () {
|
|
FB.init({
|
|
appId: '161924711105785',
|
|
version: 'v2.7'
|
|
});
|
|
FB.getLoginStatus(function (response) {
|
|
facebookResponse = response;
|
|
});
|
|
});
|
|
}
|
|
|
|
function login_with_facebook(accessToken) {
|
|
$.ajax('/auth/facebook_login/', {
|
|
method: 'POST',
|
|
data: {'access_token': accessToken},
|
|
})
|
|
.done(function (data) {
|
|
if (data.success === true) {
|
|
location.reload();
|
|
}
|
|
})
|
|
.fail(function (xhr) {
|
|
console.log('error', xhr);
|
|
if (xhr.message) {
|
|
$('.auth-register__common-error').text(xhr.message).show();
|
|
}
|
|
|
|
$('.auth-register__common-error').text('Произошла незвестная ошибка').show();
|
|
})
|
|
.always(function () {
|
|
$('button.btn_fb').removeClass('loading');
|
|
});
|
|
}
|
|
|