move fetch nodes by text id into its own function

re renders if theres a root list of nodes existing
This commit is contained in:
Yisroel Baum 2026-04-18 22:06:25 +03:00
parent 628c633823
commit f277ae7983
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -8,16 +8,23 @@ document.addEventListener('DOMContentLoaded', () => {
h1.textContent = text.name; h1.textContent = text.name;
document.getElementById('text-detail').appendChild(h1); document.getElementById('text-detail').appendChild(h1);
return fetch('/api/nodes/' + textId ); return fetchAndRenderNodes(textId);
})
.then(res => res.json())
.then(nodes => {
const tree = buildTree(nodes);
const ul = renderTree(tree);
document.getElementById('text-detail').appendChild(ul);
}); });
}); });
function fetchAndRenderNodes(textId) {
return fetch('/api/nodes/' + textId)
.then(res => res.json())
.then(nodes => {
const existing = document.querySelector('#text-detail > ul');
if (existing) existing.remove();
const tree = buildTree(nodes);
const ul = renderTree(tree, textId);
document.getElementById('text-detail').appendChild(ul);
});
}
function buildTree(nodes) { function buildTree(nodes) {
const map = {}; const map = {};
nodes.forEach(node => { nodes.forEach(node => {