-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct-detail.js
194 lines (172 loc) · 7.24 KB
/
product-detail.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
document.addEventListener('DOMContentLoaded', async function() {
const urlParams = new URLSearchParams(window.location.search);
const productId = urlParams.get('id');
if (!productId) {
alert('No product ID specified');
return;
}
try {
const product = await productService.getProduct(productId);
console.log('Fetched product:', product); // Debug log
if (product) {
displayProduct(product);
updateSizeOptions(product.sizes);
loadSuggestedProducts(product.categoryId);
} else {
throw new Error('Product not found');
}
} catch (error) {
console.error('Error fetching product:', error);
alert('Error loading product details');
}
});
function displayProduct(product) {
// Update product details
document.getElementById('productName').textContent = product.productName;
document.getElementById('productPrice').textContent = `$${product.price.toFixed(2)}`;
document.getElementById('productDescription').textContent = product.description;
document.getElementById('productCategory').textContent = product.categoryName;
document.title = `${product.productName} - Product Details`;
// Handle images
const mainImg = document.getElementById('MainImg');
const imageGallery = document.getElementById('imageGallery');
// Get all available images and ensure they have absolute URLs
let allImages = [];
if (product.imagePath) {
let imagePath = product.imagePath;
if (!imagePath.startsWith('http')) {
imagePath = `http://localhost:5037/${imagePath}`;
}
allImages.push(imagePath);
}
if (product.imagePaths && product.imagePaths.length > 0) {
// Add any images from imagePaths that aren't already included
product.imagePaths.forEach(path => {
let imagePath = path;
if (!imagePath.startsWith('http')) {
imagePath = `http://localhost:5037/${imagePath}`;
}
if (!allImages.includes(imagePath)) {
allImages.push(imagePath);
}
});
}
console.log('Product images:', allImages); // Debug log
if (allImages.length > 0) {
// Set main image
mainImg.src = allImages[0];
console.log('Set main image:', allImages[0]); // Debug log
// Create thumbnails
imageGallery.innerHTML = allImages.map((path, index) => `
<div class="small-img-col">
<img src="${path}"
width="100%"
class="small-img ${index === 0 ? 'active' : ''}"
onclick="changeMainImage(this)"
alt="${product.productName} image ${index + 1}">
</div>
`).join('');
console.log('Created thumbnails for images'); // Debug log
} else {
// Use default image if no images are available
console.log('No images found, using default'); // Debug log
const defaultImage = 'img/product/f1.jpg';
mainImg.src = defaultImage;
imageGallery.innerHTML = `
<div class="small-img-col">
<img src="${defaultImage}"
width="100%"
class="small-img active"
onclick="changeMainImage(this)"
alt="Default product image">
</div>`;
}
}
function updateSizeOptions(sizes) {
const sizeSelect = document.getElementById('size');
sizeSelect.innerHTML = '<option value="">Select Size</option>';
if (sizes && sizes.length > 0) {
sizes.forEach(sizeObj => {
if (sizeObj.quantity > 0) {
const option = document.createElement('option');
option.value = sizeObj.size;
option.textContent = `${sizeObj.size} (${sizeObj.quantity} available)`;
sizeSelect.appendChild(option);
}
});
}
}
function changeMainImage(clickedImg) {
console.log('Changing main image to:', clickedImg.src); // Debug log
// Update main image
const mainImg = document.getElementById('MainImg');
mainImg.src = clickedImg.src;
// Update active state of thumbnails
document.querySelectorAll('.small-img').forEach(img => {
img.classList.remove('active');
});
clickedImg.classList.add('active');
}
function updateQuantity(change) {
const quantityInput = document.getElementById('quantity');
let currentValue = parseInt(quantityInput.value);
let newValue = currentValue + change;
// Ensure value stays within min and max bounds
newValue = Math.max(1, Math.min(10, newValue));
quantityInput.value = newValue;
}
async function addToCart() {
const urlParams = new URLSearchParams(window.location.search);
const productId = urlParams.get('id');
const quantity = parseInt(document.getElementById('quantity').value);
const size = document.getElementById('size').value;
if (!size) {
alert('Please select a size');
return;
}
try {
await cartService.addToCart(productId, quantity, size);
window.location.href = 'cart.html';
} catch (error) {
if (error.message === 'Please login first') {
window.location.href = 'login.html';
} else {
alert('Error adding item to cart: ' + error.message);
}
}
}
async function loadSuggestedProducts(categoryId) {
try {
// Fetch products from the same category
const products = await productService.getProductsByCategory(categoryId);
// Filter out the current product and limit to 4 suggestions
const currentProductId = new URLSearchParams(window.location.search).get('id');
const suggestedProducts = products
.filter(p => p.productId.toString() !== currentProductId)
.slice(0, 4);
// Display the suggested products
const container = document.getElementById('suggestedProductsContainer');
container.innerHTML = suggestedProducts.map(product => `
<div class="pro" onclick="window.location.href='product-detail.html?id=${product.productId}'">
<img src="${product.imagePath ?
(product.imagePath.startsWith('http') ?
product.imagePath :
'http://localhost:5037/' + product.imagePath) :
'img/product/f1.jpg'}"
alt="${product.productName}">
<div class="des">
<span>${product.categoryName}</span>
<h5>${product.productName}</h5>
<h4>$${product.price.toFixed(2)}</h4>
</div>
<div class="cart">
<i class="far fa-shopping-bag"></i>
</div>
</div>
`).join('');
} catch (error) {
console.error('Error loading suggested products:', error);
document.getElementById('suggestedProductsContainer').innerHTML =
'<p>Unable to load suggested products</p>';
}
}