From 6d0db59e5ec82185fa5439b56505cd5ef71c618d Mon Sep 17 00:00:00 2001 From: dicedtomatoreal Date: Thu, 28 May 2020 16:40:04 -0700 Subject: [PATCH] add js to get statistics --- public/js/dashboard.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/public/js/dashboard.js b/public/js/dashboard.js index 447208c8..1e3b5527 100644 --- a/public/js/dashboard.js +++ b/public/js/dashboard.js @@ -161,6 +161,46 @@ $('#typexImagePagination').append(` } }); +document.getElementById('updateStatistics').addEventListener('click', async () => { + const resp = await fetch('/api/images/statistics', { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }); + const json = await resp.json(); + + console.log(json); + try { + document.getElementById('statsDescription').innerHTML = `You have an average of ${Math.floor(json.average)} views on your images, you have ${json.totalViews} views total, you currently have ${json.images} images!` + document.getElementById('statsLeaderboardImages').innerHTML = ''; + document.getElementById('statsLeaderboardImageViews').innerHTML = ''; + for (let i = 0; i < json.table.images.length; i++) { + const c = json.table.images[i]; + const v = json.table.views[i]; + $('#statsLeaderboardImages').append(` + + ${i+1} + ${c.username} + ${c.count} + + `) + } + for (let i = 0; i < json.table.views.length; i++) { + const c = json.table.views[i]; + $('#statsLeaderboardImageViews').append(` + + ${i+1} + ${c.username} + ${c.count} + + `) + } + } catch (e) { + console.error(e) + } +}); + const deleteImage = (id, url) => {