From f8ae41bca6b06243c83142bbbdf7c7d01c1e99be Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 14 May 2019 17:22:17 -0600 Subject: [PATCH] Add create account --- src/js/account/login.js | 64 ++++++++++++++++++++++++++++++++ src/js/account/render.js | 1 + src/js/account/setupListeners.js | 3 +- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/js/account/login.js b/src/js/account/login.js index f173209..5fce9f3 100644 --- a/src/js/account/login.js +++ b/src/js/account/login.js @@ -1,4 +1,5 @@ import { request } from "./helpers"; +import { addMessage } from "../utilities"; export function logIn() { const email = document.getElementById('loginEmail').value.trim(), @@ -22,4 +23,67 @@ export function logIn() { password, }); } +} + +export function createAccount() { + const email = document.getElementById('createNewEmail').value.trim(), + password = document.getElementById('createNewPassword').value.trim(), + confirm = document.getElementById('createNewConfirm').value.trim(), + publicName = document.getElementById('createNewPublicName').value.trim(), + allowEmail = document.getElementById('createNewAllowEmails').checked; + const createAccountErrorMessages = document.getElementById('createAccountErrorMessages'); + let errorHTML = ''; + + if (email === '') { + errorHTML += '

Please enter an email address.

'; + } else if (!/.+@.+\..+/.test(email)) { + errorHTML += '

Please double-check your email address.

'; + } + if (password === '') { + errorHTML += '

Please enter a password.

'; + } else if (confirm !== password) { + errorHTML += '

The password you entered to confirm did not match the password you entered.

'; + } + + createAccountErrorMessages.innerHTML = errorHTML; + if (errorHTML === '') { + request({ + action: 'check-email', + email, + }, emailExists => { + if (emailExists) { + errorHTML += '

The email address you entered already exists.

'; + } + }, errorData => { + console.error(errorData); + errorHTML += `

${errorData}

`; + }).then(() => { + createAccountErrorMessages.innerHTML = errorHTML; + if (errorHTML === '') { + console.log('creating account'); + request({ + action: 'create-account', + email, + password, + userData: { + publicName, + allowEmail, + }, + }, responseData => { + return responseData; + }, errorData => { + errorHTML += `

${errorData}

`; + }).then(responseData => { + console.log(responseData); + createAccountErrorMessages.innerHTML = errorHTML; + if (errorHTML === '') { + const loginModal = document.getElementById('loginModal'); + loginModal.parentElement.removeChild(loginModal); + addMessage('Account Created Successfully!'); + addMessage(`Welcome${publicName !== '' ? ', ' + publicName : ''}! You are logged in.`); + } + }); + } + }).catch(err => console.error(err)); + } } \ No newline at end of file diff --git a/src/js/account/render.js b/src/js/account/render.js index 06be43b..a638feb 100644 --- a/src/js/account/render.js +++ b/src/js/account/render.js @@ -40,6 +40,7 @@ export function renderLoginForm() { +
Create Account diff --git a/src/js/account/setupListeners.js b/src/js/account/setupListeners.js index bc4507f..7e00593 100644 --- a/src/js/account/setupListeners.js +++ b/src/js/account/setupListeners.js @@ -1,4 +1,4 @@ -import { logIn } from "./login"; +import { logIn, createAccount } from "./login"; export function setupLoginModal(modal) { const closeElements = modal.querySelectorAll('.modal-background, .close-button'); @@ -9,4 +9,5 @@ export function setupLoginModal(modal) { }); document.getElementById('loginSubmit').addEventListener('click', logIn); + document.getElementById('createAccountSubmit').addEventListener('click', createAccount); } \ No newline at end of file