@@ -2,7 +2,7 @@ import React, { useEffect, useState, useContext } from 'react';
22import { useParams } from 'react-router-dom' ;
33import useOnclickOutside from 'react-cool-onclickoutside' ;
44import { useResolve , useProductImages } from './hooks' ;
5- import { addToCart , loadProductBySlug } from './service' ;
5+ import { addToCart , loadProductBySlug , getProductById } from './service' ;
66import { CompareCheck } from './CompareCheck' ;
77import { SocialShare } from './SocialShare' ;
88import {
@@ -23,7 +23,6 @@ import { APIErrorContext } from './APIErrorProvider';
2323
2424import './Product.scss' ;
2525
26-
2726interface ProductParams {
2827 productSlug : string ;
2928}
@@ -61,17 +60,37 @@ export const Product: React.FC = () => {
6160 } ,
6261 [ productSlug , selectedLanguage , selectedCurrency , addError ]
6362 ) ;
63+
64+ const [ childProduct , setChildProduct ] = useState < string > ( product ?. relationships . children ? product . relationships . children . data [ 0 ] . id : "" )
65+
66+ const handleVariationChange = ( childID : string ) => {
67+ setProductId ( childID ) ;
68+ setChildProduct ( childID )
69+ } ;
70+
71+ const [ child ] = useResolve (
72+ async ( ) => {
73+ try {
74+ if ( childProduct . length > 0 )
75+ return getProductById ( childProduct )
76+ }
77+ catch ( error ) {
78+ addError ( error . errors ) ;
79+ }
80+ } , [ childProduct , addError ]
81+ )
82+
6483 const [ productId , setProductId ] = useState ( '' ) ;
6584
6685 useEffect ( ( ) => {
67- product && setProductId ( product . id ) ;
86+ product && setProductId ( product . id ) ;
6887 } , [ product ] ) ;
6988
7089 useEffect ( ( ) => {
7190 document . body . style . overflow = modalOpen ? 'hidden' : 'unset' ;
7291 } , [ modalOpen ] )
7392
74- const productImageHrefs = useProductImages ( product ) ;
93+ const productImageHrefs = useProductImages ( child ? child : product ) ;
7594 const [ currentImageIndex , setCurrentImageIndex ] = useState ( 0 ) ;
7695 const isPrevImageVisible = currentImageIndex > 0 ;
7796 const isNextImageVisible = currentImageIndex < ( productImageHrefs ?. length ?? 0 ) - 1 ;
@@ -81,6 +100,15 @@ export const Product: React.FC = () => {
81100 setCurrentImageIndex ( currentImageIndex - 1 ) ;
82101 } ;
83102
103+ useEffect ( ( ) => {
104+ if ( product )
105+ if ( product . relationships . children ) {
106+ setChildProduct ( product . relationships . children . data [ 0 ] . id ) ;
107+ setProductId ( product . relationships . children . data [ 0 ] . id )
108+
109+ }
110+ } , [ product ] )
111+
84112 const handleAddToCart = ( cartId ?: string ) => {
85113 const currentCart = localStorage . getItem ( "mcart" ) || "" ;
86114 const mcart = cartId ? cartId : currentCart ;
@@ -105,10 +133,6 @@ export const Product: React.FC = () => {
105133 setCurrentImageIndex ( currentImageIndex + 1 ) ;
106134 } ;
107135
108- const handleVariationChange = ( childID : string ) => {
109- setProductId ( childID ) ;
110- } ;
111-
112136 const handleAddToSelectedCart = ( cart :any ) => {
113137 updateSelectedCart ( cart ) ;
114138 handleAddToCart ( cart . id ) ;
@@ -218,14 +242,14 @@ export const Product: React.FC = () => {
218242 { product . name }
219243 </ h1 >
220244 < div className = "product__sku" >
221- { product . sku }
245+ { child ? child . sku : product . sku }
222246 </ div >
223247 < div className = "product__price" >
224- { product . meta . display_price . without_tax . formatted }
248+ { child ? child . meta . display_price . without_tax . formatted : product . meta . display_price . without_tax . formatted }
225249 </ div >
226- < Availability available = { isProductAvailable ( product ) } />
250+ < Availability available = { child ? isProductAvailable ( child ) : isProductAvailable ( product ) } />
227251 < div className = "product__comparecheck" >
228- < CompareCheck product = { product } />
252+ < CompareCheck product = { child ? child : product } />
229253 </ div >
230254 {
231255 product . meta . variations
@@ -238,10 +262,10 @@ export const Product: React.FC = () => {
238262 </ div >
239263 </ div >
240264 < div className = "product__description" >
241- { product . description }
265+ { child ? child . description : product . description }
242266 </ div >
243267 < div className = "product__socialshare" >
244- < SocialShare name = { product . name } description = { product . description } imageHref = { productImageHrefs ?. [ 0 ] } />
268+ < SocialShare name = { child ? child . name : product . name } description = { child ? child . description : product . description } imageHref = { productImageHrefs ?. [ 0 ] } />
245269 </ div >
246270 </ div >
247271 </ div >
0 commit comments