Skip to content

Commit 878690a

Browse files
committed
first commit
0 parents  commit 878690a

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

index.html

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html lang="tr">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>FAKESTORE API</title>
7+
<link rel="stylesheet" href="style.css" />
8+
<link
9+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
10+
rel="stylesheet"
11+
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
12+
crossorigin="anonymous"
13+
/>
14+
</head>
15+
<body>
16+
<header class="header">
17+
<nav>
18+
<div class="text-center">
19+
<h5 class="display-5">Products</h5>
20+
</div>
21+
</nav>
22+
</header>
23+
24+
<div>
25+
<div class="container">
26+
<div class="row">
27+
<div class="text-center">
28+
<select
29+
class="form-select"
30+
aria-label="Default select example"
31+
id="select"
32+
></select>
33+
</div>
34+
</div>
35+
</div>
36+
</div>
37+
38+
<section>
39+
<div class="container">
40+
<div class="row" id="products"></div>
41+
</div>
42+
</section>
43+
44+
<script src="main.js"></script>
45+
<script
46+
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
47+
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
48+
crossorigin="anonymous"
49+
></script>
50+
</body>
51+
</html>

main.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Ürünleri API'den çeken fonksiyon
2+
async function productFetchData() {
3+
try {
4+
const response = await fetch("https://fakestoreapi.com/products");
5+
const products = await response.json();
6+
return products;
7+
} catch (error) {
8+
console.error("API Hatası: " + error);
9+
}
10+
}
11+
12+
// Kategorileri oluşturup render eden fonksiyon
13+
const categoryList = (products) => {
14+
const categories = products.map((product) => product.category);
15+
const uniqueCategories = [...new Set(categories)];
16+
renderCategory(uniqueCategories);
17+
};
18+
19+
// Kategorileri <select> içinde gösteren fonksiyon
20+
const renderCategory = (categories) => {
21+
const select = document.getElementById("select");
22+
select.innerHTML = `<option value="All">Tüm Kategoriler</option>`; // Tüm Kategoriler seçeneği ekledik
23+
categories.forEach((category) => {
24+
select.innerHTML += `<option value="${category}">${category}</option>`;
25+
});
26+
};
27+
28+
// Ürün filtreleme
29+
30+
// Kategoriye göre ürünleri filtreleyen fonksiyon
31+
const filterProductsByCategory = (category, products) => {
32+
if (category === "All") {
33+
return products; // Tüm kategoriler seçildiyse tüm ürünleri göster
34+
}
35+
return products.filter((product) => product.category === category);
36+
};
37+
38+
// Kategori değiştiğinde çalışan fonksiyon
39+
const handleCategoryChange = (event, products) => {
40+
const selectedValue = event.target.value;
41+
const filtered = filterProductsByCategory(selectedValue, products);
42+
renderProducts(filtered);
43+
};
44+
45+
// Kategori seçim değişikliğini başlatan fonksiyon
46+
const initCategoryFilter = (products) => {
47+
const select = document.getElementById("select");
48+
select.addEventListener("change", (event) => {
49+
handleCategoryChange(event, products);
50+
});
51+
};
52+
53+
// -----------------------
54+
55+
// Ürünleri ekrana basan fonksiyon
56+
const renderProducts = (products) => {
57+
const productRow = document.getElementById("products");
58+
productRow.innerHTML = ""; // Önceki ürünleri temizle
59+
60+
const productItems = products.map((product) => {
61+
const li = document.createElement("li");
62+
63+
// Ürün resmi
64+
const image = document.createElement("img");
65+
image.src = product.image;
66+
image.classList.add("productImage");
67+
68+
// Ürün değerlendirmesi
69+
const rating = product.rating.rate;
70+
const paragraph = document.createElement("p");
71+
paragraph.textContent = `Ürün Değerlendirmesi: ${rating}`;
72+
73+
// Ürün başlığı ve fiyatı
74+
const productTitle = product.title;
75+
const price = (product.price * 1.2).toFixed(2);
76+
li.textContent = `${productTitle} - Fiyatı : $${price}`;
77+
li.appendChild(image);
78+
li.appendChild(paragraph);
79+
80+
return li;
81+
});
82+
83+
productItems.forEach((item) => {
84+
productRow.appendChild(item);
85+
});
86+
};
87+
88+
// API'den verileri çek ve kategori filtrelemeyi başlat
89+
productFetchData().then((products) => {
90+
renderProducts(products); // Tüm ürünleri başta göster
91+
categoryList(products); // Kategorileri doldur
92+
initCategoryFilter(products); // Kategori filtrelemeyi başlat
93+
});

style.css

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
body {
2+
font-size: 15px !important;
3+
font-family: Verdana, Geneva, Tahoma, sans-serif;
4+
background-color: rgb(212, 241, 243) !important;
5+
}
6+
7+
#products li {
8+
list-style-type: none;
9+
width: 300px;
10+
height: 350px;
11+
border: 1px solid rgb(181, 181, 181);
12+
padding: 10px 15px;
13+
margin: 10px 15px;
14+
border-radius: 15px;
15+
transition: 0.3s ease-in-out;
16+
}
17+
li:hover {
18+
box-shadow: 0 0 9px red;
19+
transform: scale(1.07) translateY(-5px);
20+
}
21+
.productImage {
22+
width: 90%;
23+
height: 250px !important;
24+
padding: 10px;
25+
border-radius: 25px;
26+
}
27+
#select {
28+
width: 250px;
29+
box-shadow: 0 0 6px black;
30+
cursor: pointer;
31+
}

0 commit comments

Comments
 (0)