File tree Expand file tree Collapse file tree 3 files changed +82
-4
lines changed
Expand file tree Collapse file tree 3 files changed +82
-4
lines changed Original file line number Diff line number Diff line change 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 ) ;
Original file line number Diff line number Diff line change 11import { renderOrderSummary } from "./checkout/orderSummary.js" ;
22import { renderPaymentSummary } from "./checkout/paymentSummary.js" ;
3+ import '../data/cart-oop.js' ;
34
45renderOrderSummary ( ) ;
56renderPaymentSummary ( ) ;
Original file line number Diff line number Diff 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">
You can’t perform that action at this time.
0 commit comments