FastRoute rejected /api/texts/all because the previously
declared variable route /api/texts/{textId} would shadow it,
crashing the app on boot. move the admin all-texts endpoint
to /api/admin/texts to clear the conflict; admin texts.js
follows the new URL.
43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const textsList = document.getElementById('texts-list');
|
|
const form = document.getElementById('texts-form');
|
|
|
|
async function loadTexts() {
|
|
const res = await fetch('/api/admin/texts', {
|
|
credentials: 'same-origin',
|
|
});
|
|
const texts = await res.json();
|
|
textsList.innerHTML = texts.map(function (text) {
|
|
return '<li class="card"><a class="card-link"'
|
|
+ ' href=/admin/texts/'
|
|
+ text.id
|
|
+ '>'
|
|
+ text.name
|
|
+ '</a></li>';
|
|
}).join('');
|
|
}
|
|
|
|
form.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const formData = new FormData(form);
|
|
const res = await fetch('/api/texts', {
|
|
method: 'POST',
|
|
credentials: 'same-origin',
|
|
body: formData,
|
|
});
|
|
if (res.ok) {
|
|
const text = await res.json();
|
|
const li = document.createElement('li');
|
|
li.className = 'card';
|
|
const a = document.createElement('a');
|
|
a.className = 'card-link';
|
|
a.href = '/admin/texts/' + text.id;
|
|
a.textContent = text.name;
|
|
li.appendChild(a);
|
|
textsList.appendChild(li);
|
|
form.reset();
|
|
}
|
|
});
|
|
|
|
loadTexts();
|
|
});
|