From 5808e82529933aee7c63ede955f64028fd1f38ee Mon Sep 17 00:00:00 2001 From: Mqdd Date: Tue, 16 Dec 2025 14:34:47 +0700 Subject: fix bug & add category --- src/pages/api/shop/searchkey.js | 31 +++++++++++++++------------ src/pages/searchkey/[slug].jsx | 47 +++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/pages/api/shop/searchkey.js b/src/pages/api/shop/searchkey.js index 2735e72c..f5546a36 100644 --- a/src/pages/api/shop/searchkey.js +++ b/src/pages/api/shop/searchkey.js @@ -1,19 +1,23 @@ import axios from 'axios'; export default async function handler(req, res) { - const { url = '', page = 1, limit = 30 } = req.query; + const { url = '', page = 1, limit = 30, all } = req.query; let q = '*:*'; - if (!req.query.all) { - const url = (req.query.q || '').trim(); - q = `keywords_s:"${url}"`; + // ✅ kalau BUKAN sitemap + if (!all) { + const cleanUrl = url.trim(); + if (!cleanUrl) { + return res.status(400).json({ error: 'Missing url param' }); + } + q = `keywords_s:"${cleanUrl}"`; } - let offset = (page - 1) * limit; + + const offset = (page - 1) * limit; const params = [ `q.op=AND`, - // `q=keywords_s:"${url}"`, `q=${q}`, `indent=true`, `rows=${limit}`, @@ -21,18 +25,19 @@ export default async function handler(req, res) { ]; try { - // let result = await axios( - // process.env.SOLR_HOST + `/solr/searchkey/select?` + params.join('&') - // ); - let result = await axios.post( - process.env.SOLR_HOST + `/solr/searchkey/select`, + const result = await axios.post( + `${process.env.SOLR_HOST}/solr/searchkey/select`, params.join('&'), - { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + } ); - console.log(result.data); res.status(200).json(result.data); } catch (error) { + console.error(error?.response?.data || error); res.status(500).json({ error: 'Internal Server Error' }); } } diff --git a/src/pages/searchkey/[slug].jsx b/src/pages/searchkey/[slug].jsx index 3ebf6469..4a6923ff 100644 --- a/src/pages/searchkey/[slug].jsx +++ b/src/pages/searchkey/[slug].jsx @@ -3,9 +3,11 @@ 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'; +// ✅ Breadcrumb = default export +import Breadcrumb from '@/lib/category/components/Breadcrumb'; + const BasicLayout = dynamic(() => import('@/core/components/layouts/BasicLayout') ); @@ -15,20 +17,19 @@ const ProductSearch = dynamic(() => export default function FindPage() { const route = useRouter(); + const [result, setResult] = useState(null); const [query, setQuery] = useState(null); + const [categoryId, setCategoryId] = useState(null); const slugRaw = route.query.slug || null; - console.log(slugRaw); - - // const cleanKey = slugRaw ? getNameFromSlug(slugRaw) : ''; - // console.log(cleanKey); const readableSlug = capitalizeEachWord(slugRaw); - const getSearchKeyData = async (clean) => { + // 🔹 Fetch searchkey dari Solr + const getSearchKeyData = async (slug) => { try { const res = await axios( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/searchkey?url=${clean}&from=searchkey` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/searchkey?url=${slug}&from=searchkey` ); setResult(res?.data?.response?.docs?.[0] || null); @@ -37,21 +38,31 @@ export default function FindPage() { } }; + // 🔹 Trigger fetch saat slug siap useEffect(() => { - if (!route.isReady) return; - if (!slugRaw) return; - + if (!route.isReady || !slugRaw) return; getSearchKeyData(slugRaw); }, [route.isReady, slugRaw]); + // 🔹 Ambil product_ids + categoryId dari Solr useEffect(() => { - if (result) { - const ids = result.product_ids_is || []; + if (!result) return; - setQuery({ - ids: ids.join(','), - from: 'searchkey', - }); + // product search + const ids = result.product_ids_is || []; + setQuery({ + ids: ids.join(','), + from: 'searchkey', + }); + + // breadcrumb category + const catId = + result.category_id_i || + result.public_categ_id_i || + (result.category_ids_is && result.category_ids_is[0]); + + if (catId) { + setCategoryId(catId); } }, [result]); @@ -69,6 +80,10 @@ export default function FindPage() { canonical={`${process.env.NEXT_PUBLIC_SELF_HOST}${route.asPath}`} /> + {/* ✅ Breadcrumb (auto fetch via component) */} + {categoryId && } + + {/* ✅ Product result */} {query && } ); -- cgit v1.2.3