-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
43 lines (34 loc) · 1.04 KB
/
store.js
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
class Store { //Local storage
static getItems() {
let store_items;
if(localStorage.getItem('store_items') === null) {
store_items = [];
} else {
store_items = JSON.parse(localStorage.getItem('store_items'));
}
return store_items;
}
static displayItems(){
const store_items = Store.getItems();
store_items.forEach(function(item){
const ui = new UI;
// Add book to UI
ui.reloadAddToCart(item);
});
}
static addItem(item){
const store_items = Store.getItems();
store_items.push(item);
localStorage.setItem('store_items', JSON.stringify(store_items));
}
static removeItem(item_id){
let id = item_id
const store_items = Store.getItems();
store_items.forEach(function(item, index){
if(item.id === id) {
store_items.splice(index, 1);
}
});
localStorage.setItem('store_items', JSON.stringify(store_items));
}
}