-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (90 loc) · 2.79 KB
/
Copy pathscript.js
File metadata and controls
102 lines (90 loc) · 2.79 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Shared Logic
document.addEventListener('DOMContentLoaded', () => {
injectNavbar();
});
function injectNavbar() {
let user = null;
try {
user = JSON.parse(localStorage.getItem('user'));
} catch (e) {
console.error('Error parsing user from localStorage:', e);
localStorage.removeItem('user');
}
console.log('Current user:', user); // Debugging
let authLinks = '';
if (user) {
authLinks = `
<span style="color: white; margin-right: 1rem;">Welcome, ${user.name}</span>
<a href="#" id="logout-btn" class="btn btn-outline">Logout</a>
`;
// Update Get Started button if it exists
const getStartedBtn = document.getElementById('get-started-btn');
if (getStartedBtn) {
getStartedBtn.textContent = 'View Recipes';
getStartedBtn.href = '/recipe-list.html';
}
} else {
authLinks = `
<a href="/signin.html" class="btn btn-outline">Sign In</a>
<a href="/signup.html" class="btn btn-primary">Sign Up</a>
`;
}
let navLinks = `
<li><a href="/">Home</a></li>
<li><a href="/most-popular.html">Most Popular</a></li>
`;
if (user) {
navLinks += `
<li><a href="/recipe-list.html">Recipes</a></li>
<li><a href="/add-recipe.html">Add Recipe</a></li>
`;
}
navLinks += `
<li><a href="/contact.html">Contact</a></li>
<li><a href="/about-us.html">About Us</a></li>
`;
const navbarHTML = `
<nav class="navbar">
<div class="logo">DISHDELIGHT</div>
<ul class="nav-links">
${navLinks}
</ul>
<div class="auth-buttons">
${authLinks}
</div>
</nav>
`;
const header = document.querySelector('header');
if (header) {
header.innerHTML = navbarHTML;
} else {
const navContainer = document.createElement('header');
navContainer.innerHTML = navbarHTML;
document.body.prepend(navContainer);
}
if (user) {
const logoutBtn = document.getElementById('logout-btn');
if (logoutBtn) {
logoutBtn.addEventListener('click', (e) => {
e.preventDefault();
localStorage.removeItem('user');
window.location.href = '/signin.html';
});
}
}
}
async function apiFetch(url, options = {}) {
try {
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
});
return await response.json();
} catch (error) {
console.error('API Error:', error);
alert('Something went wrong. Please try again.');
}
}