From 7ada2eab1773629a3f74fe1a16ba78227953f912 Mon Sep 17 00:00:00 2001
From: "HATEC\\SPVDEV001" <tri.susilo@altama.co.id>
Date: Sat, 14 Oct 2023 09:03:10 +0700
Subject: add fillterby brand di kolom pencarian

---
 src/core/components/elements/Navbar/Search.jsx | 42 +++++++++++++++++++++++---
 1 file changed, 38 insertions(+), 4 deletions(-)

(limited to 'src/core/components/elements/Navbar/Search.jsx')

diff --git a/src/core/components/elements/Navbar/Search.jsx b/src/core/components/elements/Navbar/Search.jsx
index f4a8ab3a..e4f89103 100644
--- a/src/core/components/elements/Navbar/Search.jsx
+++ b/src/core/components/elements/Navbar/Search.jsx
@@ -3,12 +3,18 @@ import { MagnifyingGlassIcon } from '@heroicons/react/24/outline'
 import { useCallback, useEffect, useRef, useState } from 'react'
 import Link from '../Link/Link'
 import { useRouter } from 'next/router'
+import { getIdFromSlug, getNameFromSlug } from '@/core/utils/slug'
 
 const Search = () => {
   const router = useRouter()
   const queryRef = useRef()
+  const { slug = '' } = router.query
   const [query, setQuery] = useState(router.query.q || '')
   const [suggestions, setSuggestions] = useState([])
+  const [segment, setSegment] = useState(null)
+  const [optionSegment, setOptionSegment] = useState(null)
+  const [optoinSelected, setOptionSelected] = useState(getNameFromSlug(slug) || 'default')
+  const [pathSegments, setPathSegments] = useState(router?.asPath.split('/') || [])
 
   const loadSuggestion = useCallback(() => {
     if (query && document.activeElement == queryRef.current) {
@@ -26,14 +32,29 @@ const Search = () => {
     } else {
       setSuggestions([])
     }
+    setFilterSearch()
   }, [loadSuggestion, query])
 
   const handleSubmit = (e) => {
     e.preventDefault()
-    if (query) {
-      router.push(`/shop/search?q=${query}`)
+    if (optionSegment && optoinSelected !== 'default' && optoinSelected) {
+      router.push(`/shop/${segment}/${slug}?q=${query}`)
     } else {
-      queryRef.current.focus()
+      if (query) {
+        router.push(`/shop/search?q=${query}`)
+      } else {
+        queryRef.current.focus()
+      }
+    }
+  }
+
+  const handleSelectChange = async (e) => {
+    await setOptionSelected(e.target.value)
+  }
+  const setFilterSearch = async () => {
+    if (router.pathname.includes('brands') && pathSegments[3]) {
+      await setSegment(pathSegments[2])
+      await setOptionSegment(getNameFromSlug(slug))
     }
   }
 
@@ -46,10 +67,23 @@ const Search = () => {
   return (
     <>
       <form onSubmit={handleSubmit} className='flex-1 flex relative'>
+        {segment && (
+          <select
+            value={optoinSelected}
+            onChange={handleSelectChange}
+            className='form-select p-3 rounded-l-sm bg-white border border-gray_r-6 border-r-0 capitalize'
+          >
+            <option value='default'>Di Indoteknik</option>
+            <option value={optionSegment}>Di {optionSegment}</option>
+          </select>
+        )}
+
         <input
           type='text'
           ref={queryRef}
-          className='form-input p-3 rounded-r-none border-r-0 focus:border-gray_r-6'
+          className={`form-input p-3 ${
+            segment ? 'rounded-l-none border-l-0' : ''
+          } rounded-r-none border-r-0 focus:border-gray_r-6`}
           placeholder='Ketik nama, part number, merk'
           value={query}
           onChange={(e) => setQuery(e.target.value)}
-- 
cgit v1.2.3

