-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
79 lines (77 loc) · 3.44 KB
/
index.html
File metadata and controls
79 lines (77 loc) · 3.44 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
<!DOCTYPE html>
<html>
<head>
<title>My Collection</title>
<link rel="stylesheet" type="text/css" href="./My_Collection/koley.css?v=1">
<script src="./My_Collection/settings-applier.js"></script>
</head>
<body>
<div class="page">
<div class="content">
<header>
<h1>My Collection</h1>
</header>
<div id="homepage-content">
Loading...
</div>
</div>
<div class="footer">
</div>
<div class="errors">
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const API_URL = `http://localhost:8080`;
const homepageContent = document.getElementById('homepage-content');
fetch(`${API_URL}/api/home`)
.then(response => {
console.log("Raw Response:", response);
return response.json();
})
.then(data => {
let html = '';
if (data && data.isAuthenticated) {
html += '<h1><a href="My_Collection/scanner.html">Scan</a></h1>';
html += '<h1><a href="My_Collection/collections.html">Collection</a></h1>';
html += '<h1><a id="random-entry-link-home" href="#">Random</a></h1>';
} else {
html += '<a href="/loginform">Click here to log in or register!</a>';
}
homepageContent.innerHTML = html;
// Attach the event listener AFTER the element is added to the DOM
const randomEntryLinkHome = document.getElementById('random-entry-link-home');
if (randomEntryLinkHome) {
randomEntryLinkHome.addEventListener('click', function(event) {
event.preventDefault();
fetch(`${API_URL}/api/random_entry`)
.then(response => {
if (!response.ok) {
return response.json().then(err => {
throw new Error(err.error || 'Failed to get random entry ID');
});
}
return response.json();
})
.then(data => {
if (data && data.entry_id) {
window.location.href = `/My_Collection/entry_detail.html?entry_id=${data.entry_id}`;
} else {
alert('Could not retrieve a random entry ID.');
}
})
.catch(error => {
console.error('Error fetching random entry:', error);
alert(error.message);
});
});
}
})
.catch(error => {
console.error('Error fetching home page data:', error);
document.getElementById('homepage-content').textContent = 'Failed to load content.';
});
});
</script>
</body>
</html>