apply the page shell to the admin texts page and present each existing text as a card-link plus the new-text form as a card with a primary submit button. ids (#texts-list, #newTextName, #submit, #back) and the name attribute on the input are preserved so the existing cypress flows continue to work.
42 lines
1.3 KiB
JavaScript
42 lines
1.3 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/texts', {
|
|
credentials: 'same-origin',
|
|
});
|
|
const texts = await res.json();
|
|
textsList.innerHTML = texts.map(text =>
|
|
'<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();
|
|
});
|