From 4a67716e13b0c37fe847339bd8dd1a65075de30e Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Mon, 3 May 2021 19:29:26 +0200 Subject: [PATCH] Reuse fetched entries from template --- package.json | 2 +- src/main/js/main.js | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index e53f1f8..58c877b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tools", - "version": "1.1.3", + "version": "1.1.4", "description": "Main page for the /tools directory.", "author": "Felix W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/js/main.js b/src/main/js/main.js index f362dbb..a04fbd5 100644 --- a/src/main/js/main.js +++ b/src/main/js/main.js @@ -2,7 +2,14 @@ const {$, doAfterLoad, footer, header, nav, stringToHtml} = window.fwdekker; -const unpackEntry = function (entry, depth = 0) { +/** + * Transforms the given (nested) entry into a list item to be displayed in HTML. + * + * @param entry the entry to transform + * @param depth the current level of nesting + * @returns {string} the HTML form of the given entry + */ +const unpackEntry = (entry, depth = 0) => { const subEntries = entry.entries.length === 0 ? "" : ``; @@ -14,19 +21,24 @@ const unpackEntry = function (entry, depth = 0) { ``; }; +/** + * Displays the given entries on the page. + * + * @param entries the entries to display + */ +const showEntries = entries => { + const toolEntries = entries.entries.find(it => it.name === "Tools").entries; + const entryHtml = stringToHtml(``, "ul"); + $("#listing").append(entryHtml); +}; + doAfterLoad(() => { - $("#nav").appendChild(nav("/Tools/")); + $("#nav").appendChild(nav("/Tools/", showEntries)); $("#header").appendChild(header({title: "Tools", description: "An overview of tools I have created"})); $("#footer").appendChild(footer({ vcsURL: "https://git.fwdekker.com/FWDekker/tools/", version: "v%%VERSION_NUMBER%%" })); $("main").classList.remove("hidden"); - - fetch("https://fwdekker.com/api/nav/") - .then(it => it.json()) - .then(json => json.entries.find(it => it.name === "Tools")) - .then(json => stringToHtml(``, "ul")) - .then(listing => $("#listing").append(listing)); });