11import { cart , calculateCartQuantity } from "../../data/cart.js"
2- import { products } from "../../data/products.js" ;
2+ import { getProduct } from "../../data/products.js" ;
33import formatCurrency from "../utils/price.js" ;
4+ import { getDeliveryOption } from "../../data/delivery-options.js" ;
45
56
67
78
9+ export function renderPaymentSummary ( ) {
10+
11+ let paymentHTML = document . querySelector ( '.js-payment-summary' )
12+
13+ if ( cart . length === 0 ) {
14+ paymentHTML . remove ( ) ;
15+ document . querySelector ( '.js-page-title' ) . innerHTML = 'Cart is empty'
16+ } else {
817
9- let itemsPrice = 0 ;
18+ let itemsPriceCents = 0 ;
19+ let deliveryShipping = 0 ;
1020
11- cart . forEach ( ( cartItem ) => {
12- // get productId out of cartItem
13- const { productId} = cartItem ;
21+ cart . forEach ( ( cartItem ) => {
1422
15- let matchingProduct ;
16- // get the full product just using id
17- products . forEach ( ( product ) => {
18- if ( product . id === productId ) {
19- matchingProduct = product ;
20- }
23+ const product = getProduct ( cartItem . productId )
24+ itemsPriceCents += product . priceCents * cartItem . quantity ;
25+
26+ const deliveryOption = getDeliveryOption ( cartItem . deliveryOptionId )
27+ deliveryShipping += deliveryOption . priceCents ;
2128 } ) ;
22- itemsPrice += Number ( formatCurrency ( matchingProduct . priceCents * cartItem . quantity ) )
23- } ) ;
2429
25- export function renderPaymentSummary ( ) {
30+ const totalBeforeTax = formatCurrency ( itemsPriceCents + deliveryShipping )
31+ const tax = totalBeforeTax / 10
32+ const orderTotal = ( Number ( totalBeforeTax ) + tax )
2633
2734
28- document . querySelector ( '.js-payment-summary' )
29- . innerHTML = `
35+
36+
37+ paymentHTML . innerHTML = `
3038
3139 <div class="payment-summary-title">
3240 Order Summary
3341 </div>
3442
3543 <div class="payment-summary-row">
3644 <div>Items (${ calculateCartQuantity ( ) } ):</div>
37- <div class="payment-summary-money">$${ itemsPrice } </div>
45+ <div class="payment-summary-money">$
46+ ${ formatCurrency ( itemsPriceCents ) }
47+ </div>
3848 </div>
3949
4050 <div class="payment-summary-row">
4151 <div>Shipping & handling:</div>
42- <div class="payment-summary-money">$4.99</div>
52+ <div class="payment-summary-money">$
53+ ${ formatCurrency ( deliveryShipping ) }
54+ </div>
4355 </div>
4456
4557 <div class="payment-summary-row subtotal-row">
4658 <div>Total before tax:</div>
47- <div class="payment-summary-money">$47.74</div>
59+ <div class="payment-summary-money">$
60+ ${ totalBeforeTax }
61+ </div>
4862 </div>
4963
5064 <div class="payment-summary-row">
5165 <div>Estimated tax (10%):</div>
52- <div class="payment-summary-money">$4.77</div>
66+ <div class="payment-summary-money">$
67+ ${ tax . toFixed ( 2 ) }
68+ </div>
5369 </div>
5470
5571 <div class="payment-summary-row total-row">
5672 <div>Order total:</div>
57- <div class="payment-summary-money">$52.51</div>
73+ <div class="payment-summary-money">$
74+ ${ orderTotal . toFixed ( 2 ) }
75+ </div>
5876 </div>
5977
6078 <button class="place-order-button button-primary">
6179 Place your order
6280 </button>
6381
6482 `
65-
66-
6783}
84+ }
0 commit comments