From 4b0a9dd484b5fd03ff25b78935a297c0239ce3bf Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Sun, 22 Jan 2023 22:04:26 -0500 Subject: fix endpoint, html parse, load more posts --- public/js/api.js | 2 +- public/js/home.js | 27 ++++++++++++++++++++++++--- public/js/main.js | 7 +++++++ 3 files changed, 32 insertions(+), 4 deletions(-) (limited to 'public/js') diff --git a/public/js/api.js b/public/js/api.js index 371ecf3..9b5a4dd 100644 --- a/public/js/api.js +++ b/public/js/api.js @@ -1,4 +1,4 @@ -const endpoint = 'https://xssbook.com/api' +const endpoint = '/api' const request = async (url, body, method) => { if (method === undefined) method = 'POST' diff --git a/public/js/home.js b/public/js/home.js index 7697c29..f3711ad 100644 --- a/public/js/home.js +++ b/public/js/home.js @@ -179,6 +179,18 @@ function render() { ` add(popup, 'popup') + + const load = ` +
+ Load More posts +
+ ` + + if (page !== -1) { + add(load, 'load') + } else { + remove('load') + } } var page = 0 @@ -189,9 +201,12 @@ const data = { } async function load() { - header(true, false) const posts = (await loadposts(page)).json - page++; + if (posts.length === 0) { + page = -1; + } else { + page++; + } data.posts.push(... posts) const batch = [] for (const post of posts) { @@ -208,7 +223,13 @@ async function load() { for (const id in users) { data.users[id] = users[id] } + render() +} + + +async function init() { + header(true, false) data.user = (await loadself()).json data.users[data.user.id] = data.user - render() + load() } \ No newline at end of file diff --git a/public/js/main.js b/public/js/main.js index 42cc6fe..0003c0d 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -12,4 +12,11 @@ function add(html, id) { document.body.appendChild( range.createContextualFragment(html) ) +} + +function remove(id) { + const old = document.getElementById(id) + if (old !== null) { + old.remove() + } } \ No newline at end of file -- cgit v1.2.3-freya