diff --git a/src/main/resources/static/contentUtility.js b/src/main/resources/static/contentUtility.js index 7555315..ff48f52 100644 --- a/src/main/resources/static/contentUtility.js +++ b/src/main/resources/static/contentUtility.js @@ -1,6 +1,6 @@ import {searchBarTimeout, searchSection, lastQuery} from "./main.js" import Dataset from "./dataset.js" - +// TODO consider renaming this to "searchUtility.js" export function fetchQuery(fetchString, clearResults) { clearTimeout(searchBarTimeout); fetch(fetchString) @@ -24,8 +24,8 @@ function parseContent(content, clearResults) { Array.from(searchSection.querySelectorAll(".datasets .dataset")).forEach(e => e.remove()); } for (const dataset of datasets) { - searchSection.querySelector(".datasets").appendChild(dataset.createDatasetHTMLElement()); + searchSection.querySelector(".datasets") + .appendChild(dataset.createDatasetHTMLElement()); } } - } diff --git a/src/main/resources/static/dataset.js b/src/main/resources/static/dataset.js index 27245e5..29170a8 100644 --- a/src/main/resources/static/dataset.js +++ b/src/main/resources/static/dataset.js @@ -36,6 +36,15 @@ export default class Dataset { clone.querySelector("h3").innerText = this.#title; clone.querySelector("p").innerText = this.#description; clone.querySelector("span").innerText = this.#upvotes; + let votedIDs = window.localStorage; + // depending on whether the button has been up/downvoted, its according button get disabled and hidden + if (votedIDs.getItem(this.#id)) { + let votedButton = clone.querySelector(votedIDs.getItem(this.#id)? ".upvote-btn":".downvote-btn"); + votedButton.classList.add("isVoted"); + votedButton.disabled = true; + let notVotedButton = clone.querySelector(votedIDs.getItem(this.#id)? ".downvote-btn":".upvote-btn"); + notVotedButton.style.visibility = "hidden"; + } // Event Listeners clone.querySelector(".upvote-btn").addEventListener("click", () => { @@ -48,4 +57,8 @@ export default class Dataset { return clone; } + + getID() { + return this.#id; + } } diff --git a/src/main/resources/static/main.css b/src/main/resources/static/main.css index c62d21e..ab5e5bf 100644 --- a/src/main/resources/static/main.css +++ b/src/main/resources/static/main.css @@ -81,6 +81,7 @@ header { .hidden { display: none; } + #search-entry:focus-visible { outline: none; } @@ -117,6 +118,7 @@ header { align-items: center; gap: .5em; } + /* Buttons */ .upvote-btn, .downvote-btn, #search-btn, #filter-btn, #sort-btn, #reset-tools-btn { background: var(--icon-url) no-repeat; @@ -143,6 +145,7 @@ header { --icon-url: url(looking-glass.svg); --icon-size: 1rem; } + #filter-btn { --icon-url: url(filter.svg); --icon-size: 1rem; @@ -158,6 +161,10 @@ header { --icon-size: 1rem; } +.isVoted { + filter: brightness(1.75); +} + .divider { width: .05rem; height: 1rem; diff --git a/src/main/resources/static/main.js b/src/main/resources/static/main.js index fb8700d..32caf41 100644 --- a/src/main/resources/static/main.js +++ b/src/main/resources/static/main.js @@ -5,10 +5,10 @@ const apiEndpoint = "/api/v1/datasets"; const baseURL = location.origin; const defaultPagingValue = 20; export const lastQuery = { - url: "", totalPages: 0, currentPage: 0 }; +const votedIDs = window.localStorage; // definition of all buttons & sections const addButton = document.getElementById("add-btn"); @@ -69,7 +69,7 @@ resetButton.addEventListener("click", () => { updateSections(); }); -// Consider moving this to datasets.js completely +// Consider moving this to datasets.js completely // TODO: we dont need them, do we? there in dataset.js const upvoteButtonClickListener = e => { const entryID = e.target.parentElement.parentElement.dataset.id; vote(entryID, true); @@ -78,7 +78,7 @@ for (const upvoteButton of upvoteButtons) { upvoteButton.addEventListener("click", upvoteButtonClickListener); } -// Consider moving this to datasets.js completely +// Consider moving this to datasets.js completely // TODO: we dont need them, do we? there in dataset.js const downvoteButtonClickListener = e => { const entryID = e.target.parentElement.parentElement.dataset.id; vote(entryID, false); @@ -89,7 +89,7 @@ for (const downvoteButton of downvoteButtons) { // functions of the main page function navigateToAdd() { - window.location.href = "/add"; + window.location.href = "/add"; //TODO: move to EventListner? } function getFilterQuery() { @@ -146,15 +146,19 @@ export function vote(entryID, up) { .then(resp => resp.json()) .then((data) => { console.log(data.upvotes); // TODO: remove, check einbauen: data.id === entryID? - let dataset = document.querySelector('[data-id= ' + CSS.escape(entryID) + ']') - dataset.querySelector("span").innerText = data.upvotes; + let dataSets = document.querySelectorAll('[data-id= ' + CSS.escape(entryID) + ']'); + for (const dataSetElement of dataSets) { + dataSetElement.querySelector("span").innerText = data.upvotes; + let votedButton = dataSetElement.querySelector(up? ".upvote-btn":".downvote-btn"); + votedButton.classList.add("isVoted"); + votedButton.disabled = true; + let notVotedButton = dataSetElement.querySelector(up? ".downvote-btn":".upvote-btn"); + notVotedButton.style.visibility = "hidden"; + votedIDs.setItem(dataSetElement.getAttribute("data-id"), up); + } }); } -function incrementPageCount() { - lastQuery.currentPage++; -} - // updates the page display. If no query is present, the initial page is shown, otherwise the search results. function updateSections() { if (searchBar.value === "" && sortButton.value === sortButton.querySelector("#default-sort").value @@ -197,7 +201,8 @@ function fetchInitialEntries() { .then((data) => { const datasets = data.content.map(dataset => new Dataset(dataset)); for (const dataset of datasets) { - document.querySelector("#recents .datasets").appendChild(dataset.createDatasetHTMLElement()); + document.querySelector("#recents .datasets") + .appendChild(dataset.createDatasetHTMLElement()); } }); @@ -208,8 +213,9 @@ function fetchInitialEntries() { fetch(topVotedQueryURL) .then(resp => resp.json()) .then((data) => { + let dataset = new Dataset(data.content[0]); document.querySelector("#top .datasets") - .appendChild(new Dataset(data.content[0]).createDatasetHTMLElement()); + .appendChild(dataset.createDatasetHTMLElement()); }); } diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 34982f9..5f8cbdd 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -5,7 +5,7 @@ DataDash - +