Skip to content

18r Solution #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 18r
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion 2-copy-of-code/lesson-18/data/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,10 @@ export async function loadCartFetch() {
const text = await response.text();
console.log(text);
return text;
}
}

// Extra feature: make the cart empty after creating an order.
export function resetCart() {
cart = [];
saveToStorage();
}
9 changes: 9 additions & 0 deletions 2-copy-of-code/lesson-18/scripts/amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,13 @@ function renderProductsGrid() {
const search = document.querySelector('.js-search-bar').value;
Copy link

@antaugustol antaugustol Apr 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One extra featured that I find useful:
The amazon header is repeated in amazon.html, checkout.html and tracking.html so, I decided to extract it to its own module.
Note: the event listeners for the search bar/button must be placed in the header module.

// using cart-class.js oop
import { cart } from '../data/cart-class.js'
import { products, loadProductsFetch } from '../data/products.js'
import { renderAmazonHeader, displayCartQuantity } from './partials/header.js'

renderAmazonHeader()
loadPage()

// loading products from backend https://supersimplebackend.dev/products
async function loadPage() {
  await loadProductsFetch().catch(() => alert('error'))
  renderProductsGrid()
}

window.location.href = `amazon.html?search=${search}`;
});

// Extra feature: searching by pressing "Enter" on the keyboard.
document.querySelector('.js-search-bar')
.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
const searchTerm = document.querySelector('.js-search-bar').value;
window.location.href = `amazon.html?search=${searchTerm}`;
}
});
}
4 changes: 3 additions & 1 deletion 2-copy-of-code/lesson-18/scripts/checkout/paymentSummary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {cart} from '../../data/cart.js';
import {cart, resetCart} from '../../data/cart.js';
import {getProduct} from '../../data/products.js';
import {getDeliveryOption} from '../../data/deliveryOptions.js';
import {formatCurrency} from '../utils/money.js';
Expand Down Expand Up @@ -89,6 +89,8 @@ export function renderPaymentSummary() {
console.log('Unexpected error. Try again later.');
}

// Extra feature: make the cart empty after creating an order.
resetCart();
window.location.href = 'orders.html';
});
}
6 changes: 5 additions & 1 deletion 2-copy-of-code/lesson-18/scripts/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ async function loadPage() {
const deliveryTime = dayjs(productDetails.estimatedDeliveryTime);
const percentProgress = ((today - orderTime) / (deliveryTime - orderTime)) * 100;

// Extra feature: display "delivered" on the tracking page
// if today's date is past the delivery date.
const deliveredMessage = today < deliveryTime ? 'Arriving on' : 'Delivered on';

const trackingHTML = `
<a class="back-to-orders-link link-primary" href="orders.html">
View all orders
</a>

<div class="delivery-date">
Arriving on ${
${deliveredMessage} ${
dayjs(productDetails.estimatedDeliveryTime).format('dddd, MMMM D')
}
</div>
Expand Down