Commit 768cd071 authored by Bryce Johnson's avatar Bryce Johnson

Clean up username_validator private vars and members.

parent d2bad46e
((global) => { ((global) => {
const debounceTimeoutDuration = 1000; const debounceTimeoutDuration = 1000;
const inputErrorClass = 'gl-field-error-outline'; const invalidInputClass = 'gl-field-error-outline';
const inputSuccessClass = 'gl-field-success-outline'; const successInputClass = 'gl-field-success-outline';
const messageErrorSelector = '.username .validation-error'; const unavailableMessageSelector = '.username .validation-error';
const messageSuccessSelector = '.username .validation-success'; const successMessageSelector = '.username .validation-success';
const messagePendingSelector = '.username .validation-pending'; const pendingMessageSelector = '.username .validation-pending';
const invalidMessageSelector = '.username .gl-field-error';
class UsernameValidator { class UsernameValidator {
constructor() { constructor() {
this.inputElement = $('#new_user_username'); this.inputElement = $('#new_user_username');
this.inputDomElement = this.inputElement.get(0); this.inputDomElement = this.inputElement.get(0);
this.state = {
this.available = false; available: false,
this.valid = false; valid: false,
this.pending = false; pending: false,
this.fresh = true; empty: true
this.empty = true; };
const debounceTimeout = _.debounce((username) => { const debounceTimeout = _.debounce((username) => {
this.validateUsername(username); this.state.validateUsername(username);
}, debounceTimeoutDuration); }, debounceTimeoutDuration);
this.inputElement.on('keyup.username_check', () => { this.inputElement.on('keyup.username_check', () => {
const username = this.inputElement.val(); const username = this.inputElement.val();
this.valid = this.inputDomElement.validity.valid; this.state.valid = this.inputDomElement.validity.valid;
this.fresh = false; this.state.empty = !username.length;
this.empty = !username.length;
if (this.valid) { if (this.state.valid) {
return debounceTimeout(username); return debounceTimeout(username);
} }
...@@ -36,43 +36,43 @@ ...@@ -36,43 +36,43 @@
}); });
// Override generic field validation // Override generic field validation
this.inputElement.on('invalid', this.handleInvalidInput.bind(this)); this.inputElement.on('invalid', this.interceptInvalid.bind(this));
} }
renderState() { renderState() {
// Clear all state // Clear all state
this.clearFieldValidationState(); this.clearFieldValidationState();
if (this.valid && this.available) { if (this.state.valid && this.state.available) {
return this.setSuccessState(); return this.setSuccessState();
} }
if (this.empty) { if (this.state.empty) {
return this.clearFieldValidationState(); return this.clearFieldValidationState();
} }
if (this.pending) { if (this.state.pending) {
return this.setPendingState(); return this.setPendingState();
} }
if (!this.available) { if (!this.state.available) {
return this.setUnavailableState(); return this.setUnavailableState();
} }
if (!this.valid) { if (!this.state.valid) {
return this.setInvalidState(); return this.setInvalidState();
} }
} }
handleInvalidInput(event) { interceptInvalid(event) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
} }
validateUsername(username) { validateUsername(username) {
if (this.valid) { if (this.state.valid) {
this.pending = true; this.state.pending = true;
this.available = false; this.state.available = false;
this.renderState(); this.renderState();
return $.ajax({ return $.ajax({
type: 'GET', type: 'GET',
...@@ -83,38 +83,40 @@ ...@@ -83,38 +83,40 @@
} }
} }
updateValidationState(usernameTaken) { setAvailabilityState(usernameTaken) {
if (usernameTaken) { if (usernameTaken) {
this.valid = false; this.state.valid = false;
this.available = false; this.state.available = false;
} else { } else {
this.available = true; this.state.available = true;
} }
this.pending = false; this.state.pending = false;
this.renderState(); this.renderState();
} }
clearFieldValidationState() { clearFieldValidationState() {
this.inputElement.siblings('p').hide(); // TODO: Double check if this is valid chaining
this.inputElement.removeClass(inputErrorClass); const $input = this.inputElement
this.inputElement.removeClass(inputSuccessClass); .siblings('p').hide().end()
.removeClass(invalidInputClass);
removeClass(successInputClass);
} }
setUnavailableState() { setUnavailableState() {
const $usernameErrorMessage = this.inputElement.siblings(messageErrorSelector); const $usernameUnavailableMessage = this.inputElement.siblings(unavailableMessageSelector);
this.inputElement.addClass(inputErrorClass).removeClass(inputSuccessClass); this.inputElement.addClass(invalidInputClass).removeClass(successInputClass);
$usernameErrorMessage.show(); $usernameUnavailableMessage.show();
} }
setSuccessState() { setSuccessState() {
const $usernameSuccessMessage = this.inputElement.siblings(messageSuccessSelector); const $usernameSuccessMessage = this.inputElement.siblings(successMessageSelector);
this.inputElement.addClass(inputSuccessClass).removeClass(inputErrorClass); this.inputElement.addClass(successInputClass).removeClass(invalidInputClass);
$usernameSuccessMessage.show(); $usernameSuccessMessage.show();
} }
setPendingState(show) { setPendingState() {
const $usernamePendingMessage = $(messagePendingSelector); const $usernamePendingMessage = $(pendingMessageSelector);
if (this.pending) { if (this.state.pending) {
$usernamePendingMessage.show(); $usernamePendingMessage.show();
} else { } else {
$usernamePendingMessage.hide(); $usernamePendingMessage.hide();
...@@ -122,8 +124,9 @@ ...@@ -122,8 +124,9 @@
} }
setInvalidState() { setInvalidState() {
this.inputElement.addClass(inputErrorClass).removeClass(inputSuccessClass); const $inputErrorMessage = $(invalidMessageSelector);
$(`.gl-field-error`).show(); this.inputElement.addClass(invalidInputClass).removeClass(successInputClass);
$inputErrorMessage.show();
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment