1- import React , { FC , useEffect , useState } from "react" ;
2-
3- import { ProductTransformer } from "@/utils/api/transformer/product" ;
4- import { getData } from "@/utils/api/fetchData/apiCall" ;
5-
6- import { ProductDetailAPI } from "@/const/endPoint" ;
1+ import React , { FC , useState } from "react" ;
72
83import { useCart } from "@/context/cartContext" ;
94
@@ -14,21 +9,32 @@ import Price from "../product/price";
149import { productInfoProps } from "./productInfo.types" ;
1510
1611import styles from "./productInfo.module.scss" ;
12+ import { useFetchProductData } from "@/utils/hooks/api/useFetchProductData" ;
1713
1814const ProductInfo : FC < productInfoProps > = ( {
1915 id,
2016 title,
2117 price,
2218 options,
2319 description,
24- setProduct,
2520 productAttributeId,
2621} ) => {
2722 const [ selectedOption , setSelectedOption ] = useState <
2823 { id : string ; value : string } [ ]
2924 > ( [ ] ) ;
3025 const { addToCart, isLoading } = useCart ( ) ;
3126 const [ quantity , setQuantity ] = useState ( 1 ) ;
27+
28+ const {
29+ data : productData ,
30+ isFetching,
31+ isError,
32+ } = useFetchProductData ( {
33+ productId : id ,
34+ selectedOption,
35+ refresh : true ,
36+ } ) ;
37+
3238 const handleSelectOption = ( id : string , value : string ) => {
3339 const index = selectedOption . findIndex ( ( option ) => option . id === id ) ;
3440 if ( index > - 1 ) {
@@ -39,30 +45,7 @@ const ProductInfo: FC<productInfoProps> = ({
3945 setSelectedOption ( [ ...selectedOption , { id, value } ] ) ;
4046 }
4147 } ;
42- useEffect ( ( ) => {
43- const fetchData = async ( ) => {
44- const resultString =
45- "&" +
46- selectedOption
47- . map ( ( item ) => `group[${ item . id } ]=${ item . value } ` )
48- . join ( "&" ) ;
49- try {
50- const productData = await getData (
51- ProductDetailAPI ,
52- {
53- product_id : id ,
54- refresh : true ,
55- } ,
56- resultString
57- ) ;
58- const transformedData = ProductTransformer ( productData ) ;
59- setProduct ( transformedData ) ;
60- } catch ( error ) {
61- console . error ( "Failed to fetch product data:" , error ) ;
62- }
63- } ;
64- selectedOption . length > 0 && fetchData ( ) ;
65- } , [ selectedOption ] ) ;
48+
6649 const handleAdd = async ( ) => {
6750 const item = {
6851 id,
@@ -72,10 +55,11 @@ const ProductInfo: FC<productInfoProps> = ({
7255 } ;
7356 addToCart ( item ) ;
7457 } ;
58+
7559 return (
7660 < div className = { styles . productInfo } >
77- < h2 className = { styles . productTitle } > { title } </ h2 >
78- < Price price = { price } />
61+ < h2 className = { styles . productTitle } > { productData ?. title || title } </ h2 >
62+ < Price price = { productData ?. price || price } />
7963 { options && (
8064 < Options options = { options } handleSelectOption = { handleSelectOption } />
8165 ) }
@@ -86,7 +70,9 @@ const ProductInfo: FC<productInfoProps> = ({
8670 isLoading = { isLoading }
8771 />
8872 < div
89- dangerouslySetInnerHTML = { { __html : description } }
73+ dangerouslySetInnerHTML = { {
74+ __html : productData ?. description || description ,
75+ } }
9076 className = { styles . description }
9177 />
9278 </ div >
0 commit comments