Created
October 23, 2018 09:44
-
-
Save NickToye/31a8d1614841b5ace2baaafcda021be3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sofologyLogin = (function () { | |
function logInExistingUser(form, email, password, optInMarketing) { | |
var deferred = new $.Deferred(); | |
hybrisModule.submitLogin(email.val(), password.val(), optInMarketing).then(function (res) { | |
hybrisModule.retrieveUsersBasket().then(function (res) { | |
return deferred.resolve({ success: true, response: res }); | |
}); | |
}) | |
.fail(function (res) { | |
return deferred.resolve({ success: false }); | |
}); | |
return deferred.promise(); | |
} | |
function registerNewUser(form, email, password,optInToMarketing) { | |
var deferred = new $.Deferred(); | |
hybrisModule.submitRegistration(email.val(), password.val(), optInToMarketing) | |
.then(function () { | |
logInExistingUser(form, email, password, optInToMarketing) | |
.then(function (res) { | |
return deferred.resolve({ success: true, response: res }); | |
}); | |
}).fail(function (response) { | |
return deferred.resolve({ success: false }); | |
}); | |
return deferred.promise(); | |
} | |
function resetPassword(email) { | |
hybrisModule.forgotPassword(email.val()); | |
} | |
function showSuccessMessage(redirectUrl) { | |
if (redirectUrl !== undefined && redirectUrl !== '') { | |
window.location.href = redirectUrl; | |
} | |
$.event.trigger({ type: "loginStateChanged" }); | |
$(".c-login-form").hide(); | |
$(".c-login-success-text").show(); | |
$(".nav-login").hide(); | |
var sessionId = hybrisModule.sessionGet('sofologySessionId'); | |
if (sessionId) { | |
hybrisModule.reloadCurrentUsersCart() | |
.done(function (data) { | |
$(document).trigger("basketUpdated", data); | |
}); | |
} | |
} | |
function init(form) { | |
var email = form.find("input[name*='email']"); | |
var password = form.find("input[name*='password']"); | |
var confirmEmail = form.find("input[name*='confirmEmail']"); | |
var newUser = false; | |
var slideContainer = form.find(".c-SlideContainer"); | |
var insertEmailButton = form.find(".insert-email-step"); | |
var confirmEmailButton = form.find(".confirm-email-step"); | |
var submitButton = form.find("input[type*='submit']"); | |
var backArrow = form.find(".back"); | |
var mailIcon = form.find(".sofology-buttons .message"); | |
var position = 1; | |
var nextArrow = form.find(".SlideAction.sofology-buttons"); | |
var forgotPasswordLink = form.find(".forgot-password-link"); | |
var passwordSentText = form.find(".password-sent"); | |
var loader = form.find(".card-loader"); | |
var basketSelection = form.parent().find(".basket-selection"); | |
var savedBasket = basketSelection.find(".retrieve-saved-basket"); | |
var newBasket = basketSelection.find(".new-basket"); | |
var savedSession = null; | |
var isHeader = form.parent().hasClass("login-header-area"); | |
var optInToMarketing = form.find(".opt-in-marketing"); | |
var redirectUrl = ''; | |
if (!isHeader) { | |
redirectUrl = form.parent().data().loginRedirect; | |
} | |
form.find(".facebook-button").click(function () { | |
loader.show(); | |
if (form.find('.facebook-button').is(":visible")) { | |
FB.login(function (response) { | |
if (response.status === 'connected') { | |
hybrisModule.loginWithFacebook(response.authResponse.accessToken, optInToMarketing.is(':checked')).then(function () { | |
hybrisModule.retrieveUsersBasket().then(function (x) { | |
loader.hide(); | |
processLogin(x); | |
}); | |
}); | |
} else { | |
loader.hide(); | |
alert("Sorry there has been a error trying to connect you to your facebook account. Please try again."); | |
} | |
}, { scope: 'public_profile,email' }); | |
} | |
return false; | |
}); | |
form.find('.navOptions').click(function () { | |
form.find(".sofology-login").show(); | |
form.find(".facebook-login").hide(); | |
form.find(".facebook-buttons").hide(); | |
form.find(".sofology-buttons").show(); | |
form.find(".Fbook").removeClass("active"); | |
form.find(".navOptions").addClass("active"); | |
}); | |
form.find('.Fbook').click(function () { | |
form.find(".sofology-login").hide(); | |
form.find(".facebook-login").show(); | |
form.find(".facebook-buttons").show(); | |
form.find(".sofology-buttons").hide(); | |
form.find(".Fbook").addClass("active"); | |
form.find(".navOptions").removeClass("active"); | |
}); | |
form.find('.Fbook').removeClass("active"); | |
form.find('.navOptions').addClass("active"); | |
form.find('.facebook-login').css('display', 'none'); | |
form.find('.sofology-login').css('display', 'block'); | |
savedBasket.click(function () { | |
var url = hybrisModule.applySavedSession(savedSession); | |
if (url != '' && isHeader) { | |
window.location = url; | |
} | |
basketSelection.hide(); | |
showSuccessMessage(redirectUrl); | |
return false; | |
}); | |
newBasket.click(function () { | |
hybrisModule.transferAnonymousBasket().then(function () { | |
basketSelection.hide(); | |
showSuccessMessage(redirectUrl); | |
}) | |
.fail(function () { | |
alert('Sorry there was an issue saving your basket.'); | |
}); | |
return false; | |
}); | |
function goBack() { | |
form.validate().resetForm(); | |
position = position - 1; | |
confirmEmailButton.hide(); | |
submitButton.hide(); | |
insertEmailButton.hide(); | |
forgotPasswordLink.hide(); | |
passwordSentText.hide(); | |
if (position === 1) { | |
insertEmailButton.show(); | |
newUser = false; | |
backArrow.hide(); | |
if (email.val() == '') { | |
nextArrow.removeClass("focus"); | |
} | |
} else if (position === 2) { | |
if (newUser) { | |
confirmEmailButton.show(); | |
} else { | |
submitButton.show(); | |
} | |
} | |
slideContainer.animate({ scrollLeft: "+" + slideContainer.scrollLeft() - slideContainer.width() + "px" }, 800, 'swing'); | |
} | |
function goToPassword() { | |
var validator = form.validate(); | |
if (validator.element(email)) { | |
email.val(email.val().replace(/\s/g, '')); | |
hybrisModule.checkIfUserExists(email.val()).then(function (response) { | |
confirmEmailButton.hide(); | |
submitButton.hide(); | |
insertEmailButton.hide(); | |
backArrow.show(); | |
if (response.userExists) { | |
newUser = false; | |
confirmEmail.val(email.val()); | |
forgotPasswordLink.text("Forgot your password?"); | |
forgotPasswordLink.show(); | |
submitButton.show(); | |
password.attr("placeholder", "Enter your password"); | |
} else { | |
newUser = true; | |
confirmEmail.val(''); | |
confirmEmailButton.show(); | |
password.attr("placeholder", "Password (min 6 characters)"); | |
} | |
nextArrow.addClass("focus"); | |
slideContainer.animate({ scrollLeft: "+" + slideContainer.width() + "px" }, 800, 'swing'); | |
setTimeout(function () { password.focus(); }, 800); | |
position = 2; | |
}); | |
} | |
} | |
function goToConfirmEmail() { | |
var validator = form.validate(); | |
if (validator.element(password)) { | |
event.preventDefault(); | |
var calc = slideContainer.scrollLeft() + slideContainer.width(); | |
slideContainer.animate({ scrollLeft: "+" + calc + "px" }, 800, 'swing'); | |
setTimeout(function () { confirmEmail.focus(); }, 800); | |
position = 3; | |
submitButton.show(); | |
confirmEmailButton.hide(); | |
} | |
} | |
function formSubmit() { | |
loader.show(); | |
if ($(form).valid()) { | |
if (newUser) { | |
registerNewUser(form, email, password, optInToMarketing.is(':checked')) | |
.then(function (result) { | |
if (result.success) { | |
showSuccessMessage(redirectUrl); | |
} else { | |
$(form).validate().showErrors({ | |
"confirmEmail": "There has been a error. Please try again." | |
}); | |
} | |
loader.hide(); | |
}); | |
} else { | |
var oldUser = retailExperience.user(); | |
var basket = hybrisModule.sessionGet('sofologyBasket') | |
logInExistingUser(form, email, password, optInToMarketing.is(':checked')).then(function (result) { | |
if (result.success) { | |
processLogin(result.response); | |
intileryModule.combineUsers(oldUser.userId, email, basket.guid) | |
} else { | |
$(form).validate().showErrors({ "password": "Wrong password. Please try again." }); | |
} | |
loader.hide(); | |
}); | |
} | |
} | |
} | |
function processLogin(x) { | |
try { | |
localStorage.removeItem("rex-recently-viewed"); | |
Cookies.remove("rex-recently-viewed"); | |
Cookies.remove("rex-favourites"); | |
} catch (ex) { | |
} | |
if (x.state === "basketSelection") { | |
savedSession = x.response; | |
$(form).hide(); | |
basketSelection.show(); | |
$('.login-close').hide(); | |
return; | |
} | |
if (isHeader) { | |
if (hybrisModule.sessionGet('sofologist')) { | |
window.location.href = '/checkout/staff-login'; | |
return; | |
} | |
var url = hybrisModule.applySavedSession(x.response); | |
if (url) { | |
window.location = url; | |
return; | |
} | |
} | |
showSuccessMessage(redirectUrl); | |
} | |
forgotPasswordLink.click(function () { | |
form.validate().resetForm(); | |
resetPassword(email); | |
forgotPasswordLink.hide(); | |
passwordSentText.show(); | |
return false; | |
}); | |
password.keypress(function (e) { | |
if (e.which === 13) { | |
if (newUser) { | |
e.preventDefault(); | |
goToConfirmEmail(); | |
return false; | |
} else { | |
formSubmit(); | |
return false; | |
} | |
} | |
}); | |
confirmEmail.keypress(function (e) { | |
if (e.which === 13) { | |
formSubmit(); | |
return false; | |
} | |
}); | |
email.keypress(function (e) { | |
if (e.which === 13) { | |
goToPassword(); | |
return false; | |
} | |
if (e.which === 32) { | |
return false; | |
} | |
}); | |
email.change(function () { | |
$(this).val($(this).val().replace(/\s/g, "")); | |
}); | |
email.focus(function () { | |
event.preventDefault(); | |
mailIcon.hide(); | |
insertEmailButton.show(); | |
nextArrow.addClass("focus"); | |
}); | |
email.blur(function () { | |
event.preventDefault(); | |
if (email.val() === '') { | |
mailIcon.show(); | |
insertEmailButton.hide(); | |
nextArrow.removeClass("focus"); | |
} | |
}); | |
mailIcon.click(function () { | |
event.stopPropagation(); | |
}); | |
backArrow.click(function () { | |
goBack(); | |
}); | |
insertEmailButton.click(function () { | |
event.preventDefault(); | |
goToPassword(); | |
return false; | |
}); | |
confirmEmailButton.click(function () { | |
goToConfirmEmail(); | |
return false; | |
}); | |
$.validator.addMethod('emailMatch', | |
function (value, element, param) { return value.toLowerCase() === param.val().toLowerCase(); }, | |
'The email address you entered did not match.'); | |
$.validator.addMethod("enhancedEmail", | |
function (value, element) { return /(.+)@(.+){2,}\.(.+){2,}/.test(value); }, | |
"Please enter a valid email address."); | |
$.validator.addMethod("password", | |
function (value, element) { | |
if (value.indexOf("?") > -1) { | |
return false; | |
} | |
if (value.indexOf("\u00A3") > -1) { | |
return false; | |
} | |
return true; | |
}, | |
"Passwords cannot contain \? or \u00A3 characters."); | |
$(form).validate({ | |
errorLabelContainer: form.find(".login-error"), | |
rules: { | |
email: { required: true, enhancedEmail: true }, | |
password: { required: true, minlength: 6, password: password }, | |
confirmEmail: { emailMatch: email } | |
}, | |
messages: { | |
email: "Please enter a valid email address.", | |
password: { | |
minlength: "The password needs to be a minimum of 6 characters.", | |
required: "Please enter a password." | |
} | |
}, | |
submitHandler: function (form, event) { | |
event.preventDefault(); | |
formSubmit(); | |
return false; | |
} | |
}); | |
} | |
return { | |
init: function (form) { | |
init(form); | |
}, | |
showSuccessMessage: function () { | |
showSuccessMessage(); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment