-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheader.js
68 lines (59 loc) · 1.64 KB
/
header.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
;(async function () {
const header = document.createElement('header')
const ul = document.createElement('ul')
header.appendChild(ul)
const pathParts = window.location.pathname.split('/')
const currentPage = pathParts[pathParts.length - 1]
const renderHeader = async () => {
const currentUser = await getCurrentUser()
ul.innerHTML = ''
let routes = [
{
label: 'Home',
href: window.location.pathname.replace(currentPage, 'index.html'),
},
]
if (currentUser) {
routes.push({
label: 'Profile',
href: window.location.pathname.replace(currentPage, 'profile.html'),
})
routes.push({
label: 'Logout',
id: 'logout-btn',
href: '#',
})
} else {
routes.push({
label: 'Login',
href: window.location.pathname.replace(currentPage, 'login.html'),
})
routes.push({
label: 'Register',
href: window.location.pathname.replace(
currentPage,
'registration.html'
),
})
}
for (const route of routes) {
const link = document.createElement('a')
link.href = route.href
if (route.id) {
link.setAttribute('id', route.id)
}
const li = document.createElement('li')
li.textContent = route.label
link.appendChild(li)
ul.appendChild(link)
}
}
await renderHeader()
document.body.insertBefore(header, document.body.firstChild)
if (document.querySelector('#logout-btn')) {
document.querySelector('#logout-btn').addEventListener('click', (event) => {
event.preventDefault()
logTheUserOut()
})
}
})()