Skip to content

Commit 2ec1925

Browse files
committed
fetch and DOM combined
1 parent a3ebd5c commit 2ec1925

1 file changed

Lines changed: 45 additions & 51 deletions

File tree

โ€Žmoto ride.jsโ€Ž

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -80,46 +80,52 @@ function changeCatalogMenueFunction(event) {
8080
}
8181
}
8282

83-
//?: ื”ื›ื ืกืช ืžื•ืฆืจื™ื ืžืชื•ืš ื’'ื™ื™ืกื•ืŸ ืœืงื˜ืœื•ื’ ืื•ืคื ื•ืขื™ื
84-
fetch('../assets/products.json')
85-
.then(response => response.json())
86-
.then(products => {
87-
// ื‘ื“ื™ืงืช ื›ืžื•ืช ืžื•ืฆืจื™ื ื‘ืืชืจ
88-
let productsNumbersTotal = 0;
89-
const productsNumbers = products.forEach(product => productsNumbersTotal++);
90-
const total_products = document.querySelector('.total_products');
91-
if (total_products)
92-
total_products.textContent = `Discover ${productsNumbersTotal} exlusive motorcyles`;
93-
94-
// ืกื™ื ื•ืŸ ื”ืžื•ืฆืจื™ื ืœืคื™ ื”ืคื•ืคื•ืœืจื™ื•ืช ื”ื’ื‘ื•ื”ื” ื‘ื™ื•ืชืจ
95-
const topPopularProducts = products
96-
.sort((a, b) => b.popularity - a.popularity) // ืžื™ื•ืŸ ืžื”ื’ื‘ื•ื” ืœื ืžื•ืš
97-
.slice(0, 4); // ืœืงื™ื—ืช ืจืง 4 ืจืืฉื•ื ื™ื
98-
99-
// ื‘ื ื™ื™ืช ื›ืจื˜ื™ืกื™ื ืจืง ืขื‘ื•ืจ ื”ืคื•ืคื•ืœืจื™ื™ื
100-
topPopularProducts.forEach(product => {
101-
createProductCardItemElement(product); // ื–ื• ื”ืคื•ื ืงืฆื™ื” ืฉืœืš ืฉื™ื•ืฆืจืช ืืช ื”ื›ืจื˜ื™ืก
102-
});
103-
104-
// ื‘ื•ื“ืง ืื ื–ื” ื“ืฃ ืžื•ืฆืจ ืœืคื™ ื›ืชื•ื‘ืช URL
105-
if (window.location.href.includes('product%20page.html')) {
106-
const urlId = window.location.href.split('=')[2].split('&')[0];
107-
const product = products.find(productId => productId.id === urlId);
108-
if (product) {
109-
createProductPage(product);
110-
} else {
111-
console.warn('Not found product with id:', urlId);
112-
}
113-
}
83+
//?: ื”ืฉืžื” ืฉืœ ืื™ืจื•ืข ืฉืžืจืขื ืŸ ืืช ื”ื“ืคื™ื
84+
document.addEventListener('DOMContentLoaded', () => {
85+
//?: ื”ื›ื ืกืช ืžื•ืฆืจื™ื ืžืชื•ืš ื’'ื™ื™ืกื•ืŸ ืœืงื˜ืœื•ื’ ืื•ืคื ื•ืขื™ื
86+
fetch('../assets/products.json')
87+
.then(response => response.json())
88+
.then(products => {
89+
// ื‘ื“ื™ืงืช ื›ืžื•ืช ืžื•ืฆืจื™ื ื‘ืืชืจ
90+
let productsNumbersTotal = 0;
91+
products.forEach(() => productsNumbersTotal++);
92+
const total_products = document.querySelector('.total_products');
93+
if (total_products)
94+
total_products.textContent = `Discover ${productsNumbersTotal} exlusive motorcyles`;
95+
96+
// ืกื™ื ื•ืŸ ื”ืžื•ืฆืจื™ื ืœืคื™ ื”ืคื•ืคื•ืœืจื™ื•ืช ื”ื’ื‘ื•ื”ื” ื‘ื™ื•ืชืจ
97+
const topPopularProducts = products
98+
.sort((a, b) => b.popularity - a.popularity) // ืžื™ื•ืŸ ืžื”ื’ื‘ื•ื” ืœื ืžื•ืš
99+
.slice(0, 4); // ืœืงื™ื—ืช ืจืง 4 ืจืืฉื•ื ื™ื
100+
101+
// ื‘ื ื™ื™ืช ื›ืจื˜ื™ืกื™ื ืจืง ืขื‘ื•ืจ ื”ืคื•ืคื•ืœืจื™ื™ื (ืœื“ืฃ ื”ื‘ื™ืช)
102+
topPopularProducts.forEach(product => {
103+
createProductCardItemElement(product); // ื–ื• ื”ืคื•ื ืงืฆื™ื” ืฉืœืš ืฉื™ื•ืฆืจืช ืืช ื”ื›ืจื˜ื™ืก
104+
});
105+
106+
// ื‘ื•ื“ืง ืื ื–ื” ื“ืฃ ืžื•ืฆืจ ืœืคื™ ื›ืชื•ื‘ืช URL
107+
if (window.location.href.includes('product%20page.html')) {
108+
const urlId = window.location.href.split('=')[2].split('&')[0];
109+
const product = products.find(p => p.id === urlId);
110+
if (product) {
111+
createProductPage(product);
112+
} else {
113+
console.warn('Not found product with id:', urlId);
114+
}
115+
}
116+
// ืื ื–ื” ืœื ื“ืฃ ืžื•ืฆืจ โ€“ ื™ื•ืฆืจื™ื ืืช ื›ืœ ื›ืจื˜ื™ืกื™ ื”ืงื˜ืœื•ื’ (ืœื“ืฃ ืงื˜ืœื•ื’)
117+
else if (window.location.href.includes('catalog.html')) {
118+
products.forEach(product => {
119+
creator(product);
120+
});
121+
}
122+
})
123+
.catch(error => console.error('Error loading JSON:', error));
114124

115-
// ืื ื–ื” ืœื ื“ืฃ ืžื•ืฆืจ โ€“ ื™ื•ืฆืจื™ื ืืช ื›ืœ ื›ืจื˜ื™ืกื™ ื”ืงื˜ืœื•ื’
116-
else {
117-
products.forEach(product => {
118-
creator(product);
119-
});
120-
}
121-
})
122-
.catch(error => console.error('Error loading JSON:', error));
125+
// ื˜ืขื™ื ืช ืžื•ืขื“ืคื™ื ื•ืขื’ืœื” ืžื”ืœื•ืงืืœ ืกื˜ื•ืจื’' ื‘ื›ืœ ื“ืฃ
126+
loadWishlistFromLocalStorage();
127+
loadCartFromLocalStorage();
128+
});
123129

124130
//TODO: ืคื•ื ืงืฆื™ื” ืจืืฉื™ืช ืฉืœ ื›ืœ ืžื” ืฉืคื•ืขืœ ืื—ืจื™ ืงื‘ืœืช ื”ื ืชื•ื ื™ื
125131
function creator (product){
@@ -489,18 +495,6 @@ function updateWishlistHearts() {
489495
});
490496
}
491497

492-
//?: ื”ืฉืžื” ืฉืœ ืื™ืจื•ืข ืฉืžืจืขื ืŸ ืืช ื”ื“ืคื™ื
493-
document.addEventListener('DOMContentLoaded', () => {
494-
loadWishlistFromLocalStorage();
495-
loadCartFromLocalStorage();
496-
497-
//TODO: ื‘ื“ื™ืงืช ื›ืชื•ื‘ืช ื•ืงืจื™ืื” ืœืคื•ื ืงืฆื™ื” ืœื‘ื ื™ื™ืช ื“ืฃ ืžื•ืฆืจ
498-
if (window.location.href.includes('product%20page.html')) {
499-
const urlId = window.location.href.split('=')[2].split('&')[0];
500-
createProductPage(product);
501-
}
502-
});
503-
504498
//TODO: ืคื•ื ืงืฆื™ื” ืฉืฉื•ืœืคืช ืžื”ื–ื™ื›ืจื•ืŸ ืืช ื”ื ืชื•ื ื™ื ืฉืœ ื”ืžื•ืฆืจื™ื ื•ืžื–ืžื ืช ืคื•ื ืงืฆื™ื” ืฉื‘ื•ื ื” ืื•ืชื ื‘ืžื•ืขื“ืคื™ื
505499
function loadWishlistFromLocalStorage() {
506500
const wishlist = JSON.parse(localStorage.getItem('wishlist')) || [];

0 commit comments

Comments
ย (0)