blob: c15d0afeff82032786d288300f054eb89db08d42 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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();
}
}
|