forked from SuperSimpleDev/javascript-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.js
112 lines (101 loc) · 3.25 KB
/
checkout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {cart, removeFromCart} from '../data/cart.js';
import {products} from '../data/products.js';
import {formatCurrency} from './utils/money.js';
let cartSummaryHTML = '';
cart.forEach((cartItem) => {
const productId = cartItem.productId;
let matchingProduct;
products.forEach((product) => {
if (product.id === productId) {
matchingProduct = product;
}
});
cartSummaryHTML += `
<div class="cart-item-container
js-cart-item-container-${matchingProduct.id}">
<div class="delivery-date">
Delivery date: Tuesday, June 21
</div>
<div class="cart-item-details-grid">
<img class="product-image"
src="${matchingProduct.image}">
<div class="cart-item-details">
<div class="product-name">
${matchingProduct.name}
</div>
<div class="product-price">
$${formatCurrency(matchingProduct.priceCents)}
</div>
<div class="product-quantity">
<span>
Quantity: <span class="quantity-label">${cartItem.quantity}</span>
</span>
<span class="update-quantity-link link-primary">
Update
</span>
<span class="delete-quantity-link link-primary js-delete-link" data-product-id="${matchingProduct.id}">
Delete
</span>
</div>
</div>
<div class="delivery-options">
<div class="delivery-options-title">
Choose a delivery option:
</div>
<div class="delivery-option">
<input type="radio" checked
class="delivery-option-input"
name="delivery-option-${matchingProduct.id}">
<div>
<div class="delivery-option-date">
Tuesday, June 21
</div>
<div class="delivery-option-price">
FREE Shipping
</div>
</div>
</div>
<div class="delivery-option">
<input type="radio"
class="delivery-option-input"
name="delivery-option-${matchingProduct.id}">
<div>
<div class="delivery-option-date">
Wednesday, June 15
</div>
<div class="delivery-option-price">
$4.99 - Shipping
</div>
</div>
</div>
<div class="delivery-option">
<input type="radio"
class="delivery-option-input"
name="delivery-option-${matchingProduct.id}">
<div>
<div class="delivery-option-date">
Monday, June 13
</div>
<div class="delivery-option-price">
$9.99 - Shipping
</div>
</div>
</div>
</div>
</div>
</div>
`;
});
document.querySelector('.js-order-summary')
.innerHTML = cartSummaryHTML;
document.querySelectorAll('.js-delete-link')
.forEach((link) => {
link.addEventListener('click', () => {
const productId = link.dataset.productId;
removeFromCart(productId);
const container = document.querySelector(
`.js-cart-item-container-${productId}`
);
container.remove();
});
});