added paging functionality
This commit is contained in:
parent
79b851a4a7
commit
1174f03d42
@ -1,22 +1,28 @@
|
|||||||
import {searchBarTimeout, searchSection} from "./main.js"
|
import {searchBarTimeout, searchSection, lastQuery} from "./main.js"
|
||||||
import Dataset from "./dataset.js"
|
import Dataset from "./dataset.js"
|
||||||
|
|
||||||
export function fetchQuery(fetchString) {
|
export function fetchQuery(fetchString, clearResults) {
|
||||||
clearTimeout(searchBarTimeout);
|
clearTimeout(searchBarTimeout);
|
||||||
fetch(fetchString)
|
fetch(fetchString)
|
||||||
.then(resp => resp.json())
|
.then(resp => resp.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
parseContent(data.content);
|
parseContent(data.content, clearResults);
|
||||||
|
lastQuery.totalPages = data.totalPages;
|
||||||
|
if (clearResults) {
|
||||||
|
lastQuery.currentPage = 0;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseContent(content) {
|
function parseContent(content, clearResults) {
|
||||||
if (content.length === 0) {
|
if (content.length === 0) {
|
||||||
searchSection.querySelector("#nothing-found ").classList.remove("hidden");
|
searchSection.querySelector("#nothing-found ").classList.remove("hidden");
|
||||||
} else {
|
} else {
|
||||||
searchSection.querySelector("#nothing-found").classList.add("hidden");
|
searchSection.querySelector("#nothing-found").classList.add("hidden");
|
||||||
const datasets = content.map(dataset => new Dataset(dataset));
|
const datasets = content.map(dataset => new Dataset(dataset));
|
||||||
Array.from(searchSection.querySelectorAll(".datasets .dataset")).forEach(e => e.remove());
|
if (clearResults) {
|
||||||
|
Array.from(searchSection.querySelectorAll(".datasets .dataset")).forEach(e => e.remove());
|
||||||
|
}
|
||||||
for (const dataset of datasets) {
|
for (const dataset of datasets) {
|
||||||
searchSection.querySelector(".datasets").appendChild(dataset.createDatasetHTMLElement());
|
searchSection.querySelector(".datasets").appendChild(dataset.createDatasetHTMLElement());
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import Dataset from "./dataset.js";
|
|||||||
const apiEndpoint = "/api/v1/datasets";
|
const apiEndpoint = "/api/v1/datasets";
|
||||||
const baseURL = location.origin;
|
const baseURL = location.origin;
|
||||||
const defaultPagingValue = 20;
|
const defaultPagingValue = 20;
|
||||||
const lastQuery = {
|
export const lastQuery = {
|
||||||
url: "",
|
url: "",
|
||||||
totalPages: 0,
|
totalPages: 0,
|
||||||
currentPage: 0
|
currentPage: 0
|
||||||
@ -35,12 +35,12 @@ addButton.addEventListener("click", () => {
|
|||||||
filterButton.addEventListener("change", () => {
|
filterButton.addEventListener("change", () => {
|
||||||
const filterString = filterButton.value;
|
const filterString = filterButton.value;
|
||||||
if (filterString !== filterButton.querySelector("#default-filter").value) {
|
if (filterString !== filterButton.querySelector("#default-filter").value) {
|
||||||
fetchQuery(createQuery());
|
fetchQuery(createQuery(), true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
searchButton.addEventListener("click", () => {
|
searchButton.addEventListener("click", () => {
|
||||||
fetchQuery(createQuery());
|
fetchQuery(createQuery(), true);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -48,18 +48,18 @@ searchBar.addEventListener("input", () => {
|
|||||||
updateSections();
|
updateSections();
|
||||||
clearTimeout(searchBarTimeout);
|
clearTimeout(searchBarTimeout);
|
||||||
searchBarTimeout = setTimeout(() => {
|
searchBarTimeout = setTimeout(() => {
|
||||||
fetchQuery(createQuery());
|
fetchQuery(createQuery(), true);
|
||||||
}, searchDelay);
|
}, searchDelay);
|
||||||
});
|
});
|
||||||
|
|
||||||
searchBar.addEventListener('keypress', function (e) {
|
searchBar.addEventListener('keypress', function (e) {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
fetchQuery(createQuery());
|
fetchQuery(createQuery(), true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
sortButton.addEventListener("change", () => {
|
sortButton.addEventListener("change", () => {
|
||||||
fetchQuery(createQuery());
|
fetchQuery(createQuery(), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
resetButton.addEventListener("click", () => {
|
resetButton.addEventListener("click", () => {
|
||||||
@ -79,7 +79,7 @@ for (const upvoteButton of upvoteButtons) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Consider moving this to datasets.js completely
|
// Consider moving this to datasets.js completely
|
||||||
const downvoteButtonClickListener = e => {
|
const downvoteButtonClickListener = e => {
|
||||||
const entryID = e.target.parentElement.parentElement.dataset.id;
|
const entryID = e.target.parentElement.parentElement.dataset.id;
|
||||||
vote(entryID, false);
|
vote(entryID, false);
|
||||||
};
|
};
|
||||||
@ -93,7 +93,7 @@ function navigateToAdd() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getFilterQuery() {
|
function getFilterQuery() {
|
||||||
let filterString= filterButton.value.toUpperCase();
|
let filterString = filterButton.value.toUpperCase();
|
||||||
if (filterString === "NONE") {
|
if (filterString === "NONE") {
|
||||||
return ["type", "%"]
|
return ["type", "%"]
|
||||||
} else if (document.querySelector('#filter-btn option:checked').parentElement.label === "Standard categories") {
|
} else if (document.querySelector('#filter-btn option:checked').parentElement.label === "Standard categories") {
|
||||||
@ -141,13 +141,14 @@ export function vote(entryID, up) {
|
|||||||
method: "PUT",
|
method: "PUT",
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
}})
|
}
|
||||||
.then(resp => resp.json())
|
})
|
||||||
.then((data) => {
|
.then(resp => resp.json())
|
||||||
console.log(data.upvotes); // TODO: remove, check einbauen: data.id === entryID?
|
.then((data) => {
|
||||||
let dataset = document.querySelector('[data-id= ' + CSS.escape(entryID) + ']')
|
console.log(data.upvotes); // TODO: remove, check einbauen: data.id === entryID?
|
||||||
dataset.querySelector("span").innerText = data.upvotes;
|
let dataset = document.querySelector('[data-id= ' + CSS.escape(entryID) + ']')
|
||||||
});
|
dataset.querySelector("span").innerText = data.upvotes;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function incrementPageCount() {
|
function incrementPageCount() {
|
||||||
@ -173,7 +174,7 @@ function updateSections() {
|
|||||||
// fetches the further categories used in the filter function
|
// fetches the further categories used in the filter function
|
||||||
function fetchCategories() {
|
function fetchCategories() {
|
||||||
const fetchURL = new URL(
|
const fetchURL = new URL(
|
||||||
"api/v1/categories" , baseURL);
|
"api/v1/categories", baseURL);
|
||||||
fetch(fetchURL)
|
fetch(fetchURL)
|
||||||
.then(resp => resp.json())
|
.then(resp => resp.json())
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
@ -217,6 +218,21 @@ window.onload = function () {
|
|||||||
fetchInitialEntries();
|
fetchInitialEntries();
|
||||||
updateSections();
|
updateSections();
|
||||||
if (searchBar.value !== "") {
|
if (searchBar.value !== "") {
|
||||||
fetchQuery(createQuery());
|
fetchQuery(createQuery(), true);
|
||||||
|
}
|
||||||
|
let observer = new IntersectionObserver(() => {
|
||||||
|
if (!searchSection.classList.contains("hidden")) {
|
||||||
|
fetchPagingResults();
|
||||||
|
}
|
||||||
|
}, {root: null, rootMargin: "0px", threshold: .9});
|
||||||
|
observer.observe(document.getElementById("observable"));
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchPagingResults() {
|
||||||
|
lastQuery.currentPage++
|
||||||
|
if (lastQuery.currentPage < lastQuery.totalPages) {
|
||||||
|
let pagingQuery = new URL(createQuery());
|
||||||
|
pagingQuery.searchParams.append("page", lastQuery.currentPage.toString(10));
|
||||||
|
fetchQuery(pagingQuery, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,6 +78,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<ul class="datasets">
|
<ul class="datasets">
|
||||||
</ul>
|
</ul>
|
||||||
|
<div id="observable" style="height: 2rem"></div>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
Reference in New Issue
Block a user