11import { cart , removeFromCart , calculateCartQuantity , updateQuantityLabel } from '../data/cart.js'
22import { products } from '../data/products.js'
33import formatCurrency from './utils/price.js' ;
4- import dayjs from 'https://cdn.jsdelivr.net/npm/dayjs/+esm'
4+ import deliveryOptions from '../data/delivery-options.js' ;
5+ import dayjs from 'https://cdn.jsdelivr.net/npm/dayjs/+esm' ;
6+
57
68
79let cartSummaryHTML = ''
@@ -11,19 +13,30 @@ cart.forEach((cartItem) => {
1113 const { productId} = cartItem ;
1214
1315 let matchingProduct ;
14-
1516 // get the full product just using id
1617 products . forEach ( ( product ) => {
1718 if ( product . id === productId ) {
1819 matchingProduct = product ;
1920 }
2021 } ) ;
2122
23+ const deliveryOptionId = cartItem . deliveryOptionId ;
24+
25+ let deliveryOption ;
26+
27+ deliveryOptions . forEach ( ( option ) => {
28+ if ( option . id === deliveryOptionId ) {
29+ deliveryOption = option
30+ }
31+ } ) ;
32+
33+
34+
2235
2336 cartSummaryHTML += `
2437 <div class="cart-item-container js-cart-item-${ matchingProduct . id } ">
2538 <div class="delivery-date">
26- Delivery date: Wednesday, June 15
39+ Delivery date: ${ deliveryOption . date }
2740 </div>
2841
2942 <div class="cart-item-details-grid">
@@ -54,57 +67,53 @@ cart.forEach((cartItem) => {
5467 </span>
5568 </div>
5669 </div>
57-
5870 <div class="delivery-options">
5971 <div class="delivery-options-title">
6072 Choose a delivery option:
6173 </div>
74+ ${ deliveryOptionsHTML ( matchingProduct , cartItem ) }
75+ </div>
76+ </div>
77+ </div>
78+ `
79+ } ) ;
6280
63- <div class="delivery-option">
64- <input type="radio" class="delivery-option-input"
65- name="delivery-option-${ matchingProduct . id } ">
66- <div>
67- <div class="delivery-option-date">
68- ${ dayjs ( ) . add ( 7 , 'days' ) . format ( 'dddd, MMMM D' ) }
69- </div>
70- <div class="delivery-option-price">
71- FREE Shipping
72- </div>
73- </div>
74- </div>
75- <div class="delivery-option">
76- <input type="radio" checked class="delivery-option-input"
77- name="delivery-option-${ matchingProduct . id } ">
78- <div>
79- <div class="delivery-option-date">
80- ${ dayjs ( ) . add ( 3 , 'days' ) . format ( 'dddd, MMMM D' ) }
81- </div>
82- <div class="delivery-option-price">
83- $4.99 - Shipping
84- </div>
85- </div>
86- </div>
87- <div class="delivery-option js-delivery-option">
88- <input type="radio" class="delivery-option-input"
81+ document . querySelector ( '.js-order-summary' )
82+ . innerHTML = cartSummaryHTML ;
83+
84+
85+ function deliveryOptionsHTML ( matchingProduct , cartItem ) {
86+ let html = ''
87+
88+ deliveryOptions . forEach ( ( deliveryOption ) => {
89+
90+ const price = deliveryOption . priceCents === 0 ? 'FREE' : `$${ formatCurrency ( deliveryOption . priceCents ) } -` ;
91+
92+ const isChecked = deliveryOption . id === cartItem . deliveryOptionId
93+
94+ html += `
95+
96+ <div class="delivery-option js-delivery-option"
97+ data-product-id="${ matchingProduct . id } "
98+ data-delivery-option-id="${ deliveryOption . id } ">
99+ <input type="radio"
100+ ${ isChecked ? 'checked' : '' }
101+ class="delivery-option-input"
89102 name="delivery-option-${ matchingProduct . id } ">
90103 <div>
91104 <div class="delivery-option-date">
92- ${ dayjs ( ) . add ( 1 , 'days' ) . format ( 'dddd, MMMM D' ) }
105+ ${ deliveryOption . date }
93106 </div>
94107 <div class="delivery-option-price">
95- $9.99 - Shipping
108+ ${ price } Shipping
96109 </div>
97110 </div>
98111 </div>
99- </div>
100- </div>
101- </div>
102- `
103- } ) ;
104-
105- document . querySelector ( '.js-order-summary' )
106- . innerHTML = cartSummaryHTML ;
112+ `
107113
114+ } )
115+ return html ;
116+ } ;
108117
109118
110119document . querySelectorAll ( '.js-delete-button' )
@@ -115,17 +124,17 @@ document.querySelectorAll('.js-delete-button')
115124
116125 const container = document . querySelector ( `.js-cart-item-${ itemId } ` )
117126 container . remove ( ) ;
118- updateCartQunatity ( ) ;
127+ updateCartQuantity ( ) ;
119128 } )
120129 } ) ;
121130
122131
123- function updateCartQunatity ( ) {
132+ function updateCartQuantity ( ) {
124133
125134 const checkoutHeader = document . querySelector ( '.js-return-to-home-link' )
126135 checkoutHeader . innerHTML = `${ calculateCartQuantity ( ) } items` ;
127136} ;
128- updateCartQunatity ( ) ;
137+ updateCartQuantity ( ) ;
129138
130139
131140document . querySelectorAll ( `.js-update-button` )
@@ -158,7 +167,7 @@ function saveUpdatedQuantity(itemId){
158167 const newQuantity = Number ( quantityInput . value ) ;
159168
160169 updateQuantityLabel ( newQuantity , itemId ) ;
161- updateCartQunatity ( ) ;
170+ updateCartQuantity ( ) ;
162171
163172 quantityInput . value = null ;
164173} ;
@@ -172,10 +181,4 @@ document.querySelectorAll('.js-save-link')
172181 saveUpdatedQuantity ( itemId ) ;
173182
174183 } )
175- } ) ;
176-
177-
178- document . querySelectorAll ( '.delivery-option-input' )
179- . forEach ( ( input ) => {
180- input
181- } )
184+ } ) ;
0 commit comments