diff --git a/static/js/settings.js b/static/js/settings.js new file mode 100644 index 0000000..4183cc9 --- /dev/null +++ b/static/js/settings.js @@ -0,0 +1,122 @@ +'use strict'; +/* global location $ */ + +// Validate email addresses +function validateEmail(email) { + var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + return re.test(email); +} + +// Turn inputed value into slug +function slugify(cb) { + $.get('/validate?slugify='+$('#slug-input').val()) + .done(function(data){ + console.log('Got '+data); + $('#slug-input').val(data); + cb(); + }); +} + +// On page load +$(function(){ + + function validateForm(input) { + + // Everything passed - make sure no help texts are visible + function recheckInputs() { + if ($('#email-help').is(":visible")) { + $('#submit-group .main').prop('disabled',true).prop('title',"You need to supply a different email address. "); + } + else if ($('#slug-help').is(":visible")) { + $('#submit-group .main').prop('disabled',true).prop('title',"You need to supply a different slug. "); + } + else { + $('#submit-group .main').prop('disabled',false).prop('title',"Click here to save your changes. "); + } + } + + // Empty fields + if ($('#slug-input').val()===''){ + $('#slug-help').show().text("A slug is required. "); + $('#submit-group .main').prop('disabled',true).prop('title',"You need to enter a slug. "); + } + else if ($('#email-input').val()===''){ + $('#email-help').show().text("An email is required. "); + $('#submit-group .main').prop('disabled',true).prop('title',"You need to enter an email address. "); + } + + // Is email + else if (!validateEmail($('#email-input').val())) { + $('#email-help').show().text("You must enter a valid email address. "); + $('#submit-group .main').prop('disabled',true).prop('title',"You need to enter a valid email address. "); + } + + // Validate unique fields with server + else if (input) { + + // Make AJAX request + $.get('/validate?'+input+'='+$('#'+input+'-input').val()) + .fail(function(data,status){ + + console.log(typeof status); + console.log(status); + + // Input is not unique + if (status===400) { + $('#'+input+'-help').show().text("That "+input+" is already in use by another user. "); + $('#submit-group .main').prop('disabled',true).prop('title',"You need to supply a different "+input+". "); + } + + // Server error + else { + $('#'+input+'-help').show().text("Unable to confirm unique "+input+" with the server. This might not work... "); + recheckInputs(); + } + + }) + + // Input is unique + .done(function(){ + $('#'+input+'-help').hide(); + recheckInputs(); + }); + + } + + // All passed + else { recheckInputs(); } + + } + + // Validate slug + $('#slug-input').change(function(){ + slugify( function(){ + validateForm('slug'); + }); + }); + + // Validate email + $('#email-input').change(function(){ + validateForm('email'); + }); + + // Validate form after name change + $('#name-input').change(validateForm); + + // Delete account + $('#delete').click(function(){ + if (confirm("Are you sure you want to delete your account? This CANNOT be undone! ")) { + $.ajax({ + url: "/settings", + type: "DELETE", + success: function(){ + location.reload(); + }, + fail: function(){ + alert("Failed to delete account!"); + } + }); + } + }); + +});