-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptPART2.js copy
135 lines (119 loc) · 4.03 KB
/
scriptPART2.js copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"use strict";
const productListSection = document.getElementById("product-list");
const productDetailSection = document.getElementById("product-detail");
const backButton = document.getElementById("back-button");
let allProducts = [];
// Function to fetch and display all products (List View)
async function getProducts() {
const options = {
method: "GET",
headers: {
Authorization: `Bearer patalfq5yroYtu7wV.77b794fc190f162598e9f93cc9207da0fdcd337901d7694394a4a1085cc0524c`,
},
};
try {
const response = await fetch(
`https://api.airtable.com/v0/appVWWY9vo2FICye0/Furnitures`,
options
);
const data = await response.json();
allProducts = data.records; // Store all products for filtering
displayProducts(allProducts);
} catch (error) {
console.error("Error fetching products:", error);
productListSection.innerHTML =
"<p>Failed to load products. Please try again later.</p>";
}
}
// Function to display products dynamically
function displayProducts(products) {
productListSection.innerHTML = ""; // Clear the container
products.forEach((record) => {
const { id } = record;
const { Name, Image, Price, Category } = record.fields;
const productCard = document.createElement("div");
productCard.className = "col-md-4 mb-4";
productCard.innerHTML = `
<div class="card product-card" onclick="showProductDetail('${id}')">
<img
src="${Image ? Image[0].url : "placeholder.jpg"}"
class="card-img-top"
alt="${Name}"
/>
<div class="card-body">
<h5 class="card-title">${Name}</h5>
<p class="card-text"><strong>Category:</strong> ${Category}</p>
<p class="card-text"><strong>Price:</strong> $${Price}</p>
</div>
</div>
`;
productListSection.appendChild(productCard);
});
}
// Function to filter products by category
function filterProducts(category) {
const filteredProducts = allProducts.filter(
(product) => product.fields.Category === category
);
displayProducts(filteredProducts);
}
// Function to fetch and display a single product (Detail View)
async function showProductDetail(productId) {
const options = {
method: "GET",
headers: {
Authorization: `Bearer patalfq5yroYtu7wV.77b794fc190f162598e9f93cc9207da0fdcd337901d7694394a4a1085cc0524c`,
},
};
try {
const response = await fetch(
`https://api.airtable.com/v0/appVWWY9vo2FICye0/Furnitures/${productId}`,
options
);
const data = await response.json();
const {
Name,
Image,
Description,
Material,
Price,
Availability,
Color,
} = data.fields;
productDetailSection.innerHTML = `
<div class="card">
<img
src="${Image ? Image[0].url : "placeholder.jpg"}"
class="card-img-top"
alt="${Name}"
/>
<div class="card-body">
<h2 class="card-title">${Name}</h2>
<p class="card-text">${Description}</p>
<p class="card-text"><strong>Material:</strong> ${Material}</p>
<p class="card-text"><strong>Color:</strong> ${Color}</p>
<p class="card-text"><strong>Price:</strong> $${Price}</p>
<p class="card-text"><strong>Availability:</strong> ${Availability}</p>
</div>
</div>
`;
// Switch views
productListSection.classList.add("hidden");
productDetailSection.classList.remove("hidden");
backButton.classList.remove("hidden");
} catch (error) {
console.error("Error fetching product detail:", error);
productDetailSection.innerHTML =
"<p>Failed to load product details. Please try again later.</p>";
}
}
// Function to return to the list view
function showListView() {
productListSection.classList.remove("hidden");
productDetailSection.classList.add("hidden");
backButton.classList.add("hidden");
}
// Event listener for the back button
backButton.addEventListener("click", showListView);
// Fetch products on page load
document.addEventListener("DOMContentLoaded", getProducts);