mirror of
https://github.com/badlogic/heissepreise.git
synced 2024-06-24 09:25:53 +02:00
Merge pull request #39 from HannesOberreiter/refactor
refactor: ♻️ refactor some utils
This commit is contained in:
commit
c9cae1ec51
5
.editorconfig
Normal file
5
.editorconfig
Normal file
|
@ -0,0 +1,5 @@
|
|||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 4
|
18
site/cart.js
18
site/cart.js
|
@ -8,7 +8,7 @@ async function load() {
|
|||
let cart = null;
|
||||
const cartName = getQueryParameter("name");
|
||||
if (cartName) {
|
||||
for (c of carts) {
|
||||
for (c of shoppingCarts.carts) {
|
||||
if (c.name == cartName) {
|
||||
cart = c;
|
||||
break;
|
||||
|
@ -19,11 +19,11 @@ async function load() {
|
|||
let items = [];
|
||||
for (cartItem of cart.items) {
|
||||
const item = lookup[cartItem.id];
|
||||
if (!item) items.push(cartItem);
|
||||
if (!item) shoppingCarts.items.push(cartItem);
|
||||
else items.push(item);
|
||||
}
|
||||
cart.items = items;
|
||||
saveCarts();
|
||||
shoppingCarts.save();
|
||||
}
|
||||
|
||||
const cartDesc = getQueryParameter("cart");
|
||||
|
@ -41,13 +41,13 @@ async function load() {
|
|||
let saveButton = document.querySelector("#save");
|
||||
saveButton.classList.remove("hide");
|
||||
saveButton.addEventListener("click", () => {
|
||||
let index = carts.findIndex(c => c.name == cart.name);
|
||||
let index = shoppingCarts.carts.findIndex((c) => c.name === cart.name);
|
||||
if (index != -1) {
|
||||
if (confirm("Existierenden Warenkorb '" + cart.name + " überschreiben?")) {
|
||||
carts[index] = cart;
|
||||
shoppingCarts.carts[index] = cart;
|
||||
}
|
||||
} else {
|
||||
carts.push(importedCart);
|
||||
shoppingCarts.carts.push(importedCart);
|
||||
}
|
||||
location.href = "/cart.html?name=" + encodeURIComponent(cart.name);
|
||||
});
|
||||
|
@ -114,7 +114,7 @@ function showSearch(cart, items) {
|
|||
|
||||
addButton.addEventListener("click", () => {
|
||||
cart.items.push(item);
|
||||
saveCarts();
|
||||
shoppingCarts.save();
|
||||
showCart(cart);
|
||||
});
|
||||
|
||||
|
@ -170,7 +170,7 @@ function showCart(cart) {
|
|||
itemDom.append(showCheckbox);
|
||||
showCheckbox.addEventListener("change", () => {
|
||||
cartItem.chart = showCheckbox.checked;
|
||||
saveCarts();
|
||||
shoppingCarts.save();
|
||||
showCharts(canvasDom, cart.items);
|
||||
});
|
||||
cell.append(showCheckbox);
|
||||
|
@ -182,7 +182,7 @@ function showCart(cart) {
|
|||
itemDom.append(deleteButton);
|
||||
deleteButton.addEventListener("click", () => {
|
||||
cart.items.splice(idx, 1);
|
||||
saveCarts();
|
||||
shoppingCarts.save();
|
||||
showCart(cart)
|
||||
})
|
||||
cell.appendChild(deleteButton);
|
||||
|
|
|
@ -18,7 +18,7 @@ async function load() {
|
|||
}
|
||||
|
||||
// Update carts with latest price info
|
||||
for (cart of carts) {
|
||||
for (cart of shoppingCarts.carts) {
|
||||
const items = [];
|
||||
for (cartItem of cart.items) {
|
||||
const item = lookup[cartItem.id];
|
||||
|
@ -27,32 +27,32 @@ async function load() {
|
|||
}
|
||||
cart.items = items;
|
||||
}
|
||||
saveCarts();
|
||||
shoppingCarts.save();
|
||||
|
||||
if (carts.findIndex(cart => cart.name == "Momentum Eigenmarken Vergleich") == -1) {
|
||||
if (shoppingCarts.carts.findIndex(cart => cart.name === "Momentum Eigenmarken Vergleich") == -1) {
|
||||
response = await fetch("momentum-cart.json");
|
||||
momentumCart = await response.json();
|
||||
carts.unshift(momentumCart);
|
||||
saveCarts();
|
||||
shoppingCarts.carts.unshift(momentumCart);
|
||||
shoppingCarts.save();
|
||||
}
|
||||
|
||||
const newCartButton = document.querySelector("#newcart");
|
||||
newCartButton.addEventListener("click", () => {
|
||||
let name = prompt("Name für Warenkorb eingeben:");
|
||||
if (name.length == 0) return;
|
||||
for (cart of carts) {
|
||||
if (cart.name == name) {
|
||||
if (name.length === 0) return;
|
||||
for (cart of shoppingCarts.carts) {
|
||||
if (cart.name === name) {
|
||||
alert("Warenkorb mit Namen '" + name + "' existiert bereits");
|
||||
return;
|
||||
}
|
||||
}
|
||||
addCart(name);
|
||||
shoppingCarts.add(name);
|
||||
location.href = "/cart.html?name=" + name;
|
||||
});
|
||||
|
||||
const exportButton = document.querySelector("#export");
|
||||
exportButton.addEventListener("click", () => {
|
||||
downloadFile("carts.json", JSON.stringify(carts, null, 2));
|
||||
downloadFile("carts.json", JSON.stringify(shoppingCarts.carts, null, 2));
|
||||
});
|
||||
|
||||
const importButton = document.querySelector("#import");
|
||||
|
@ -67,25 +67,27 @@ async function load() {
|
|||
reader.onload = function (event) {
|
||||
const contents = event.target.result;
|
||||
const importedCarts = JSON.parse(contents);
|
||||
for (importedCart of importedCarts) {
|
||||
for (const importedCart of importedCarts) {
|
||||
const items = [];
|
||||
for (cartItem of cart.items) {
|
||||
for (cartItem of importedCart.items) {
|
||||
const item = lookup[cartItem.id];
|
||||
if (!item) continue;
|
||||
items.push(item);
|
||||
}
|
||||
importedCart.items = items;
|
||||
|
||||
let index = carts.findIndex(cart => cart.name == importedCart.name);
|
||||
const index = shoppingCarts.carts.findIndex(cart => cart.name === importedCart.name);
|
||||
if (index != -1) {
|
||||
if (confirm("Existierenden Warenkorb '" + importedCart.name + " überschreiben?")) {
|
||||
carts[index] = importedCart;
|
||||
console.log(shoppingCarts.carts[index]);
|
||||
shoppingCarts.carts[index] = importedCart;
|
||||
console.log(shoppingCarts.carts[index])
|
||||
}
|
||||
} else {
|
||||
carts.push(importedCart);
|
||||
shoppingCarts.carts.push(importedCart);
|
||||
}
|
||||
}
|
||||
saveCarts();
|
||||
shoppingCarts.save();
|
||||
showCarts(lookup);
|
||||
};
|
||||
reader.readAsText(file);
|
||||
|
@ -106,7 +108,7 @@ function showCarts(lookup) {
|
|||
</tr>
|
||||
`));
|
||||
|
||||
carts.forEach(cart => {
|
||||
shoppingCarts.carts.forEach(cart => {
|
||||
let oldPrice = 0;
|
||||
let currPrice = 0;
|
||||
let link = cart.name + ";"
|
||||
|
@ -145,7 +147,7 @@ function showCarts(lookup) {
|
|||
actionsDom.appendChild(deleteButton);
|
||||
|
||||
deleteButton.addEventListener("click", () => {
|
||||
removeCart(cart.name);
|
||||
shoppingCarts.remove(cart.name);
|
||||
showCarts(lookup);
|
||||
});
|
||||
}
|
||||
|
|
141
site/utils.js
141
site/utils.js
|
@ -38,32 +38,33 @@ const stores = {
|
|||
|
||||
const STORE_KEYS = Object.keys(stores);
|
||||
const BUDGET_BRANDS = [].concat(
|
||||
...Object
|
||||
.values(stores)
|
||||
.map(store => store.budgetBrands)
|
||||
...Object.values(stores).map((store) => store.budgetBrands)
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Returns the current date in ISO format
|
||||
* @returns {string} ISO date string in format YYYY-MM-DD
|
||||
*/
|
||||
function currentDate() {
|
||||
const currentDate = new Date();
|
||||
const year = currentDate.getFullYear();
|
||||
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(currentDate.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
return new Date().toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Gets the query parameter from the URL
|
||||
* @param {string} name Name of the query parameter
|
||||
* @returns {string | null} Value of the query parameter or null if not found
|
||||
*/
|
||||
function getQueryParameter(name) {
|
||||
const url = window.location.href;
|
||||
const queryString = url.substring(url.indexOf('?') + 1);
|
||||
const parameters = queryString.split('&');
|
||||
|
||||
for (var i = 0; i < parameters.length; i++) {
|
||||
const parameter = parameters[i].split('=');
|
||||
const paramName = decodeURIComponent(parameter[0]);
|
||||
if (paramName == name) return decodeURIComponent(parameter[1]);
|
||||
}
|
||||
return null;
|
||||
const url = new URL(window.location.href);
|
||||
return url.searchParams.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts a string to a number
|
||||
* @param {string} value String to convert
|
||||
* @param {number} defaultValue Default value if conversion fails
|
||||
* @returns {number} Converted number or default value
|
||||
*/
|
||||
function toNumber(value, defaultValue) {
|
||||
try {
|
||||
return Number.parseFloat(value);
|
||||
|
@ -72,22 +73,29 @@ function toNumber(value, defaultValue) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Create dom element from html string and add inner html via string template
|
||||
* @param {string} el Element type
|
||||
* @param {string} html Inner html
|
||||
* @returns {HTMLElement} DOM element
|
||||
*/
|
||||
function dom(el, html) {
|
||||
let element = document.createElement(el);
|
||||
const element = document.createElement(el);
|
||||
element.innerHTML = html;
|
||||
return element;
|
||||
}
|
||||
|
||||
async function loadItems() {
|
||||
const response = await fetch("latest-canonical.json")
|
||||
const response = await fetch("latest-canonical.json");
|
||||
const items = await response.json();
|
||||
|
||||
for (item of items) {
|
||||
for (const item of items) {
|
||||
item.search = item.name + " " + item.unit;
|
||||
item.search = item.search.toLowerCase().replace(",", ".");
|
||||
|
||||
item.numPrices = item.priceHistory.length;
|
||||
item.priceOldest = item.priceHistory[item.priceHistory.length - 1].price;
|
||||
item.priceOldest =
|
||||
item.priceHistory[item.priceHistory.length - 1].price;
|
||||
item.dateOldest = item.priceHistory[item.priceHistory.length - 1].date;
|
||||
item.date = item.priceHistory[0].date;
|
||||
let highestPriceBefore = -1;
|
||||
|
@ -101,37 +109,66 @@ async function loadItems() {
|
|||
return items;
|
||||
}
|
||||
|
||||
let carts = [];
|
||||
loadCarts();
|
||||
|
||||
function loadCarts() {
|
||||
let val = localStorage.getItem("carts");
|
||||
carts = val ? JSON.parse(val) : [];
|
||||
}
|
||||
|
||||
function saveCarts() {
|
||||
localStorage.setItem("carts", JSON.stringify(carts, null, 2));
|
||||
}
|
||||
|
||||
function hasCart(name) {
|
||||
for (cart of carts) {
|
||||
if (cart.name = name) return true;
|
||||
/**
|
||||
* @description Class for managing the shopping carts, which are stored in local storage
|
||||
*/
|
||||
class ShoppingCarts {
|
||||
constructor() {
|
||||
this.carts = [];
|
||||
this.load();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Load the shopping carts from local storage into carts array
|
||||
*/
|
||||
load() {
|
||||
const val = localStorage.getItem("carts");
|
||||
this.carts = val ? JSON.parse(val) : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Save the shopping carts to local storage, with key "carts"
|
||||
*/
|
||||
save() {
|
||||
localStorage.setItem("carts", JSON.stringify(this.carts, null, 2));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Check if the shopping carts contains a cart with the given name
|
||||
* @param {string} name Name of the shopping cart to check
|
||||
*/
|
||||
has(name) {
|
||||
for (const cart of this.carts) {
|
||||
if (cart.name === name) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Add new shopping card to array and save new carts array to local storage
|
||||
* @param {string} name Name of the shopping cart to add
|
||||
*/
|
||||
add(name) {
|
||||
this.carts.push({
|
||||
name: name,
|
||||
items: [],
|
||||
});
|
||||
this.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Remove shopping cart from carts array based on name and save updated array to local storage
|
||||
* @param {string} name Name of the shopping cart to remove
|
||||
*/
|
||||
remove(name) {
|
||||
this.carts = this.carts.filter((cart) => cart.name !== name);
|
||||
this.save();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function addCart(name) {
|
||||
carts.push({
|
||||
name: name,
|
||||
items: []
|
||||
});
|
||||
saveCarts();
|
||||
}
|
||||
const shoppingCarts = new ShoppingCarts();
|
||||
shoppingCarts.load();
|
||||
|
||||
function removeCart(name) {
|
||||
carts = carts.filter(cart => cart.name != name);
|
||||
saveCarts();
|
||||
}
|
||||
|
||||
function itemToStoreLink(item) {
|
||||
if (STORE_KEYS.includes(item.store)) {
|
||||
|
@ -207,10 +244,10 @@ function searchItems(items, query, checkedStores, budgetBrands, minPrice, maxPri
|
|||
const tokens = query.split(/\s+/).map(token => token.toLowerCase().replace(",", "."));
|
||||
|
||||
let hits = [];
|
||||
for (item of items) {
|
||||
for (const item of items) {
|
||||
let allFound = true;
|
||||
for (token of tokens) {
|
||||
if (token.length == 0) continue;
|
||||
for (const token of tokens) {
|
||||
if (token.length === 0) continue;
|
||||
const index = item.search.indexOf(token);
|
||||
if (index < 0) {
|
||||
allFound = false;
|
||||
|
|
Loading…
Reference in New Issue
Block a user