1- import React from 'react' ;
1+ import React , { useState } from 'react' ;
22import { useParams } from 'react-router-dom' ;
3- import { useResolve } from './hooks' ;
3+ import { useResolve , useProductImages } from './hooks' ;
44import { loadProductBySlug } from './service' ;
55import { CompareCheck } from './CompareCheck' ;
6- import { ProductMainImage } from './ProductMainImage' ;
76import { useTranslation , useCurrency } from './app-state' ;
87import { isProductAvailable } from './helper' ;
98
@@ -18,17 +17,46 @@ export const Product: React.FC = () => {
1817 const { productSlug } = useParams < ProductParams > ( ) ;
1918 const { t, selectedLanguage } = useTranslation ( ) ;
2019 const { selectedCurrency } = useCurrency ( ) ;
20+
2121 const [ product ] = useResolve (
2222 async ( ) => loadProductBySlug ( productSlug , selectedLanguage , selectedCurrency ) ,
2323 [ productSlug , selectedLanguage , selectedCurrency ]
2424 ) ;
2525
26+ const productImageHrefs = useProductImages ( product ) ;
27+ const [ currentImageIndex , setCurrentImageIndex ] = useState ( 0 ) ;
28+ const isPrevImageVisible = currentImageIndex > 0 ;
29+ const isNextImageVisible = currentImageIndex < ( productImageHrefs ?. length ?? 0 ) - 1 ;
30+ const productBackground = product ?. background_color ?? '' ;
31+
32+ const handlePrevImageClicked = ( ) => {
33+ setCurrentImageIndex ( currentImageIndex - 1 ) ;
34+ } ;
35+
36+ const handleNextImageClicked = ( ) => {
37+ setCurrentImageIndex ( currentImageIndex + 1 ) ;
38+ } ;
39+
2640 return (
2741 < div className = "product" >
2842 { product && (
2943 < div className = "product__maincontainer" >
3044 < div className = "product__imgcontainer" >
31- < ProductMainImage product = { product } size = { 400 } />
45+ { productImageHrefs . length > 0 && (
46+ < >
47+ < img className = "product__img" src = { productImageHrefs ?. [ currentImageIndex ] } alt = { product . name } style = { { backgroundColor : productBackground } } />
48+ { isPrevImageVisible && (
49+ < button className = "product__previmagebtn" aria-label = { t ( 'previous-image' ) } onClick = { handlePrevImageClicked } >
50+ < svg fill = "none" strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" viewBox = "0 0 24 24" stroke = "currentColor" > < path d = "M15 19l-7-7 7-7" > </ path > </ svg >
51+ </ button >
52+ ) }
53+ { isNextImageVisible && (
54+ < button className = "product__nextimagebtn" aria-label = { t ( 'next-image' ) } onClick = { handleNextImageClicked } >
55+ < svg fill = "none" strokeLinecap = "round" strokeLinejoin = "round" strokeWidth = "2" viewBox = "0 0 24 24" stroke = "currentColor" > < path d = "M9 5l7 7-7 7" > </ path > </ svg >
56+ </ button >
57+ ) }
58+ </ >
59+ ) }
3260 </ div >
3361 < div className = "product__details" >
3462 < h1 className = "product__name" >
0 commit comments