Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion 2-copy-of-code/lesson-18/scripts/orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {getProduct, loadProductsFetch} from '../data/products.js';
import {orders} from '../data/orders.js';
import dayjs from 'https://unpkg.com/dayjs@1.11.10/esm/index.js';
import formatCurrency from './utils/money.js';
import {addToCart} from '../data/cart.js';

async function loadPage() {
await loadProductsFetch();
Expand Down Expand Up @@ -61,7 +62,8 @@ async function loadPage() {
<div class="product-quantity">
Quantity: ${productDetails.quantity}
</div>
<button class="buy-again-button button-primary">
<button class="buy-again-button button-primary js-buy-again"
data-product-id="${product.id}">
<img class="buy-again-icon" src="images/icons/buy-again.png">
<span class="buy-again-message">Buy it again</span>
</button>
Expand All @@ -81,6 +83,22 @@ async function loadPage() {
}

document.querySelector('.js-orders-grid').innerHTML = ordersHTML;

document.querySelectorAll('.js-buy-again').forEach((button) => {
button.addEventListener('click', () => {
addToCart(button.dataset.productId);

// (Optional) display a message that the product was added,
// then change it back after a second.
button.innerHTML = 'Added';
setTimeout(() => {
button.innerHTML = `
<img class="buy-again-icon" src="images/icons/buy-again.png">
<span class="buy-again-message">Buy it again</span>
`;
}, 1000);
});
});
}

loadPage();