From 95b27ddb0604fbb4fae130f2d80e5ee2aec6d0fc Mon Sep 17 00:00:00 2001 From: Mqdd Date: Thu, 11 Dec 2025 09:00:03 +0700 Subject: fix --- src/pages/searchkey/[slug].jsx | 94 ++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 64 deletions(-) (limited to 'src/pages/searchkey') diff --git a/src/pages/searchkey/[slug].jsx b/src/pages/searchkey/[slug].jsx index 9cf1df05..3ebf6469 100644 --- a/src/pages/searchkey/[slug].jsx +++ b/src/pages/searchkey/[slug].jsx @@ -3,6 +3,7 @@ import { useRouter } from 'next/router'; import { useEffect, useState } from 'react'; import Seo from '@/core/components/Seo'; import dynamic from 'next/dynamic'; +import { getNameFromSlug } from '@/core/utils/slug'; import { capitalizeEachWord } from '../../utils/capializeFIrstWord'; const BasicLayout = dynamic(() => @@ -12,98 +13,63 @@ const ProductSearch = dynamic(() => import('@/lib/product/components/ProductSearch') ); -// const BASE_URL = process.env.NEXT_PUBLIC_SELF_HOST; -const BASE_URL = 'https://indoteknik.com'; - -export default function KeywordPage() { - const router = useRouter(); +export default function FindPage() { + const route = useRouter(); const [result, setResult] = useState(null); const [query, setQuery] = useState(null); - // Ambil slug dari URL dinamis - const keywordSlug = router?.query?.slug || ''; - const keyword = keywordSlug.replace(/-/g, ' ').toLowerCase(); - const url = BASE_URL + router.asPath.split('?')[0]; - const slugTitle = capitalizeEachWord(keyword); + const slugRaw = route.query.slug || null; + console.log(slugRaw); + + // const cleanKey = slugRaw ? getNameFromSlug(slugRaw) : ''; + // console.log(cleanKey); + const readableSlug = capitalizeEachWord(slugRaw); - // Fetch info dari Solr index "url_category_brand" - const getUrls = async (url) => { + const getSearchKeyData = async (clean) => { try { - const response = await axios( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/url-category_brand?url=${url}` + const res = await axios( + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/searchkey?url=${clean}&from=searchkey` ); - const data = response?.data?.response?.docs[0] || null; - setResult(data); - console.log('[🔍 result from API]', data); // Tambahin ini - } catch (error) { - console.error('Error fetching data:', error); + + setResult(res?.data?.response?.docs?.[0] || null); + } catch (e) { + console.error('Fetching searchkey failed:', e); } }; - // Panggil fetch setelah router siap useEffect(() => { - if (router.isReady) { - getUrls(url); - } - }, [router.isReady]); + if (!route.isReady) return; + if (!slugRaw) return; + + getSearchKeyData(slugRaw); + }, [route.isReady, slugRaw]); useEffect(() => { if (result) { - let fqParts = []; - - if (result.category_id_i) { - fqParts.push(`category_parent_ids:${result.category_id_i}`); - } - - if (result.brand_id_i) { - fqParts.push(`manufacture_id_i:${result.brand_id_i}`); - } - - const fq = fqParts.join(' AND '); - const q = keyword || '*:*'; - - console.log('SOLR QUERY:', { q, fq }); + const ids = result.product_ids_is || []; setQuery({ - fq, - q, + ids: ids.join(','), from: 'searchkey', }); } - }, [result, keyword]); - - // if (!result) { - // return ( - // - // - //
- //

Produk tidak ditemukan berdasarkan keyword

- //
- //
- // ); - // } + }, [result]); return ( - {query && ( - - )} + + {query && } ); } -- cgit v1.2.3