-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathHeader.js
More file actions
53 lines (51 loc) · 1.7 KB
/
Header.js
File metadata and controls
53 lines (51 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { getCartItems, getUserInfo } from '../localStorage';
import { parseRequestUrl } from '../utils';
const Header = {
render: () => {
const { name, isAdmin } = getUserInfo();
const { value } = parseRequestUrl();
const totalCartItems = JSON.parse(localStorage.cartItems)[0]===undefined?'':JSON.parse(localStorage.cartItems)[0].qty
const textAppendedAfterCart = totalCartItems>0?`(${totalCartItems})`:'';
return `
<div class="brand">
<button id="aside-open-button">
☰
</button>
<a href="/#/">jsamazona</a>
</div>
<div class="search">
<form class="search-form" id="search-form">
<input type="text" name="q" id="q" value="${value || ''}" />
<button type="submit"><i class="fa fa-search"></i></button>
</form>
</div>
<div>
${
name
? `<a href="/#/profile">${name}</a>`
: `<a href="/#/signin">Sign-In</a>`
}
<a href="/#/cart">Cart${textAppendedAfterCart}</a>
${isAdmin ? `<a href="/#/dashboard">Dashboard</a>` : ''}
</div>`;
},
after_render: () => {
window.addEventListener('cartUpdated', () => {
// Re-render the header when the cart is updated
document.getElementById('header-container').innerHTML = Header.render();
});
document
.getElementById('search-form')
.addEventListener('submit', async (e) => {
e.preventDefault();
const searchKeyword = document.getElementById('q').value;
document.location.hash = `/?q=${searchKeyword}`;
});
document
.getElementById('aside-open-button')
.addEventListener('click', async () => {
document.getElementById('aside-container').classList.add('open');
});
},
};
export default Header;