- Count: - - View requests in any state +
diff --git a/public/main.js b/public/main.js index 73559b2..3b2da71 100644 --- a/public/main.js +++ b/public/main.js @@ -1,29 +1,49 @@ var requestsDiv = document.getElementById("requests"); -var cronJobs = ['processBans'] +var cronJobs = ['processBans']; +var currentPage = 1; +var totalPages = 1; -function getRequests(count,allRequests) { - var reqUrl; - if (allRequests) { - reqUrl = "/api/getAllRequests"; - } else { - reqUrl = "/api/getRequests"; - } - reqUrl += `?count=${count}`; +function getRequests(count,offset,allRequests) { + if (allRequests) var reqUrl = "/api/getAllRequests"; + else var reqUrl = "/api/getRequests"; + reqUrl += `?count=${count}&offset=${offset}`; fetch(reqUrl) .then(response => response.json()) .then(requests => { - window.requests = requests; - buildTable(); + buildTable(requests); }); } -function buildTable() { +function buildTable(requests) { + totalPages = Math.ceil(requests.total/document.getElementById("count").value); + document.getElementById("totalPages").innerText = totalPages; + if (currentPage <= 1) { + currentPage = 1; + document.getElementById("pageBtnFirst").disabled = true; + document.getElementById("pageBtnPrev").disabled = true; + } else { + document.getElementById("pageBtnFirst").disabled = false; + document.getElementById("pageBtnPrev").disabled = false; + } + if (currentPage >= totalPages) { + currentPage = totalPages; + document.getElementById("pageBtnLast").disabled = true; + document.getElementById("pageBtnNext").disabled = true; + } else { + document.getElementById("pageBtnLast").disabled = false; + document.getElementById("pageBtnNext").disabled = false; + } + document.getElementById("page").innerHTML = ""; + for (i = 1; i <= totalPages; i++) { + document.getElementById("page").innerHTML += ``; + } + document.getElementById("page").value = currentPage; var requestsDivHTML = '
Song | Requester | Score | '; requestsDivHTML += 'State'; if (window.loggedIn) requestsDivHTML += ' | Vote'; if (window.isStreamer) requestsDivHTML += ' | Update | ' requestsDivHTML += "
---|---|---|---|---|---|
${request.title} | \${request.imageurl ? `` : ''}${request.requester} | \${request.score} | `; @@ -45,8 +65,10 @@ function buildTable() { } function updateTable() { - allRequests = document.getElementById("allRequests").checked; - getRequests(document.getElementById("count").value,allRequests); + var count = document.getElementById("count").value; + var offset = (currentPage - 1) * count; + var allRequests = document.getElementById("allRequests").checked; + getRequests(count,offset,allRequests); } function applyUrlTransforms(url) { @@ -66,6 +88,11 @@ function applyUrlTransforms(url) { } } +function goToPage(page) { + currentPage = parseInt(page,10); + updateTable(); +} + function getColorObject() { return { bg: { diff --git a/public/style.css b/public/style.css index 5ff0029..6be863d 100644 --- a/public/style.css +++ b/public/style.css @@ -111,6 +111,11 @@ div#nav-userpic { text-align: right; } +#tableSettings { + display: flex; + justify-content: space-between; +} + #modalBackground { display: none; position: fixed; diff --git a/src/app.ts b/src/app.ts index 3e931ce..efaa358 100644 --- a/src/app.ts +++ b/src/app.ts @@ -42,31 +42,47 @@ app.use(session({ // API app.get("/api/getRequests", async (request, response) => { - if (!request.session) { - throw new Error ("Missing request.session") - } - var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); + if (!request.session) throw new Error ("Missing request.session"); await validateApiToken(request.session); + var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); + var requestOffset = ( request.query.offset ? parseInt(request.query.offset as string, 10) : 0 ); + var requestsTotal = await requests.getRequestsTotal(); if (request.session.user) { - requests.getRequestsVoted(requestCount,request.session.user.id).then((val: Array