Skip to content

Commit d5448db

Browse files
committed
save data and generate html for payment summary
1 parent 8291baa commit d5448db

File tree

7 files changed

+78
-49
lines changed

7 files changed

+78
-49
lines changed

basic-projects/amazon-project/checkout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</div>
4141

4242
<div class="main">
43-
<div class="page-title">Review your order</div>
43+
<div class="page-title js-page-title">Review your order</div>
4444

4545
<div class="checkout-grid">
4646
<div class="order-summary js-order-summary">

basic-projects/amazon-project/data/cart.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import deliveryOptions from '../data/delivery-options.js';
1+
import {deliveryOptions} from '../data/delivery-options.js';
2+
import { renderPaymentSummary } from '../scripts/checkout/payment-summary.js';
23

34
// named export, used when there are multiple exports in the file
45
export let cart = JSON.parse(localStorage.getItem('cart')) || [];
@@ -44,6 +45,7 @@ export function removeFromCart(itemId){
4445
});
4546
cart = newCart;
4647
saveToStorage();
48+
renderPaymentSummary();
4749
};
4850

4951

basic-projects/amazon-project/data/delivery-options.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dayjs from 'https://cdn.jsdelivr.net/npm/dayjs/+esm';
22

3-
const deliveryOptions = [{
3+
export const deliveryOptions = [{
44
id: '1',
55
date: dayjs().add(7, 'days').format('dddd, MMMM D'),
66
priceCents: 0
@@ -16,4 +16,13 @@ const deliveryOptions = [{
1616
priceCents: 999
1717
}];
1818

19-
export default deliveryOptions;
19+
export function getDeliveryOption(deliveryOptionId){
20+
let deliveryOption;
21+
22+
deliveryOptions.forEach((option) => {
23+
if (option.id === deliveryOptionId){
24+
deliveryOption = option
25+
}
26+
});
27+
return deliveryOption || deliveryOptions[0];
28+
}

basic-projects/amazon-project/data/products.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
export function getProduct(productId){
2+
3+
let matchingProduct;
4+
// get the full product just using id
5+
products.forEach((product) => {
6+
if (product.id === productId){
7+
matchingProduct = product;
8+
}
9+
});
10+
return matchingProduct;
11+
}
12+
113
export const products = [
214
{
315
id: "e43638ce-6aa0-4b85-b27f-e1d07eb678c6",
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { renderOrderSummary } from "./checkout/orderSummary.js";
22
import { renderPaymentSummary } from "./checkout/payment-summary.js";
33

4-
renderPaymentSummary()
5-
renderOrderSummary();
4+
renderOrderSummary();
5+
renderPaymentSummary();

basic-projects/amazon-project/scripts/checkout/orderSummary.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {cart, removeFromCart, calculateCartQuantity, updateQuantityLabel, updateDeliveryOption} from '../../data/cart.js'
2-
import {products} from '../../data/products.js'
2+
import {getProduct} from '../../data/products.js'
33
import formatCurrency from '../utils/price.js';
4-
import deliveryOptions from '../../data/delivery-options.js';
4+
import {deliveryOptions, getDeliveryOption} from '../../data/delivery-options.js';
5+
import { renderPaymentSummary } from './payment-summary.js';
56

67

78

@@ -13,26 +14,13 @@ export function renderOrderSummary(){
1314
// get productId out of cartItem
1415
const {productId} = cartItem;
1516

16-
let matchingProduct;
17-
// get the full product just using id
18-
products.forEach((product) => {
19-
if (product.id === productId){
20-
matchingProduct = product;
21-
}
22-
});
17+
const matchingProduct = getProduct(productId);
18+
2319

2420
const deliveryOptionId = cartItem.deliveryOptionId;
2521

26-
let deliveryOption;
27-
28-
deliveryOptions.forEach((option) => {
29-
if (option.id === deliveryOptionId){
30-
deliveryOption = option
31-
}
32-
});
33-
34-
35-
22+
const deliveryOption = getDeliveryOption(deliveryOptionId);
23+
3624

3725
cartSummaryHTML += `
3826
<div class="cart-item-container js-cart-item-${matchingProduct.id}">
@@ -169,7 +157,7 @@ export function renderOrderSummary(){
169157

170158
updateQuantityLabel(newQuantity, itemId);
171159
updateCartQuantity();
172-
160+
renderPaymentSummary();
173161
quantityInput.value = null;
174162
};
175163

@@ -189,6 +177,7 @@ export function renderOrderSummary(){
189177
updateDeliveryOption(productId, deliveryOptionId);
190178
// regenerate the whole page, to update delivery data automatically when delivery option is selected
191179
renderOrderSummary();
180+
renderPaymentSummary();
192181
})
193182
});
194183
};
Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,84 @@
11
import { cart, calculateCartQuantity } from "../../data/cart.js"
2-
import { products } from "../../data/products.js";
2+
import { getProduct } from "../../data/products.js";
33
import 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 &amp; 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

Comments
 (0)