From 0c745c513765b23ee200eb61ebfe13086a1ea700 Mon Sep 17 00:00:00 2001 From: FIN-IT_AndriFP Date: Tue, 16 Dec 2025 15:57:30 +0700 Subject: (andri) try search with q* solr --- .../components/ProductComparisonModal.tsx | 133 ++++++++------------- 1 file changed, 50 insertions(+), 83 deletions(-) diff --git a/src-migrate/modules/product-detail/components/ProductComparisonModal.tsx b/src-migrate/modules/product-detail/components/ProductComparisonModal.tsx index a58ad5b2..3ee61c9d 100644 --- a/src-migrate/modules/product-detail/components/ProductComparisonModal.tsx +++ b/src-migrate/modules/product-detail/components/ProductComparisonModal.tsx @@ -80,6 +80,7 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant } const targetId = activeItem.id; + // Ambil code dari properti yang tersedia const displayCode = activeItem.default_code || activeItem.code || activeItem.sku || mainProduct.default_code || mainProduct.code; const variantOptions = mainProduct.variants?.map((v: any) => ({ @@ -102,8 +103,8 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant const productSlot1 = { id: targetId, - sku: targetId, // ID untuk API - realCode: displayCode, + sku: targetId, // ID untuk API & Key + realCode: displayCode, // String untuk Tampilan name: mainProduct.name, price: activeItem.price?.price || activeItem.price || mainProduct.lowest_price?.price || 0, image: activeItem.image || mainProduct.image, @@ -130,6 +131,7 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant const fetchSpecs = async () => { setIsLoadingMatrix(true); try { + // Kirim ID ke API const allSkus = validProducts.map(p => p.sku).join(','); const mainSku = validProducts[0]?.sku; @@ -151,7 +153,7 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant }, [products, isOpen]); // =========================================================================== - // 3. SEARCH LOGIC + // 3. SEARCH LOGIC (MATCHED WITH API JSON) // =========================================================================== useEffect(() => { const delayDebounceFn = setTimeout(async () => { @@ -163,16 +165,21 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant setIsSearching(true); try { const attrSetId = selectedVariant?.attribute_set_id || mainProduct?.attribute_set_id; + const params = new URLSearchParams({ source: 'compare', - q: searchQuery, + q: '*', limit: '5', + // Filter kategori aktif agar Apple-to-Apple fq: attrSetId ? `attribute_set_id_i:${attrSetId}` : '' }); - const res = await fetch(`/api/search?${params.toString()}`); + // Gunakan endpoint yang benar: /api/shop/search + const res = await fetch(`/api/shop/search?${params.toString()}`); + if (res.ok) { const data = await res.json(); + // Data sudah CamelCase dari Backend setSearchResults(data.response?.products || []); } else { setSearchResults([]); @@ -210,12 +217,19 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant } }; - const handleAddProduct = (solrProduct: any, slotIndex: number) => { + const handleAddProduct = (searchItem: any, slotIndex: number) => { const newProducts = [...products]; - const idToAdd = solrProduct.product_id_i || solrProduct.id; - const codeToAdd = solrProduct.default_code_s || solrProduct.sku; + // [FIX] MAPPING DARI JSON API ANDA (CAMELCASE) + // JSON: { id: 88019, code: "RX-SP0006", displayName: "...", lowestPrice: { price: ... } } + + const idToAdd = searchItem.id; + const codeToAdd = searchItem.code; // Langsung 'code', bukan 'default_code_s' + const nameToAdd = searchItem.displayName || searchItem.name; + const imageToAdd = searchItem.image; + const priceToAdd = searchItem.lowestPrice?.price || 0; + // Cek Duplikat if (newProducts.find(p => p && String(p.id) === String(idToAdd))) { toast({ title: "Produk sudah ada", status: "warning", position: "top" }); return; @@ -223,17 +237,17 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant newProducts[slotIndex] = { id: idToAdd, - sku: idToAdd, - realCode: codeToAdd, - name: solrProduct.display_name_s || solrProduct.name_s, - price: solrProduct.price_tier1_v2_f || 0, - image: solrProduct.image_s, + sku: idToAdd, // ID untuk API + realCode: codeToAdd, // Code String untuk Tampilan + name: nameToAdd, + price: priceToAdd, + image: imageToAdd, variants: [{ id: idToAdd, code: codeToAdd, - name: solrProduct.name_s, - price: solrProduct.price_tier1_v2_f, - image: solrProduct.image_s + name: nameToAdd, + price: priceToAdd, + image: imageToAdd }] }; @@ -269,17 +283,11 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant - - {/* Cell 1: Kosong */} - - {/* Loop Slot Produk */} {products.map((product, index) => ( {product ? ( - - {/* Tombol Hapus */} {index !== 0 && ( } @@ -287,46 +295,33 @@ const ProductComparisonModal = ({ isOpen, onClose, mainProduct, selectedVariant colorScheme="red" onClick={() => handleRemoveProduct(index)} zIndex={2} /> )} - - {/* Gambar */} {product.name} { (e.target as HTMLImageElement).src = '/images/noimage.jpeg'; }} + alt={product.name} maxH="100%" objectFit="contain" + onError={(e) => { (e.target as HTMLImageElement).src = '/images/no-image-compare.svg'; }} /> - - {/* Info Harga & Nama */} {product.price > 0 ? formatPrice(product.price) : 'Hubungi Admin'} - {/* Margin Bottom agar tidak tertutup dropdown */} {product.name} - - {/* Dropdown Varian */} + - {/* Tombol */} } variant="outline" colorScheme="red" size="sm" />