diff options
| -rw-r--r-- | src/lib/product/components/ProductSearch.jsx | 76 |
1 files 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 = ({ <div className='p-4 pt-0'> {isNotReadyStockPage && isBrand && isBrand.logo && ( <div className='mb-3'> - <h1 className='mb-2 font-semibold text-h-sm'>Brand Pencarian {q}</h1> + <h1 className='mb-2 font-semibold text-h-sm'> + Brand Pencarian {q} + </h1> <Link href={createSlug('/shop/brands/', isBrand.name, isBrand.id)} className='inline' @@ -462,7 +477,8 @@ const ProductSearch = ({ {pageCount > 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 <span className='font-semibold'>{query.q}</span> + untuk pencarian{' '} + <span className='font-semibold'>{query.q}</span> </> )} </> @@ -512,7 +529,9 @@ const ProductSearch = ({ </div> )} {!!dataLob?.length && <LobSectionCategory categories={dataLob} />} - {!!dataCategories?.length && <CategorySection categories={dataCategories} />} + {!!dataCategories?.length && ( + <CategorySection categories={dataCategories} /> + )} <div className='grid grid-cols-2 gap-3'> {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} + <strong>{productFound}</strong> produk{' '} {query.q && ( <> - untuk pencarian{' '} - <span className='font-semibold'>{query.q}</span> + untuk pencarian <strong>{query.q}</strong> </> )} </> @@ -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 = ({ </Tag> )} {brandValues?.length > 0 || - categoryValues?.length > 0 || - priceFrom || - priceTo ? ( + categoryValues?.length > 0 || + priceFrom || + priceTo ? ( <span> <button className='btn-transparent py-2 px-5 h-[40px] text-red-700' |
