// client/app.js: the page's script. It calls the function at /api/notes on // the page's own address. const list = document.getElementById('notes'); const status = document.getElementById('status'); const form = document.getElementById('add'); const title = document.getElementById('title'); function show(notes) { list.replaceChildren( ...notes.map((note) => { const item = document.createElement('li'); item.textContent = note.title; return item; }), ); status.textContent = notes.length === 0 ? 'No notes yet.' : ''; } async function load() { const response = await fetch('/api/notes'); if (!response.ok) throw new Error('The notes could not be read.'); show((await response.json()).notes); } form.addEventListener('submit', async (event) => { event.preventDefault(); status.textContent = ''; try { const response = await fetch('/api/notes', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ title: title.value }) }); if (!response.ok) throw new Error('The note could not be added.'); title.value = ''; await load(); } catch (error) { status.textContent = error.message; } }); load().catch((error) => { status.textContent = error.message; });