From af3b8a74d1f851c9c754814c900749e5bd6ee4cc Mon Sep 17 00:00:00 2001 From: Mqdd Date: Sun, 15 Feb 2026 01:10:14 +0700 Subject: auto set to popular when search --- src/lib/product/components/ProductSearch.jsx | 76 +++++++++++++++++----------- 1 file changed, 47 insertions(+), 29 deletions(-) diff --git a/src/lib/product/components/ProductSearch.jsx b/src/lib/product/components/ProductSearch.jsx index 850d00cc..c73c7036 100644 --- a/src/lib/product/components/ProductSearch.jsx +++ b/src/lib/product/components/ProductSearch.jsx @@ -6,7 +6,10 @@ import { HStack, Image, Tag, TagCloseButton, TagLabel } from '@chakra-ui/react'; import axios from 'axios'; import _ from 'lodash'; import { toQuery } from 'lodash-contrib'; -import { FunnelIcon, AdjustmentsHorizontalIcon } from '@heroicons/react/24/outline'; +import { + FunnelIcon, + AdjustmentsHorizontalIcon, +} from '@heroicons/react/24/outline'; import odooApi from '@/core/api/odooApi'; import searchSpellApi from '@/core/api/searchSpellApi'; import Link from '@/core/components/elements/Link/Link'; @@ -57,9 +60,15 @@ const ProductSearch = ({ if (!router.isReady) return; const onBrandsPage = router.pathname.includes('brands'); - const hasOrder = typeof router.query?.orderBy === 'string' && router.query.orderBy !== ''; - - if (onBrandsPage && !hasOrder && !appliedDefaultBrandOrder.current) { + const onSearchPage = prefixUrl === '/shop/search'; + const hasOrder = + typeof router.query?.orderBy === 'string' && router.query.orderBy !== ''; + + if ( + (onBrandsPage || onSearchPage) && + !hasOrder && + !appliedDefaultBrandOrder.current + ) { let params = { ...router.query, orderBy: 'popular', @@ -83,7 +92,7 @@ const ProductSearch = ({ const loadProduct = async () => { const getCategoriesId = await odooApi( 'GET', - `/api/v1/category/numFound?parent_id=${categoryId}` + `/api/v1/category/numFound?parent_id=${categoryId}`, ); if (getCategoriesId) { setDataCategoriesProduct(getCategoriesId); @@ -94,7 +103,7 @@ const ProductSearch = ({ const loadProduct = async () => { const lobData = await odooApi( 'GET', - `/api/v1/lob_homepage/${categoryId}/category_id` + `/api/v1/lob_homepage/${categoryId}/category_id`, ); if (lobData) { @@ -175,7 +184,11 @@ const ProductSearch = ({ }, [dataCategoriesProduct, dataLob]); useEffect(() => { - if (prefixUrl.includes('category') || prefixUrl.includes('lob') || router.asPath.includes('penawaran')) { + if ( + prefixUrl.includes('category') || + prefixUrl.includes('lob') || + router.asPath.includes('penawaran') + ) { setQueryFinal({ ...finalQuery, q, limit, orderBy }); } else { setQueryFinal({ ...query, q, limit, orderBy }); @@ -198,10 +211,10 @@ const ProductSearch = ({ ? router.query.brand ? router.query.brand.split(',') : [] - : [] + : [], ); const [categoryValues, setCategory] = useState( - router.query?.category?.split(',') || router.query?.category?.split(',') + router.query?.category?.split(',') || router.query?.category?.split(','), ); const [priceFrom, setPriceFrom] = useState(router.query?.priceFrom || null); @@ -217,11 +230,11 @@ const ProductSearch = ({ if (productFound == 0 && query.q && !spellings) { searchSpellApi({ query: query.q }).then((response) => { const oddIndexSuggestions = response.data.spellcheck.suggestions.filter( - (_, index) => index % 2 === 1 + (_, index) => index % 2 === 1, ); const oddIndexCollations = response.data.spellcheck.collations.filter( - (_, index) => index % 2 === 1 + (_, index) => index % 2 === 1, ); const dataSpellings = oddIndexSuggestions.reduce((acc, curr) => { @@ -246,7 +259,7 @@ const ProductSearch = ({ useEffect(() => { const checkIfBrand = async () => { const brand = await axios( - `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/brands?params=search&q=${search}` + `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/brands?params=search&q=${search}`, ); if (brand.data.length > 0) { @@ -265,7 +278,7 @@ const ProductSearch = ({ const loadCategories = async () => { const getCategories = await odooApi( 'GET', - `/api/v1/category/child?parent_id=${categoryId}` + `/api/v1/category/child?parent_id=${categoryId}`, ); if (getCategories) { setDataCategories(getCategories); @@ -335,15 +348,15 @@ const ProductSearch = ({ if (router.pathname.includes('search')) { const getBannerHeader = await odooApi( 'GET', - '/api/v1/banner?type=promotion-header' + '/api/v1/banner?type=promotion-header', ); const getBannerFooter = await odooApi( 'GET', - '/api/v1/banner?type=promotion-footer' + '/api/v1/banner?type=promotion-footer', ); var randomIndex = Math.floor(Math.random() * getBannerHeader.length); var randomIndexFooter = Math.floor( - Math.random() * getBannerFooter.length + Math.random() * getBannerFooter.length, ); setBannerPromotionHeader(getBannerHeader[randomIndex]); setBannerPromotionFooter(getBannerFooter[randomIndexFooter]); @@ -430,7 +443,9 @@ const ProductSearch = ({
{isNotReadyStockPage && isBrand && isBrand.logo && (
-

Brand Pencarian {q}

+

+ Brand Pencarian {q} +

1 ? ( <> {productStart + 1}- - {parseInt(productStart) + parseInt(productRows) > productFound + {parseInt(productStart) + parseInt(productRows) > + productFound ? productFound : parseInt(productStart) + parseInt(productRows)}  dari  @@ -474,7 +490,8 @@ const ProductSearch = ({  produk{' '} {query.q && ( <> - untuk pencarian {query.q} + untuk pencarian{' '} + {query.q} )} @@ -512,7 +529,9 @@ const ProductSearch = ({
)} {!!dataLob?.length && } - {!!dataCategories?.length && } + {!!dataCategories?.length && ( + + )}
{products && products.map((product) => ( @@ -621,7 +640,7 @@ const ProductSearch = ({ <> {productStart + 1}- {parseInt(productStart) + parseInt(productRows) > - productFound + productFound ? productFound : parseInt(productStart) + parseInt(productRows)}  dari  @@ -629,12 +648,11 @@ const ProductSearch = ({ ) : ( '' )} - {productFound} + {productFound}  produk{' '} {query.q && ( <> - untuk pencarian{' '} - {query.q} + untuk pencarian {query.q} )} @@ -697,8 +715,8 @@ const ProductSearch = ({ href={ query?.q ? whatsappUrl('productSearch', { - name: query.q, - }) + name: query.q, + }) : whatsappUrl() } className='text-danger-500' @@ -783,9 +801,9 @@ const FilterChoicesComponent = ({ )} {brandValues?.length > 0 || - categoryValues?.length > 0 || - priceFrom || - priceTo ? ( + categoryValues?.length > 0 || + priceFrom || + priceTo ? (