Improve loading torrents

This commit is contained in:
Michał Gątkowski 2022-08-05 22:38:46 +02:00
parent 791e53c84f
commit 45ffca7f9b

View File

@ -7,7 +7,7 @@ let orderBy, ascending; // use `setOrderBy()` to modify orderBy
let lastOrderedValue, lastID; let lastOrderedValue, lastID;
window.onload = function() { window.onload = function () {
if (query !== null && query !== "") { if (query !== null && query !== "") {
orderBy = "RELEVANCE"; orderBy = "RELEVANCE";
} }
@ -52,36 +52,36 @@ function setOrderBy(x) {
} }
function orderedValue(torrent) { function orderedValue(torrent) {
if (orderBy === "TOTAL_SIZE") return torrent.size; if (orderBy === "TOTAL_SIZE") return torrent.size;
else if (orderBy === "DISCOVERED_ON") return torrent.discoveredOn; else if (orderBy === "DISCOVERED_ON") return torrent.discoveredOn;
else if (orderBy === "UPDATED_ON") alert("implement it server side first!"); else if (orderBy === "UPDATED_ON") alert("implement it server side first!");
else if (orderBy === "N_FILES") return torrent.nFiles; else if (orderBy === "N_FILES") return torrent.nFiles;
else if (orderBy === "N_SEEDERS") alert("implement it server side first!"); else if (orderBy === "N_SEEDERS") alert("implement it server side first!");
else if (orderBy === "N_LEECHERS") alert("implement it server side first!"); else if (orderBy === "N_LEECHERS") alert("implement it server side first!");
else if (orderBy === "RELEVANCE") return torrent.relevance; else if (orderBy === "RELEVANCE") return torrent.relevance;
} }
function load() { function load() {
const button = document.getElementsByTagName("button")[0]; const button = document.getElementsByTagName("button")[0];
button.textContent = "Loading More Results..."; button.textContent = "Loading More Results...";
button.setAttribute("disabled", ""); // disable the button whilst loading... button.setAttribute("disabled", ""); // disable the button whilst loading...
const ul = document.querySelector("main ul"); const ul = document.querySelector("main ul");
const template = document.getElementById("item-template").innerHTML; const template = document.getElementById("item-template").innerHTML;
const reqURL = "/api/v0.1/torrents?" + encodeQueryData({ const reqURL = "/api/v0.1/torrents?" + encodeQueryData({
query : query, query: query,
epoch : epoch, epoch: epoch,
lastID : lastID, lastID: lastID,
lastOrderedValue: lastOrderedValue, lastOrderedValue: lastOrderedValue,
orderBy : orderBy, orderBy: orderBy,
ascending : ascending ascending: ascending
}); });
console.log("reqURL", reqURL); console.log("reqURL", reqURL);
let req = new XMLHttpRequest(); let req = new XMLHttpRequest();
req.onreadystatechange = function() { req.onreadystatechange = function () {
if (req.readyState !== 4) if (req.readyState !== 4)
return; return;
@ -99,7 +99,7 @@ function load() {
} }
const last = torrents[torrents.length - 1]; const last = torrents[torrents.length - 1];
lastID = last.id; lastID = last.id;
lastOrderedValue = orderedValue(last); lastOrderedValue = orderedValue(last);
for (let t of torrents) { for (let t of torrents) {
@ -108,6 +108,11 @@ function load() {
ul.innerHTML += Mustache.render(template, t); ul.innerHTML += Mustache.render(template, t);
} }
if (torrents.length < 20) {
button.textContent = "No More Results";
button.setAttribute("disabled", "");
}
}; };
req.open("GET", reqURL); req.open("GET", reqURL);