From e190d3b84aa5edcf5e03fa4338879526927849fb Mon Sep 17 00:00:00 2001 From: Elias Schriefer Date: Mon, 8 Jul 2024 22:09:02 +0200 Subject: [PATCH] Fix API base URL + phase 2 of removing `/search` --- src/main/resources/static/add.js | 9 +++++---- src/main/resources/static/constants.js | 4 ++-- src/main/resources/static/dataset.js | 8 ++++---- src/main/resources/static/details.js | 9 +++++---- src/main/resources/static/main.js | 12 ++++++------ 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main/resources/static/add.js b/src/main/resources/static/add.js index 7adfb86..b167536 100644 --- a/src/main/resources/static/add.js +++ b/src/main/resources/static/add.js @@ -1,4 +1,5 @@ import Dataset from "./dataset.js"; +import { CATEGORIES_ENDPOINT, DATASETS_ENDPOINT } from "./constants.js"; const form = document.forms[0]; const { @@ -69,7 +70,7 @@ changeCategoryBtn.addEventListener("click", e => { validationListener(); }); -let categoriesResponse = await fetch(`${location.origin}/api/v1/categories`); +let categoriesResponse = await fetch(CATEGORIES_ENDPOINT); let categories = []; if (!categoriesResponse.ok) { console.warn("Could not load categories!"); @@ -102,7 +103,7 @@ form.addEventListener("submit", async e => { if (!categories.map(c => c.name).includes(newCategoryName)) { // Try to add the new category - const newCategoryResponse = await fetch(`/api/v1/categories`, { + const newCategoryResponse = await fetch(CATEGORIES_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json;charset=utf-8" }, body: JSON.stringify({ name: newCategoryName }), @@ -139,7 +140,7 @@ form.addEventListener("submit", async e => { // Don't allow several requests to be sent at the same time addBtn.disabled = true; - let response = await fetch("/api/v1/datasets", { + let response = await fetch(DATASETS_ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json;charset=utf-8" }, body: JSON.stringify(newContent), @@ -150,7 +151,7 @@ form.addEventListener("submit", async e => { dataset.storageSetKey("created-locally", true); if (response.ok) { - location.assign("/"); + location.assign(".."); } else { addBtn.disabled = !form.checkValidity(); } diff --git a/src/main/resources/static/constants.js b/src/main/resources/static/constants.js index ed33038..a395864 100644 --- a/src/main/resources/static/constants.js +++ b/src/main/resources/static/constants.js @@ -1,2 +1,2 @@ -export const DATASET_ENDPOINT = "/api/v1/datasets"; -export const getBaseURL = () => location.origin; +export const DATASETS_ENDPOINT = "api/v1/datasets"; +export const CATEGORIES_ENDPOINT = "api/v1/categories"; diff --git a/src/main/resources/static/dataset.js b/src/main/resources/static/dataset.js index 2bdfc47..0cb3f55 100644 --- a/src/main/resources/static/dataset.js +++ b/src/main/resources/static/dataset.js @@ -1,4 +1,4 @@ -import { DATASET_ENDPOINT, getBaseURL } from "./constants.js"; +import { DATASETS_ENDPOINT } from "./constants.js"; export default class Dataset { static #datasets = new Map(); @@ -124,7 +124,7 @@ export default class Dataset { datasetContainer.dataset.id = this.#id; datasetContainer.addEventListener("click", event => { if (!event.target.classList.contains("btn")) { - let detailsPage = new URL("/details.html", location.origin); + let detailsPage = new URL("details.html", location.href); detailsPage.searchParams.append("id", this.#id); window.location.href = detailsPage.toString(); } @@ -182,8 +182,8 @@ export default class Dataset { async vote(up = true) { const fetchURL = new URL( - `${DATASET_ENDPOINT}/id/${this.#id}/${up ? "up" : "down"}vote`, - getBaseURL(), + `${DATASETS_ENDPOINT}/id/${this.#id}/${up ? "up" : "down"}vote`, + location.href, ); const response = await fetch(fetchURL, { diff --git a/src/main/resources/static/details.js b/src/main/resources/static/details.js index 9b775e6..edbe5a6 100644 --- a/src/main/resources/static/details.js +++ b/src/main/resources/static/details.js @@ -1,3 +1,4 @@ +import { DATASETS_ENDPOINT } from "./constants.js"; import Dataset from "./dataset.js"; const mainPage = document.getElementById("details"); @@ -22,7 +23,7 @@ let currentRating = 0; const currentLocation = new URL(location.href); if (currentLocation.searchParams.has("id")) { const id = currentLocation.searchParams.get("id"); - const response = await fetch(`/api/v1/datasets/id/${id}`); + const response = await fetch(`${DATASETS_ENDPOINT}/id/${id}`); if (response.ok) { const data = await response.json(); @@ -72,7 +73,7 @@ backButton.addEventListener("click", () => { deleteButton.addEventListener("click", async () => { if (dataset != null) { const response = await fetch( - `/api/v1/datasets/id/${dataset.id}`, + `${DATASETS_ENDPOINT}/id/${dataset.id}`, { method: 'DELETE' } ); @@ -99,13 +100,13 @@ rating.addEventListener("mouseleave", () => { rating.addEventListener("click", async () => { if (!dataset.storageGetKey("is-rated", false)) { const starsResponse = await fetch( - `/api/v1/datasets/id/${dataset.id}/stars?stars=${currentRating}`, + `${DATASETS_ENDPOINT}/id/${dataset.id}/stars?stars=${currentRating}`, { method: 'PUT' } ); if (starsResponse.ok) { dataset.storageSetKey("is-rated", true); - const response = await fetch(`/api/v1/datasets/id/${dataset.id}`); + const response = await fetch(`${DATASETS_ENDPOINT}/id/${dataset.id}`); const data = await response.json(); dataset = new Dataset(data); diff --git a/src/main/resources/static/main.js b/src/main/resources/static/main.js index a48586d..b6db4e1 100644 --- a/src/main/resources/static/main.js +++ b/src/main/resources/static/main.js @@ -1,4 +1,4 @@ -import { DATASET_ENDPOINT, getBaseURL } from "./constants.js" +import { DATASETS_ENDPOINT, CATEGORIES_ENDPOINT } from "./constants.js" import { fetchQuery } from "./contentUtility.js"; import Dataset from "./dataset.js"; @@ -25,7 +25,7 @@ export let searchBarTimeout; const searchDelay = 500; // Event listeners -addButton.addEventListener("click", () => location.assign("/add.html")); +addButton.addEventListener("click", () => location.assign("add.html")); filterButton.addEventListener("change", () => fetchQuery(createQuery(), true)); filterButton.addEventListener("click", () => fetchCategories()); searchButton.addEventListener("click", () => fetchQuery(createQuery(), true)); @@ -83,7 +83,7 @@ function getSortQuery() { // creates query for the whole toolbar, so that searching, sorting and filtering are always combined function createQuery() { updateSections(); - let queryURL = new URL(DATASET_ENDPOINT + "/search", getBaseURL()); + let queryURL = new URL(DATASETS_ENDPOINT, location.href); queryURL.searchParams.append("search", getSearchQuery()); let filterQuery = getFilterQuery(); @@ -115,7 +115,7 @@ function updateSections() { // fetches the further categories used in the filter function function fetchCategories() { - const fetchURL = new URL("api/v1/categories", getBaseURL()); + const fetchURL = new URL(CATEGORIES_ENDPOINT, location.href); fetch(fetchURL) .then(resp => resp.json()) .then((data) => { @@ -133,7 +133,7 @@ function fetchCategories() { // fetches entries for the initial page async function fetchInitialEntries() { - let recentsQueryURL = new URL(DATASET_ENDPOINT + "/search", getBaseURL()); + let recentsQueryURL = new URL(DATASETS_ENDPOINT, location.href); recentsQueryURL.searchParams.append("sort", "date"); recentsQueryURL.searchParams.append("direction", "desc"); recentsQueryURL.searchParams.append("size", "6"); @@ -146,7 +146,7 @@ async function fetchInitialEntries() { document.querySelector("#recents .datasets").appendChild(recentDataset.createDatasetHTMLElement()); } - let topVotedQueryURL = new URL(DATASET_ENDPOINT + "/search", getBaseURL()); + let topVotedQueryURL = new URL(DATASETS_ENDPOINT, location.href); topVotedQueryURL.searchParams.append("sort", "upvotes"); topVotedQueryURL.searchParams.append("direction", "desc"); topVotedQueryURL.searchParams.append("size", "1");