From e47edf3cf2c612e60a2c004817c3d06dae80d26c Mon Sep 17 00:00:00 2001 From: J-Klinke Date: Tue, 25 Jun 2024 09:44:01 +0200 Subject: [PATCH] improved searchBar functionality: on page reload, it now displays the query fitting to the entered string --- src/main/resources/static/main.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/resources/static/main.js b/src/main/resources/static/main.js index 9753fad..be21842 100644 --- a/src/main/resources/static/main.js +++ b/src/main/resources/static/main.js @@ -41,16 +41,7 @@ searchButton.addEventListener("click", () => { }); searchBar.addEventListener("input", () => { - if (searchBar.value === "") { - searchSection.classList.add("hidden"); - recentSection.classList.remove("hidden"); - mostLikedSection.classList.remove("hidden"); - } else { - searchSection.classList.remove("hidden"); - recentSection.classList.add("hidden"); - mostLikedSection.classList.add("hidden"); - } - + updateSections(); clearTimeout(searchBarTimeout); searchBarTimeout = setTimeout(() => { const searchString = searchBar.value; @@ -137,6 +128,21 @@ function incrementPageCount() { lastQuery.currentPage++; } -window.onload = function () { - document.getElementById("search-entry").value = ""; +function updateSections() { + if (searchBar.value === "") { + searchSection.classList.add("hidden"); + recentSection.classList.remove("hidden"); + mostLikedSection.classList.remove("hidden"); + } else { + searchSection.classList.remove("hidden"); + recentSection.classList.add("hidden"); + mostLikedSection.classList.add("hidden"); + } +} + +window.onload = function () { + updateSections(); + if (searchBar.value !== "") { + search(searchBar.value); + } }