-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.js
More file actions
120 lines (108 loc) · 2.7 KB
/
cart.js
File metadata and controls
120 lines (108 loc) · 2.7 KB
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
110
111
112
113
114
115
116
117
118
119
120
/**
* The cart item to remove in the confirm dialog
*/
let itemToRemove;
/**
* Adds an article to the cart.
* Increases its quantity, if it already exists.
* @param articleNo - the article number of the article to add
*/
function addToCart(articleNo) {
// is this a valid shop article?
const shopItem = findItemInArticles(articleNo)
if (shopItem) {
// is article already in cart?
let cartItem = findItemInCart(articleNo)
if (!cartItem) {
cartItem = shopItem
cartItem.quantity = 0
cartItem.total = 0.0
cart.items.push(cartItem)
}
increaseQuantity(cartItem)
refresh('#partial-header')
} else {
console.warn(`Article with id '${articleNo}' doesn't exist`)
}
}
/**
* Removes an item from the cart; is called after the confirm dialog.
* @param articleNo
*/
function removeFromCart(articleNo) {
const cartItem = findItemInCart(articleNo)
if (cartItem) {
cart.numOfItems -= cartItem.quantity
cart.grandTotal -= cartItem.total
const itemIndex = cart.items.indexOf(cartItem)
cart.items.splice(itemIndex, 1)
refresh('#partial-header')
refresh('#cart-table')
}
}
/**
* Increase the quantity of an existing cart item
* @param item
* @param doRefresh - trigger a refresh, afterward
*/
function increaseQuantity(item, doRefresh) {
if (typeof item === "number")
item = findItemInCart(item)
item.quantity++
item.showQuantity = (item.quantity > 1)
item.total += item.price
cart.numOfItems++
cart.grandTotal += item.price
if (doRefresh) {
refresh('#partial-header')
refresh('#cart-table')
}
}
/**
* Decrease the quantity of an existing cart item
* @param item
*/
function decreaseQuantity(item) {
if (typeof item === "number")
item = findItemInCart(item)
if (item.quantity > 0) {
item.quantity--
item.total -= item.price
cart.numOfItems--
cart.grandTotal -= item.price
refresh('#partial-header')
refresh('#cart-table')
}
}
/**
* Finds a cart item by its article number
* @param articleNo
* @returns {null|*}
*/
function findItemInCart(articleNo) {
for (const item of cart.items) {
if (item.id === articleNo) return item
}
// no cart item with this articleNo was found
return null
}
/**
* Finds a item in the list of books
* @param articleNo
* @returns {any|null}
*/
function findItemInArticles(articleNo) {
for (const item of data.books) {
if (item.id === articleNo) return item
}
// no shop item with this articleNo was found
return null
}
/**
* Stores the cart model in the session storage and refreshes the page
* @param querySelector
*/
function refresh(querySelector) {
sessionStorage.setItem("cart", JSON.stringify(cart))
render(cart, querySelector)
}