Skip to content

Commit f25727b

Browse files
committed
create oop version of cart
1 parent 798fb58 commit f25727b

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

data/cart-oop.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
function Cart(localStorageKey) {
2+
const cart = {
3+
cartItems: undefined,
4+
loadFromStorage() {
5+
this.cartItems = JSON.parse(localStorage.getItem(localStorageKey)) || [{
6+
productId: "e43638ce-6aa0-4b85-b27f-e1d07eb678c6",
7+
quantity: 2,
8+
deliveryOptionId: '1',
9+
},
10+
{
11+
productId: "15b6fc6f-327a-4ec4-896f-486349e85a3d",
12+
quantity: 1,
13+
deliveryOptionId: '2'
14+
}];
15+
},
16+
17+
saveToStorage() {
18+
localStorage.setItem(localStorageKey, JSON.stringify(this.cartItems));
19+
},
20+
21+
addToCart(productId) {
22+
let matchingItem;
23+
24+
this.cartItems.forEach((cartItem) => {
25+
if (productId === cartItem.productId) {
26+
matchingItem = cartItem;
27+
}
28+
})
29+
30+
if (matchingItem) {
31+
matchingItem.quantity++;
32+
} else{
33+
this.cartItems.push({
34+
productId: productId,
35+
quantity: 1,
36+
deliveryOptionId: '1'
37+
})
38+
}
39+
this.saveToStorage();
40+
},
41+
42+
removeFromCart(productId) {
43+
const newCart = [];
44+
45+
this.cartItems.forEach(cartItem => {
46+
if (cartItem.productId != productId) {
47+
newCart.push(cartItem);
48+
}
49+
});
50+
51+
this.cartItems = newCart;
52+
this.saveToStorage();
53+
},
54+
55+
updateDeliveryOption(productId, deliveryOptionId) {
56+
let matchingItem;
57+
58+
this.cartItems.forEach((cartItem) => {
59+
if (productId === cartItem.productId) {
60+
matchingItem = cartItem;
61+
}
62+
});
63+
64+
matchingItem.deliveryOptionId = deliveryOptionId;
65+
66+
this.saveToStorage();
67+
}
68+
};
69+
70+
return cart;
71+
}
72+
73+
const cart = Cart('cart-oop');
74+
const businessCart = Cart('cart-business');
75+
76+
cart.loadFromStorage();
77+
78+
businessCart.loadFromStorage();
79+
80+
console.log(cart);
81+
console.log(businessCart);

scripts/checkout.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { renderOrderSummary } from "./checkout/orderSummary.js";
22
import { renderPaymentSummary } from "./checkout/paymentSummary.js";
3+
import '../data/cart-oop.js';
34

45
renderOrderSummary();
56
renderPaymentSummary();

scripts/checkout/orderSummary.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ export function renderOrderSummary() {
2121
const deliveryDate = today.add(deliveryOption.deliveryDays, 'days');
2222
const dateString = deliveryDate.format('dddd, MMMM D');
2323

24-
console.log(dateString);
25-
26-
console.log(matchingProduct);
27-
2824
cartSummary +=
2925
`<div class="cart-item-container js-cart-item-container js-cart-item-container-${matchingProduct.id}">
3026
<div class="delivery-date">

0 commit comments

Comments
 (0)