Skip to content

Commit

Permalink
Handle logged-in user on page load
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Robert committed Jan 17, 2024
1 parent 0a25ff4 commit 90bdc72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions blocks/login/login-delayed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { close, displayError, reset } from './login.js';
import { login } from '../../scripts/apis/user.js';
import { login, isLoggedIn, getUserDetails } from '../../scripts/apis/user.js';

const block = document.querySelector('.login.block');

Expand Down Expand Up @@ -32,24 +32,36 @@ function loginError(response) {
}
}

/**
* Checks if the user is logged in and updates the header accordingly.
*/
function checkForLoggedInUser() {
if (isLoggedIn()) {
const userDetailsLink = document.body.querySelector('.nav-profile .username a');
document.body.querySelector('.nav-profile .login').style.display = 'none';
document.body.querySelector('.nav-profile .username').style.display = 'block';
const userDetails = getUserDetails();
userDetailsLink.textContent = userDetails?.profile?.firstName || 'Valued Customer';
}
}

/**
* Submits the form.
*
* @param {HTMLFormElement} form
*/
function submit(form) {
async function submit(form) {
reset();

if (isValid(form)) {
const credentials = {
username: form.querySelector('input[name="username"]').value,
password: form.querySelector('input[name="password"]').value,
};
const userDetails = login(credentials, loginError);
const userDetails = await login(credentials, loginError);
if (userDetails) {
close();
const userDetailsLink = document.body.querySelector('.username a');
userDetailsLink.textContent = userDetails?.profile?.firstName || 'Valued Customer';
checkForLoggedInUser();
}
}
}
Expand Down Expand Up @@ -107,3 +119,5 @@ block.querySelector('.cta a.cancel').addEventListener('click', (e) => {
e.stopPropagation();
close();
});

checkForLoggedInUser();

Check failure on line 123 in blocks/login/login-delayed.js

View workflow job for this annotation

GitHub Actions / build

Newline required at end of file but not found
2 changes: 1 addition & 1 deletion scripts/apis/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function getUserDetails() {

async function fetchUserProfile(username) {
const time = new Date().getTime();
const profileResponse = await fetch(`https://www.bhhs.com/bin/bhhs/cregUserProfile?Email=${username}brendan.robert%40gmail.com&_=${time}`);
const profileResponse = await fetch(`${API_URL}/cregUserProfile?Email=${encodeURIComponent(username)}&_=${time}`);
const json = profileResponse.json();
return json;
}
Expand Down

0 comments on commit 90bdc72

Please sign in to comment.