Skip to content

Commit 7f71f8e

Browse files
committed
generate html for delivery options
1 parent 05e58af commit 7f71f8e

File tree

3 files changed

+79
-66
lines changed

3 files changed

+79
-66
lines changed

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
// named export, used when there are multiple exports in the file
2-
export let cart = JSON.parse(localStorage.getItem('cart'));
3-
4-
if (!cart){
5-
cart = [{
6-
productId: '83d4ca15-0f35-48f5-b7a3-1ea210004f2e',
7-
quantity: 2
8-
},
9-
{
10-
productId: '54e0eccd-8f36-462b-b68a-8182611d9add',
11-
quantity: 4
12-
}];
13-
}
1+
import deliveryOptions from '../data/delivery-options.js';
142

3+
// named export, used when there are multiple exports in the file
4+
export let cart = JSON.parse(localStorage.getItem('cart')) || [];
155

166

177
function saveToStorage(){
188
localStorage.setItem('cart', JSON.stringify(cart))
19-
}
9+
};
2010

2111
export function addToCart(productId){
2212
const selectQuant = document.querySelector(`.js-select-quant-${productId}`)
@@ -35,7 +25,8 @@ export function addToCart(productId){
3525
} else {
3626
cart.push({
3727
productId,
38-
quantity
28+
quantity,
29+
deliveryOptionId: deliveryOptions[0].id
3930
});
4031
};
4132

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import dayjs from 'https://cdn.jsdelivr.net/npm/dayjs/+esm';
2+
3+
const deliveryOptions = [{
4+
id: '1',
5+
date: dayjs().add(7, 'days').format('dddd, MMMM D'),
6+
priceCents: 0
7+
},
8+
{
9+
id: '2',
10+
date: dayjs().add(3, 'days').format('dddd, MMMM D'),
11+
priceCents: 499
12+
},
13+
{
14+
id: '3',
15+
date: dayjs().add(1, 'days').format('dddd, MMMM D'),
16+
priceCents: 999
17+
}];
18+
19+
export default deliveryOptions;

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

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {cart, removeFromCart, calculateCartQuantity, updateQuantityLabel} from '../data/cart.js'
22
import {products} from '../data/products.js'
33
import 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

79
let 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

110119
document.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

131140
document.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

Comments
 (0)