From f277ae79836b41f2c943b038ee08b516adb48199 Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Sat, 18 Apr 2026 22:06:25 +0300 Subject: [PATCH] move fetch nodes by text id into its own function re renders if theres a root list of nodes existing --- public/js/text.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/public/js/text.js b/public/js/text.js index eafc1c6..8f7b72e 100644 --- a/public/js/text.js +++ b/public/js/text.js @@ -8,16 +8,23 @@ document.addEventListener('DOMContentLoaded', () => { h1.textContent = text.name; document.getElementById('text-detail').appendChild(h1); - return fetch('/api/nodes/' + textId ); - }) - .then(res => res.json()) - .then(nodes => { - const tree = buildTree(nodes); - const ul = renderTree(tree); - document.getElementById('text-detail').appendChild(ul); + return fetchAndRenderNodes(textId); }); }); +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) { const map = {}; nodes.forEach(node => {