Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 46x 46x 46x 46x 14x 14x 46x 32x 32x 1x 1x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 6x 6x 6x 6x 6x 55x 55x 55x 55x 55x 55x 55x 55x 55x 46x 46x 46x 46x 46x 1x 1x 1x 1x 45x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 47x 55x 55x 55x 46x 46x 46x 46x 55x 55x 55x 55x 46x 46x 46x 46x 14x 14x 14x 14x 46x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 1x 55x 55x 55x 55x 55x 2x 55x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 2x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 52x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 53x 55x 55x 55x 55x 1x 1x | "use client";
import React, { useEffect, useState, useRef } from "react";
import Newsletter from "@/components/shared/Newsletter";
import RecentlyViewdItems from "./RecentlyViewd";
import { usePreviewSlider } from "@/contexts";
import { useAppSelector } from "@/redux/store";
import SpecificationsTable from "@/components/features/product/SpecificationsTable";
import ProductDetails from "@/components/features/product/ProductDetails";
import { ReviewSection } from "@/components/features/product/Reviews";
import { Icon } from "@/components/ui/icons";
import { StarRating, PriceDisplay, DiscountBadge, Button, ProductImage } from "@/components/ui";
import { WishlistButton } from "@/components/features/product/WishlistButton";
import { useProductActions } from "@/hooks/useProductActions";
// Map between tab IDs and URL hashes
const TAB_HASH_MAP: Record<string, string> = {
'tabOne': 'description',
'tabTwo': 'specifications',
'tabThree': 'reviews',
};
const HASH_TAB_MAP: Record<string, string> = {
'description': 'tabOne',
'specifications': 'tabTwo',
'reviews': 'tabThree',
};
// Helper function to get initial tab from URL hash
const getInitialTabFromHash = (): string => {
if (typeof window !== 'undefined') {
const hash = window.location.hash.replace('#', '');
const tabId = HASH_TAB_MAP[hash];
if (tabId) {
return tabId;
}
}
return "tabOne";
};
const ShopDetails = () => {
const { openPreviewModal } = usePreviewSlider();
const [previewImg, setPreviewImg] = useState(0);
const [quantity, setQuantity] = useState(1);
const [activeTab, setActiveTab] = useState(getInitialTabFromHash);
const tabSectionRef = useRef<HTMLDivElement>(null);
const tabs = [
{
id: "tabOne",
title: "Description"},
{
id: "tabTwo",
title: "Additional Information"},
{
id: "tabThree",
title: "Reviews"},
];
// Handle tab change - update URL hash
const handleTabChange = (tabId: string) => {
setActiveTab(tabId);
const hash = TAB_HASH_MAP[tabId];
if (hash && typeof window !== 'undefined') {
window.history.replaceState(null, '', `#${hash}`);
}
};
const productFromStorage = useAppSelector(
(state) => state.productDetailsReducer.value
);
// Get product from localStorage on client only (SSR-safe)
// Use lazy initialization to read from localStorage once on mount
const [product, setProduct] = useState(() => {
// During SSR, return Redux state
if (typeof window === 'undefined') {
return productFromStorage;
}
// On client, try to get from localStorage first
const stored = localStorage.getItem("productDetails");
if (stored) {
try {
return JSON.parse(stored);
} catch {
return productFromStorage;
}
}
return productFromStorage;
});
// Use the product actions hook for cart/wishlist operations
const { addToCart } = useProductActions(product);
// Sync Redux state changes to local state (for navigation from other pages)
// This is intentional derived state sync - the effect updates local state when
// Redux state changes due to navigation. This is a valid pattern for keeping
// local state in sync with external state changes.
useEffect(() => {
if (productFromStorage?.id && productFromStorage.id !== product?.id) {
// eslint-disable-next-line react-hooks/set-state-in-effect -- Intentional sync from Redux state on navigation
setProduct(productFromStorage);
}
}, [productFromStorage, product?.id]);
useEffect(() => {
// Sync product to localStorage when it changes
if (product && typeof window !== 'undefined') {
localStorage.setItem("productDetails", JSON.stringify(product));
}
}, [product]);
// Handle URL hash on load - scroll to tab section if hash is present
useEffect(() => {
if (typeof window !== 'undefined') {
const hash = window.location.hash.replace('#', '');
const tabId = HASH_TAB_MAP[hash];
if (tabId) {
// Small delay to ensure DOM is ready, then scroll to tab section
setTimeout(() => {
tabSectionRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 100);
}
}
}, []);
// Calculate rating and review count - handle both legacy and new data formats
// Legacy format: product.rating (number), product.reviews (number)
// New format: product.averageRating (number), product.reviews (array)
const reviewCount = Array.isArray(product.reviews) ? product.reviews.length : (product.reviews ?? 0);
const averageRating = product.averageRating ?? product.rating ?? 0;
// pass the product here when you get the real data.
const handlePreviewSlider = () => {
openPreviewModal();
};
return (
<>
{product.title === "" ? (
"Please add product"
) : (
<>
<section className="overflow-hidden relative pb-20 pt-[140px]">
<div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0">
<div className="flex flex-col lg:flex-row gap-7.5 xl:gap-17.5">
<div className="lg:max-w-[570px] w-full">
<div className="aspect-square lg:aspect-auto lg:min-h-[512px] rounded-lg shadow-1 bg-gray-2 dark:bg-gray-800 p-4 sm:p-7.5 relative flex items-center justify-center">
<div className="w-full h-full flex items-center justify-center">
<Button
onClick={handlePreviewSlider}
aria-label="Zoom product image"
variant="ghost"
size="sm"
className="gallery__Image w-11 h-11 rounded-[5px] bg-gray-1 dark:bg-gray-700 shadow-1 absolute top-4 lg:top-6 right-4 lg:right-6 z-50"
>
<Icon name="expand" size={22} />
</Button>
<ProductImage
src={product.imgs?.previews[previewImg]}
alt={product.title || "Product image"}
width={400}
height={400}
/>
</div>
</div>
{/* ? 'border-blue ' : 'border-transparent' */}
<div className="flex flex-wrap sm:flex-nowrap gap-4.5 mt-6">
{product.imgs?.thumbnails.map((item: string, key: number) => (
<Button
onClick={() => setPreviewImg(key)}
key={key}
variant="ghost"
size="sm"
className={`w-15 sm:w-25 h-15 sm:h-25 overflow-hidden rounded-lg bg-gray-2 dark:bg-gray-800 shadow-1 border-2 hover:border-blue ${
key === previewImg
? "border-blue"
: "border-transparent"
}`}
>
<ProductImage
width={50}
height={50}
src={item}
alt={`${product.title || "Product"} thumbnail ${key + 1}`}
/>
</Button>
))}
</div>
</div>
{/* <!-- product content --> */}
<div className="max-w-[539px] w-full">
<div className="flex items-center justify-between mb-3">
<h2 className="font-semibold text-xl sm:text-2xl xl:text-custom-3 text-dark dark:text-gray-100">
{product.title}
</h2>
<DiscountBadge percentage={30} />
</div>
<div className="flex flex-wrap items-center gap-5.5 mb-4.5">
<button
type="button"
onClick={() => {
handleTabChange('tabThree');
setTimeout(() => {
tabSectionRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
}, 100);
}}
className="cursor-pointer hover:opacity-80 transition-opacity"
>
<StarRating
rating={averageRating}
reviewCount={reviewCount}
showCount={true}
size="sm"
/>
</button>
<div className="flex items-center gap-1.5">
<Icon name="check-circle" size={20} style={{ color: "#22AD5C" }} />
<span className="text-green"> In Stock </span>
</div>
</div>
<div className="mb-4.5">
<PriceDisplay
price={product.price}
discountedPrice={product.discountedPrice}
size="lg"
/>
</div>
<ul className="flex flex-col gap-2 text-dark-3 dark:text-gray-300">
<li className="flex items-center gap-2.5">
<Icon name="check-circle" size={20} style={{ color: "#3C50E0" }} />
Free delivery available
</li>
<li className="flex items-center gap-2.5">
<Icon name="check-circle" size={20} style={{ color: "#3C50E0" }} />
Sales 30% Off Use Code: PROMO30
</li>
</ul>
<form onSubmit={(e) => e.preventDefault()}>
<div className="flex flex-wrap items-center gap-4.5">
<div className="flex items-center rounded-md border border-gray-3 dark:border-gray-600">
<Button
aria-label="Decrease quantity"
variant="ghost"
size="sm"
className="w-12 h-12"
onClick={() =>
quantity > 1 && setQuantity(quantity - 1)
}
>
<Icon name="minus" size={20} />
</Button>
<span className="flex items-center justify-center w-16 h-12 border-x border-gray-4 dark:border-gray-600 dark:text-gray-200">
{quantity}
</span>
<Button
onClick={() => setQuantity(quantity + 1)}
aria-label="Increase quantity"
variant="ghost"
size="sm"
className="w-12 h-12"
>
<Icon name="plus" size={20} />
</Button>
</div>
<Button
onClick={() => addToCart(quantity)}
variant="primary"
size="md"
>
Add to Cart
</Button>
<WishlistButton product={product} variant="icon-large" />
</div>
</form>
</div>
</div>
</div>
</section>
<section ref={tabSectionRef} id="reviews-section" className="overflow-hidden bg-gray-2 dark:bg-gray-800 py-20">
<div className="max-w-[1170px] w-full mx-auto px-4 sm:px-8 xl:px-0">
{/* <!--== tab header start ==--> */}
<div className="flex flex-wrap items-center bg-white dark:bg-gray-900 rounded-[10px] shadow-1 gap-5 xl:gap-12.5 py-4.5 px-4 sm:px-6">
{tabs.map((item, key) => (
<Button
key={key}
onClick={() => handleTabChange(item.id)}
variant="ghost"
size="md"
className={`font-medium lg:text-lg relative before:h-0.5 before:bg-blue before:absolute before:left-0 before:bottom-0 before:ease-out before:duration-200 hover:before:w-full ${
activeTab === item.id
? "text-blue before:w-full"
: "text-dark dark:text-gray-200 before:w-0"
}`}
>
{item.title}
</Button>
))}
</div>
{/* <!--== tab header end ==--> */}
{/* <!--== tab content start ==--> */}
{/* <!-- tab content one start --> */}
<div>
<div
className={`mt-12.5 ${
activeTab === "tabOne" ? "block" : "hidden"
}`}
>
{/* Product Description */}
{product.description && (
<div className="mb-8">
<h2 className="font-medium text-2xl text-dark dark:text-gray-100 mb-4">
Description
</h2>
<p className="text-body dark:text-gray-300">{product.description}</p>
</div>
)}
{/* Product Details (Key Features) */}
<ProductDetails details={product.details} />
</div>
</div>
{/* <!-- tab content one end --> */}
{/* <!-- tab content two start --> */}
<div>
<div
className={`mt-10 ${
activeTab === "tabTwo" ? "block" : "hidden"
}`}
>
{/* Product Specifications Table */}
<SpecificationsTable specifications={product.specifications} />
</div>
</div>
{/* <!-- tab content two end --> */}
{/* <!-- tab content three start --> */}
<div>
<div
className={`mt-12.5 ${
activeTab === "tabThree" ? "block" : "hidden"
}`}
>
{product.id && (
<ReviewSection productId={product.id} />
)}
</div>
</div>
{/* <!-- tab content three end --> */}
{/* <!--== tab content end ==--> */}
</div>
</section>
<RecentlyViewdItems />
<Newsletter />
</>
)}
</>
);
};
export default ShopDetails;
|