aboutsummaryrefslogtreecommitdiffstats
path: root/src/wwwroot/scripts/cart-core.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/wwwroot/scripts/cart-core.js')
-rw-r--r--src/wwwroot/scripts/cart-core.js109
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();
+ }
+}