(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 = '

There have not been any Songs added, go add some at /admin

'; } else { html = songs.map(function(song) { return `

${song.title || 'Unknown'} by ${song.artist || 'Unknown'}

rating: ${song.rating}

opinion:
${song.opinion || 'none'}
`; }).join(); } } else { html = '

The /api endpoint did not respond correctly :-(

'; } document.querySelector('#main').innerHTML = html; } } document.addEventListener("DOMContentLoaded", loadHomepage); })();