-
Notifications
You must be signed in to change notification settings - Fork 2
/
cart.service.ts
142 lines (105 loc) · 3.85 KB
/
cart.service.ts
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { Cart, CartItem } from '../model/cart.model';
@Injectable({
providedIn: 'root'
})
export class CartService {
public cartDetails: any;
private cartProducts = new BehaviorSubject([]);
currentCartProducts = this.cartProducts.asObservable();
private cartTotal = new BehaviorSubject(0);
currentCartTotal = this.cartTotal.asObservable();
private cartItemCount = new BehaviorSubject(0);
currentCartItemCount = this.cartItemCount.asObservable();
constructor() { }
loadCart(): Observable<CartItem[]> | any {
if (localStorage['cart_contents'] === undefined) {
this.cartDetails = {
cartItems: [],
cartTotal: 0,
cartItemCount: 0,
};
}
else {
this.cartDetails = JSON.parse(
localStorage
.getItem('cart_contents')!) as Cart || {};
}
this.saveCart()
this.cartProducts.next(this.cartDetails.cartItems);
}
saveCart() {
this.cartDetails.cartTotal = this.loadCartTotal();
this.cartDetails.cartItemCount = this.loadCartCount();
localStorage.setItem('cart_contents', JSON.stringify(this.cartDetails));
this.cartDetails.cartTotal = this.loadCartTotal();
this.cartDetails.cartItemCount = this.loadCartCount();
}
loadCartCount() {
let itemCount = this.cartDetails
.cartItems
.reduce((acc: any, item: any) => {
return acc += item.quantity;
}, 0);
this.cartDetails.cartItemCount = itemCount;
this.cartItemCount.next(itemCount);
return itemCount;
}
loadCartTotal() {
let cartTotal = this.cartDetails
.cartItems
.reduce((acc: any, item: any) => {
return acc += (item.price * item.quantity);
}, 0)
this.cartDetails.cartTotal = cartTotal;
this.cartTotal.next(cartTotal);
return cartTotal;
}
addItemToCart(addedProduct: any) {
const itemInCartCheck = this.cartDetails.cartItems.findIndex((x: any) => x.id === addedProduct.id) > -1;
const idxCheck = this.cartDetails.cartItems.findIndex((x: any) => x.id === addedProduct.id);
const itemAndSizeInCartCheck = this.cartDetails.cartItems.findIndex((x: any) => x.id === addedProduct.id && x.variant === addedProduct.variant);
if (!itemInCartCheck) {
/* add item to cart if not already there */
this.cartDetails.cartItems.push(addedProduct);
}
else if (itemInCartCheck && !addedProduct.hasOwnProperty('variant')) {
/* item is in cart, no variant, overide with new quanity */
this.cartDetails.cartItems[idxCheck].quantity = addedProduct.quantity;
}
else if (itemAndSizeInCartCheck > -1) {
/* item with variant in cart, overide with new quanity */
this.cartDetails.cartItems[itemAndSizeInCartCheck].quantity = addedProduct.quantity;
}
else {
/* item is in cart but not size, add new item with size and quantity */
this.cartDetails.cartItems.push(addedProduct);
}
this.saveCart();
}
updateCartItem(increaseOrDecrease: string, product: any): any {
const idx = this.cartDetails.cartItems.findIndex((x: any) => x.id === product.id && x.variant === product.variant);
const productQuantity = this.cartDetails.cartItems[idx].quantity;
if (increaseOrDecrease === 'increase') {
this.cartDetails.cartItems[idx].quantity = productQuantity + 1;
}
if (increaseOrDecrease === 'decrease' && productQuantity > 1) {
this.cartDetails.cartItems[idx].quantity = productQuantity - 1;
}
else {
return null
}
}
removeCartItem(product: any) {
const idx = this.cartDetails.cartItems.findIndex((x: any) => x.id === product.id && x.variant === product.variant)
if (idx > -1) {
this.cartDetails.cartItems.splice(idx, 1);
this.saveCart();
}
}
emptyCart() {
localStorage.clear();
this.loadCart();
}
}