DataDash/src/main/resources/static/main.js
Erik Foris e400b7e9ce feat: Update URL construction for add page navigation
The code changes modify the `PageController` class to update the URL construction for navigating to the add page. The `getAddPage()` method now maps to the `/add` endpoint instead of the previous `add` endpoint. This change ensures that the URL matches the correct route for the add page.

Recent commits:
- Integrate add content page into backend
- Add base URL to URL construction
- Rework URL construction

Recent repository commits:
- Integrate add content page into backend
- Merge branch 'main' into 15-make-content-adding-page-functional-js
- Merge branch '11-add-api-for-getting-home-page-data' into 'main'
- Merge branch '22-integrate-api-and-frontend' into '11-add-api-for-getting-home-page-data'
- Merge branch '11-add-api-for-getting-home-page-data' into '22-integrate-api-and-frontend'
- feature: add ability to vote on datasets
- Merge remote-tracking branch 'origin/implement-js' into 22-integrate-api-and-frontend
- Add base URL to URL construction
- Merge remote-tracking branch 'origin/implement-js' into 22-integrate-api-and-frontend
- Rework URL construction
2024-06-25 11:33:36 +02:00

129 lines
3.5 KiB
JavaScript

import { fetchQuery } from "./contentUtility.js";
const apiEndpoint = "/api/v1/datasets";
const baseURL = location.origin;
const defaultPagingValue = 20;
const lastQuery = {
url: "",
totalPages: 0,
currentPage: 0
};
// definition of all buttons
const addButton = document.getElementById("add-btn");
const filterButton = document.getElementById("filter-btn");
const searchButton = document.getElementById("search-btn");
const searchBar = document.getElementById("search-entry");
const sortButton = document.getElementById("sort-btn");
const upvoteButtons = document.getElementsByClassName("upvote-btn");
const downvoteButtons = document.getElementsByClassName("downvote-btn");
// ID of the timeout, because we need to cancel it at some point
export let searchBarTimeout;
// Event listeners
addButton.addEventListener("click", () => {
navigateToAdd();
});
filterButton.addEventListener("change", () => {
const filterString = filterButton.value;
filter(filterString);
});
searchButton.addEventListener("click", () => {
const searchString = searchBar.value;
search(searchString);
});
searchBar.addEventListener("input", () => {
clearTimeout(searchBarTimeout);
searchBarTimeout = setTimeout(() => {
const searchString = searchBar.value;
search(searchString);
}, 1000);
});
searchBar.addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
const searchString = searchBar.value;
search(searchString);
}
})
sortButton.addEventListener("change", () => {
const sortString = sortButton.value;
sort(sortString);
});
const upvoteButtonClickListener = e => {
const entryID = e.target.parentElement.parentElement.dataset.id;
vote(entryID, true);
};
for (const upvoteButton of upvoteButtons) {
upvoteButton.addEventListener("click", upvoteButtonClickListener);
}
const downvoteButtonClickListener = e => {
const entryID = e.target.parentElement.parentElement.dataset.id;
vote(entryID, false);
};
for (const downvoteButton of downvoteButtons) {
downvoteButton.addEventListener("click", downvoteButtonClickListener);
}
// functions of the main page
function navigateToAdd() {
//TODO: url to add page not yet implemented, add here
window.location.href = "/add";
}
function filter(filterString) {
filterString = filterString.toUpperCase();
let fetchURL = new URL(apiEndpoint, baseURL);
fetchURL.searchParams.append("type", filterString);
fetchURL.searchParams.append("size", defaultPagingValue);
console.log(fetchURL); // TODO: remove
fetchQuery(fetchURL);
}
function search(searchString) {
let fetchURL = new URL(apiEndpoint + "/search", baseURL);
fetchURL.searchParams.append("search", searchString.length == 0 ? "%" : searchString);
console.log(fetchURL); // TODO: remove
fetchQuery(fetchURL);
}
function sort(sortString) {
let query = sortString.toLowerCase().split(" ");
if (query[1] === "a-z" || query[1] === "↑") {
query[1] = "asc";
} else {
query[1] = "desc";
}
let fetchURL = new URL(apiEndpoint, baseURL);
fetchURL.searchParams.append("sort", query[0]);
fetchURL.searchParams.append("direction", query[1]);
console.log(fetchURL); // TODO: remove
fetchQuery(fetchURL);
}
function vote(entryID, up) {
const fetchURL = new URL(
`${apiEndpoint}/id/${entryID}/${up ? "up" : "down"}vote`,
baseURL,
);
console.log(fetchURL); // TODO: remove
fetch(fetchURL);
}
function incrementPageCount() {
lastQuery.currentPage++;
}