demarches-normaliennes/app/javascript/new_design/user-sign_up.js

36 lines
1.5 KiB
JavaScript
Raw Normal View History

import { delegate, show, hide } from '@utils';
2019-12-02 14:20:26 +01:00
import { suggest } from 'email-butler';
const userNewEmailSelector = '#new_user input#user_email';
const passwordFieldSelector = '#new_user input#user_password';
const suggestionsSelector = '.suspect-email';
2019-12-03 10:04:54 +01:00
const emailSuggestionSelector = '.suspect-email .email-suggestion-address';
delegate('focusout', userNewEmailSelector, () => {
2019-12-02 14:20:26 +01:00
// When the user leaves the email input during account creation, we check if this account looks correct.
// If not (e.g if its "bidou@gmail.coo" or "john@yahoo.rf"), we attempt to suggest a fix for the invalid email.
const userEmailInput = document.querySelector(userNewEmailSelector);
const suggestedEmailSpan = document.querySelector(emailSuggestionSelector);
const suggestion = suggest(userEmailInput.value);
if (suggestion && suggestion.full && suggestedEmailSpan) {
suggestedEmailSpan.innerHTML = suggestion.full;
show(document.querySelector(suggestionsSelector));
} else {
hide(document.querySelector(suggestionsSelector));
2019-12-02 14:20:26 +01:00
}
});
export function acceptEmailSuggestion() {
const userEmailInput = document.querySelector(userNewEmailSelector);
const suggestedEmailSpan = document.querySelector(emailSuggestionSelector);
userEmailInput.value = suggestedEmailSpan.innerHTML;
hide(document.querySelector(suggestionsSelector));
document.querySelector(passwordFieldSelector).focus();
}
export function discardEmailSuggestionBox() {
hide(document.querySelector(suggestionsSelector));
}