blob: f9ae5fcdb4d8607fe3a1fdf83b64ce8bec2214f1 (
plain)
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
|
(function() {
function loadHomepage() {
const xhr = new XMLHttpRequest();
xhr.open('GET', '/api/contents?type=Song');
xhr.onreadystatechange = renderHomepage;
xhr.send(null);
}
function renderHomepage(event) {
const DONE = 4;
const OK = 200;
let xhr = event.currentTarget;
let html = '';
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
const songs = window.JSON.parse(xhr.responseText).data;
console.log(songs);
if(songs.length === 0){
html = '<p><strong>There have not been any Songs added, <a href="/admin">go add some at /admin</a></strong></p>';
} else {
html = songs.map(function(song) {
return `
<article>
<h3>${song.title || 'Unknown'} by ${song.artist || 'Unknown'}</h3>
<p>rating: ${song.rating}</p>
<h6>opinion:</h6>
<div>${song.opinion || 'none'}
</article>
`;
}).join();
}
} else {
html = '<p><strong>The /api endpoint did not respond correctly :-(</strong></p>';
}
document.querySelector('#main').innerHTML = html;
}
}
document.addEventListener("DOMContentLoaded", loadHomepage);
})();
|