import style from '../styles/product-detail.module.css'; import Link from 'next/link'; import { useRouter } from 'next/router'; import { useEffect, useRef, useState, UIEvent } from 'react'; // Import komponen Chakra UI import { Button, Tabs, TabList, TabPanels, Tab, TabPanel, Table, Tbody, Tr, Td, Box, Spinner, Center, Text } from '@chakra-ui/react'; // Import Icons import { AlertTriangle, MessageCircleIcon, Share2Icon, } from 'lucide-react'; import { LazyLoadComponent } from 'react-lazy-load-image-component'; import useDevice from '@/core/hooks/useDevice'; import { getAuth } from '~/libs/auth'; import { whatsappUrl } from '~/libs/whatsappUrl'; import ProductPromoSection from '~/modules/product-promo/components/Section'; import { IProductDetail } from '~/types/product'; import { useProductDetail } from '../stores/useProductDetail'; import AddToWishlist from './AddToWishlist'; import Breadcrumb from './Breadcrumb'; import ProductImage from './Image'; import Information from './Information'; import PriceAction from './PriceAction'; import SimilarBottom from './SimilarBottom'; import SimilarSide from './SimilarSide'; import dynamic from 'next/dynamic'; import { gtagProductDetail } from '@/core/utils/googleTag'; type Props = { product: IProductDetail; }; const RWebShare = dynamic( () => import('react-web-share').then((m) => m.RWebShare), { ssr: false } ); const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST; const ProductDetail = ({ product }: Props) => { const { isDesktop, isMobile } = useDevice(); const router = useRouter(); const [auth, setAuth] = useState(null); // State Data dari Magento const [specs, setSpecs] = useState<{ code: string; label: string; value: string }[]>([]); const [upsellIds, setUpsellIds] = useState([]); const [loadingSpecs, setLoadingSpecs] = useState(false); const [errorSpecs, setErrorSpecs] = useState(false); useEffect(() => { try { setAuth(getAuth() ?? null); } catch {} }, []); const canShare = typeof navigator !== 'undefined' && typeof (navigator as any).share === 'function'; const { setAskAdminUrl, askAdminUrl, activeVariantId, setIsApproval, isApproval, selectedVariant, setSelectedVariant, } = useProductDetail(); useEffect(() => { gtagProductDetail(product); }, [product]); useEffect(() => { const createdAskUrl = whatsappUrl({ template: 'product', payload: { manufacture: product.manufacture.name, productName: product.name, url: process.env.NEXT_PUBLIC_SELF_HOST + router.asPath, }, fallbackUrl: router.asPath, }); setAskAdminUrl(createdAskUrl); }, [router.asPath, product.manufacture.name, product.name, setAskAdminUrl]); // ========================================================================= // LOGIC INISIALISASI VARIANT (HANDLE NAVIGASI) // ========================================================================= useEffect(() => { if (typeof auth === 'object') { setIsApproval(auth?.feature?.soApproval); } const variantInit = product?.variants?.find((variant) => variant.is_in_bu) || product?.variants?.[0]; setSelectedVariant(variantInit); // Reset data Magento saat produk berubah setSpecs([]); setUpsellIds([]); }, [product, auth]); // ========================================================================= // LOGIC FETCH: SPECS & UPSELLS // ========================================================================= useEffect(() => { const fetchMagentoData = async () => { // Gunakan ID Variant (SKU Odoo) const idToFetch = selectedVariant?.id; if (!idToFetch) return; setLoadingSpecs(true); setErrorSpecs(false); try { console.log("Fetching Magento data via Proxy for ID:", idToFetch); const endpoint = `/api/magento-product?sku=${encodeURIComponent(String(idToFetch))}`; const response = await fetch(endpoint, { method: 'GET', headers: { 'Content-Type': 'application/json' } }); if (!response.ok) { console.warn(`Magento API status: ${response.status}`); setSpecs([]); setUpsellIds([]); return; } const data = await response.json(); // 1. Simpan Data Spesifikasi if (data.specs && Array.isArray(data.specs)) { setSpecs(data.specs); } else { setSpecs([]); } // 2. Simpan Data Up-Sells (ID) if (data.upsell_ids && Array.isArray(data.upsell_ids)) { setUpsellIds(data.upsell_ids); } else { setUpsellIds([]); } } catch (error) { console.error("Gagal mengambil data Magento:", error); setErrorSpecs(true); setSpecs([]); setUpsellIds([]); } finally { setLoadingSpecs(false); } }; fetchMagentoData(); }, [selectedVariant, product.id]); // ========================================================================= // HELPER: RENDER SPEC VALUE (SIMPLE TEXT/HTML ONLY) // ========================================================================= const renderSpecValue = (item: { code: string; label: string; value: string }) => { const val = item.value; if (!val) return '-'; // Cek apakah mengandung tag HTML sederhana (

, ,