-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
59 lines (48 loc) · 1.93 KB
/
main.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
async function fetchAndDisplayAPIData(url) {
try {
let dataEntries = null;
const cachedData = sessionStorage.getItem(url);
if (cachedData) {
dataEntries = JSON.parse(cachedData);
} else {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch data from ${url}`);
}
dataEntries = await response.json();
sessionStorage.setItem(url, JSON.stringify(dataEntries));
}
console.log(dataEntries);
displayArtists(dataEntries);
} catch (err) {
console.error(err);
throw err;
}
}
function displayArtists(artists) {
const artistList = document.getElementById("display");
artistList.innerHTML = '';
artists.forEach((artist, index) => {
const listItem = document.createElement('li');
listItem.textContent = `${index + 1}. ${artist}`;
artistList.appendChild(listItem);
});
}
document.getElementById('artists-medium').addEventListener('click', async () => {
await fetchAndDisplayAPIData('http://localhost:8888/top-artists-medium');
});
document.getElementById('artists-long').addEventListener('click', async () => {
await fetchAndDisplayAPIData('http://localhost:8888/top-artists-long');
});
document.getElementById('artists-short').addEventListener('click', async () => {
await fetchAndDisplayAPIData('http://localhost:8888/top-artists-short');
});
document.getElementById('tracks-medium').addEventListener('click', async () => {
await fetchAndDisplayAPIData('http://localhost:8888/top-tracks-medium');
});
document.getElementById('tracks-long').addEventListener('click', async () => {
await fetchAndDisplayAPIData('http://localhost:8888/top-tracks-long');
});
document.getElementById('tracks-short').addEventListener('click', async () => {
await fetchAndDisplayAPIData('http://localhost:8888/top-tracks-short');
});