diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-06-01 21:13:43 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-06-01 21:13:43 +0200 |
| commit | 9383a2fb09ffb60cfe63683106945bd688affa59 (patch) | |
| tree | 65b3f4b48841583e355887db5de5a16e7005fc87 /src/wwwroot/scripts/cart-core.js | |
| download | vinjesvingenhandel.no-9383a2fb09ffb60cfe63683106945bd688affa59.tar.xz vinjesvingenhandel.no-9383a2fb09ffb60cfe63683106945bd688affa59.zip | |
feat: Initial commit after clean slate
Diffstat (limited to 'src/wwwroot/scripts/cart-core.js')
| -rw-r--r-- | src/wwwroot/scripts/cart-core.js | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/src/wwwroot/scripts/cart-core.js b/src/wwwroot/scripts/cart-core.js new file mode 100644 index 0000000..c15d0af --- /dev/null +++ b/src/wwwroot/scripts/cart-core.js @@ -0,0 +1,109 @@ +import {utilites} from "./utilities"; +import {configuration} from "./configuration"; +import {getProducts} from "./api/products-api"; + +export function setCart(data) { + return utilites.setSessionStorageJSON(configuration.storageKeys.cart, data); +} + +export function getCart() { + return utilites.getSessionStorageJSON(configuration.storageKeys.cart); +} + +export function cartDispose() { + sessionStorage.removeItem(configuration.storageKeys.cart); +} + +export function priceTotal() { + const cartData = getCart(); + if (utilites.array.isEmpty(cartData)) { + return null; + } + let total = null; + for (const product of cartData) { + total += product.price * product.count; + } + return typeof total !== "number" ? total : total.toFixed(2) + ",-"; +} + +export function priceTax() { + const cartData = getCart(); + if (utilites.array.isEmpty(cartData)) { + return null; + } + let total = null; + for (const product of cartData) { + total += product.price * product.count; + } + return typeof total !== "number" ? total : ((total) * 25 / 100).toFixed(2) + ",-"; +} + +export function updatePricesFromApi() { + return new Promise((ok, error) => { + const cartData = getCart(); + if (!cartData) { + + ok(); + return; + } + getProducts().then(res => { + if (res.ok) { + res.json().then(apiProducts => { + apiProducts = utilites.resolveReferences(apiProducts); + for (const latestProduct of apiProducts) { + const index = cartData.findIndex(c => c.id === latestProduct.id); + if (index !== -1) { + cartData[index].price = latestProduct.price; + } + if (index !== -1) { + cartData[index].readablePrice = latestProduct.price + latestProduct.readablePriceSuffix; + } + } + setCart(cartData); + ok(); + }); + } + }); + }); +} + +export function setProductCount(id, newCount) { + const cartData = getCart(); + const productIndex = cartData.findIndex(c => c.id === id); + cartData[productIndex].count = newCount; + setCart(cartData); +} + +export function removeProductFromCart(id, callback) { + const cartData = getCart(); + if (utilites.array.isEmpty(cartData)) { + return; + } + const updatedCart = utilites.array.removeItemByIdAll(cartData, {id}); + if (updatedCart.length === 0) { + cartDispose(); + } else { + setCart(updatedCart); + } + if (typeof callback === "function") { + callback(updatedCart); + } +} + +export function addOrUpdateProduct(product, callback) { + let cartData = getCart(); + if (utilites.array.isEmpty(cartData)) { + cartData = []; + } + const indexOfCurrent = cartData.length === 0 ? -1 : cartData.findIndex(c => c.id === product.id); + if (indexOfCurrent === -1 && product !== undefined) { // adding + product.count = 1; + cartData.push(product); + } else if (indexOfCurrent !== -1) { // updating + cartData[indexOfCurrent] = product; + } + setCart(cartData); + if (typeof callback === "function") { + callback(); + } +} |
