From 684794e961def3ec5a737ce89922dfb4984ef91d Mon Sep 17 00:00:00 2001 From: Mario Zechner Date: Mon, 29 May 2023 12:31:14 +0200 Subject: [PATCH] Fix filtering in aktionen, add billiger.html/.js to show all products that are cheaper than their previous price. --- site/aktionen.js | 1 + site/billiger.html | 25 ++++++++++++++++ site/billiger.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 site/billiger.html create mode 100644 site/billiger.js diff --git a/site/aktionen.js b/site/aktionen.js index 93bd01c..c580925 100644 --- a/site/aktionen.js +++ b/site/aktionen.js @@ -30,6 +30,7 @@ async function load() { document.querySelector("#filters-changes").querySelectorAll("input").forEach(input => { input.addEventListener("change", () => showResults(items, currentDate())); }); + document.querySelector("#filter").addEventListener("input", () => showResults(items, currentDate())); showResults(items, currentDate()); } diff --git a/site/billiger.html b/site/billiger.html new file mode 100644 index 0000000..5dfb75a --- /dev/null +++ b/site/billiger.html @@ -0,0 +1,25 @@ + + + + + + + + Heisse Preise + + + + +
+

Billiger seit letzter Preisänderung

+
+
+ +
+
+
+ + + + + \ No newline at end of file diff --git a/site/billiger.js b/site/billiger.js new file mode 100644 index 0000000..88f10c1 --- /dev/null +++ b/site/billiger.js @@ -0,0 +1,71 @@ +let changeDates = []; +let items = []; + +async function load() { + const today = currentDate(); + items = await loadItems(); + items.sort((a, b) => { + if (a.store < b.store) { + return -1; + } else if (a.store > b.store) { + return 1; + } + + if (a.name < b.name) { + return -1; + } else if (a.name > b.name) { + return 1; + } + + return 0; + }); + + const filtersStore = document.querySelector("#filters-store"); + filtersStore.innerHTML = STORE_KEYS.map(store => ``).join(" "); + filtersStore.querySelectorAll("input").forEach(input => { + input.addEventListener("change", () => showResults(items, currentDate())); + }); + document.querySelector("#filter").addEventListener("input", () => showResults(items, currentDate())); + showResults(items, currentDate()); +} + +function showResults(items, today) { + const storeCheckboxes = STORE_KEYS.map(store => document.querySelector(`#${store}`)); + const checkedStores = STORE_KEYS.filter((store, i) => storeCheckboxes[i].checked) + let changedItems = []; + for (item of items) { + if (item.priceHistory.length < 2) continue; + if (!checkedStores.includes(item.store)) continue; + + if (item.priceHistory[0].price < item.priceHistory[1].price && item.priceHistory[1].date.indexOf("2020") != 0) + changedItems.push(item); + } + + const query = document.querySelector("#filter").value.trim(); + const total = changedItems.length; + if (query.length >= 3) changedItems = searchItems(changedItems, document.querySelector("#filter").value, checkedStores, false, 0, 10000, false, false); + document.querySelector("#numresults").innerText = "Resultate: " + changedItems.length + (total > changedItems.length ? " / " + total : ""); + + const table = document.querySelector("#result"); + table.innerHTML = ""; + const header = dom("thead", ` + KetteNameMengePreis 📈 + `) + const showHideAll = header.querySelectorAll('th:nth-child(4)')[0]; + showHideAll.style["cursor"] = "pointer"; + showHideAll.showAll = true; + showHideAll.addEventListener("click", () => { + table.querySelectorAll(".priceinfo").forEach(el => showHideAll.showAll ? el.classList.remove("hide") : el.classList.add("hide")); + showHideAll.showAll = !showHideAll.showAll; + }) + + table.appendChild(header); + + for (item of changedItems) { + item = JSON.parse(JSON.stringify(item)); + const itemDom = itemToDOM(item); + table.appendChild(itemDom); + } +} + +load(); \ No newline at end of file