heissepreise/site/changes.js

54 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-05-17 16:17:45 +02:00
let changeDates = [];
let items = [];
2023-05-16 15:56:46 +02:00
async function load() {
2023-05-17 16:17:45 +02:00
const today = currentDate();
const response = await fetch("api/index")
items = await response.json();
2023-05-16 15:56:46 +02:00
2023-05-17 16:17:45 +02:00
const dates = {};
for (item of items) {
for (price of item.priceHistory) {
dates[price.date] = dates[price.date] ? dates[price.date] + 1 : 1;
}
}
const dateNames = Object.keys(dates).sort((a, b) => b.localeCompare(a));
const dateSelection = document.querySelector("#dates");
for (dateName of dateNames) {
const option = dom("option", dateName + " (" + dates[dateName] + ")");
option.setAttribute("value", dateName);
dateSelection.appendChild(option);
}
showResults(items, dateNames[0]);
dateSelection.addEventListener("change", () => {
showResults(items, dateSelection.value);
})
}
function showResults(items, today) {
const changedItems = [];
for (item of items) {
if (item.priceHistory.length < 2) continue;
for (let i = 0; i < item.priceHistory.length; i++) {
if (item.priceHistory[i].date == today && i + 1 < item.priceHistory.length) {
changedItems.push(item);
2023-05-17 16:17:45 +02:00
}
}
}
2023-05-16 15:56:46 +02:00
const table = document.querySelector("#result");
2023-05-17 16:17:45 +02:00
table.innerHTML = "";
2023-05-16 15:56:46 +02:00
table.appendChild(dom("tr", `
<th>Kette</th><th>Name</th><th>Menge</th><th>Preis</th>
2023-05-16 15:56:46 +02:00
`));
2023-05-17 16:17:45 +02:00
for (item of changedItems) {
table.appendChild(itemToDOM(item));
2023-05-16 15:56:46 +02:00
}
}
load();