diff --git a/src/js/account/helpers.js b/src/js/account/helpers.js new file mode 100644 index 0000000..58108d4 --- /dev/null +++ b/src/js/account/helpers.js @@ -0,0 +1,21 @@ +export function request (data = {}, success = () => {}, error = () => {}, fail = () => {}) { + fetch('./account/', { + method: 'POST', // or 'PUT' + body: JSON.stringify(data), // data can be `string` or {object}! + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'same-origin', + }).then(res => res.json()) + .then(response => { + console.log('Success:', JSON.stringify(response)); + if (response.error) { + error(response); + } + success(response); + }) + .catch(err => { + console.error('Request Error:', err); + fail(err); + }); +} \ No newline at end of file diff --git a/src/js/account/index.js b/src/js/account/index.js new file mode 100644 index 0000000..bcb1285 --- /dev/null +++ b/src/js/account/index.js @@ -0,0 +1,7 @@ +import '../../scss/Account/main.scss'; + +import { renderLoginForm } from "./render"; + +export function showLoginForm() { + renderLoginForm(); +} \ No newline at end of file diff --git a/src/js/account/login.js b/src/js/account/login.js new file mode 100644 index 0000000..f173209 --- /dev/null +++ b/src/js/account/login.js @@ -0,0 +1,25 @@ +import { request } from "./helpers"; + +export function logIn() { + const email = document.getElementById('loginEmail').value.trim(), + password = document.getElementById('loginPassword').value.trim(); + const loginErrorMessages = document.getElementById('loginErrorMessages'); + let errorHTML = ''; + + if (email === '') { + errorHTML += '
Please enter your email address.
'; + } + if (password === '') { + errorHTML += 'Please enter your password.
'; + } + + loginErrorMessages.innerHTML = errorHTML; + + if (errorHTML !== '') { + request({ + action: 'login', + email, + password, + }); + } +} \ No newline at end of file diff --git a/src/js/account/render.js b/src/js/account/render.js new file mode 100644 index 0000000..06be43b --- /dev/null +++ b/src/js/account/render.js @@ -0,0 +1,52 @@ +import { setupLoginModal } from "./setupListeners"; + +export function renderLoginForm() { + const loginModal = document.createElement('section'); + loginModal.classList.add('modal'); + loginModal.id = 'loginModal'; + loginModal.innerHTML = ` + `; + + document.body.appendChild(loginModal); + + setupLoginModal(loginModal); +} \ No newline at end of file diff --git a/src/js/account/setupListeners.js b/src/js/account/setupListeners.js new file mode 100644 index 0000000..bc4507f --- /dev/null +++ b/src/js/account/setupListeners.js @@ -0,0 +1,12 @@ +import { logIn } from "./login"; + +export function setupLoginModal(modal) { + const closeElements = modal.querySelectorAll('.modal-background, .close-button'); + Array.from(closeElements).forEach(close => { + close.addEventListener('click', () => { + modal.parentElement.removeChild(modal); + }); + }); + + document.getElementById('loginSubmit').addEventListener('click', logIn); +} \ No newline at end of file diff --git a/src/js/setupListeners.js b/src/js/setupListeners.js index 820e060..e79b0c5 100644 --- a/src/js/setupListeners.js +++ b/src/js/setupListeners.js @@ -11,8 +11,7 @@ import { showSearchModal, clearSearchText } from './search'; export default function setupListeners() { setupDetailsTabs(); - setupSearchBar(); - setupSettingsModal(); + setupHeaderButtons(); setupWordForm(); setupMobileWordFormButton(); setupInfoButtons(); @@ -21,6 +20,17 @@ export default function setupListeners() { } } +export function setupHeaderButtons() { + setupSearchBar(); + setupSettingsModal(); + + document.getElementById('loginCreateAccountButton').addEventListener('click', () => { + import('./account/index.js').then(account => { + account.showLoginForm(); + }); + }); +} + function setupDetailsTabs() { const tabs = document.querySelectorAll('#detailsSection nav li'); tabs.forEach(tab => { diff --git a/src/scss/Account/_structure.scss b/src/scss/Account/_structure.scss new file mode 100644 index 0000000..e54f47f --- /dev/null +++ b/src/scss/Account/_structure.scss @@ -0,0 +1,16 @@ +#loginModal { + .modal-content { + padding: $general-padding; + width: 1000px; + + section { + overflow-y: auto; + height: 100%; + + p { + font-size: 10pt; + line-height: 12pt; + } + } + } +} \ No newline at end of file diff --git a/src/scss/Account/main.scss b/src/scss/Account/main.scss new file mode 100644 index 0000000..3d9cd0d --- /dev/null +++ b/src/scss/Account/main.scss @@ -0,0 +1,3 @@ +@import '../variables'; + +@import './structure'; \ No newline at end of file