Skip to content

Commit

Permalink
KL-176/refactor: seaprate initializeSearchState to util
Browse files Browse the repository at this point in the history
  • Loading branch information
seoulyego committed Oct 17, 2024
1 parent 9696c81 commit 9ad09e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
17 changes: 1 addition & 16 deletions src/hooks/useDebouncedSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import router from '@/router'
import theme from '@styles/theme'
import { modalIndex } from '@constants/navIndex'
import kyInstance from '@utils/kyInstance'
import initializeSearchState from '@utils/initializeSearchState'

const SearchMapping = {
products: '리뷰',
Expand All @@ -15,20 +16,8 @@ const SearchMapping = {
countries: '국가',
}

const initializeSearchState = (searchedCategory, searchedContent) => {
const result = {
countries: [],
cities: [],
categories: [],
subcategories: [],
}
result[searchedCategory].push(searchedContent)
return result
}

const useDebouncedSearch = (setModalState) => {
const [results, setResults] = useState([])

const debouncedSearch = useCallback(
debounce(async (inputValue) => {
if (inputValue === '') {
Expand All @@ -44,7 +33,6 @@ const useDebouncedSearch = (setModalState) => {
if (result.length === 0) return null

const searchValues = []

searchValues.push(
<Divider
key={category}
Expand Down Expand Up @@ -81,19 +69,16 @@ const useDebouncedSearch = (setModalState) => {
</ResultRow>
)
)

return searchValues
}
)

setResults(newResult)
} catch (error) {
setResults([<ResultRow>오류가 발생했습니다</ResultRow>])
}
}, 150),
[]
)

return [results, debouncedSearch]
}

Expand Down
12 changes: 12 additions & 0 deletions src/utils/initializeSearchState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const initializeSearchState = (searchedCategory, searchedContent) => {
const result = {
countries: [],
cities: [],
categories: [],
subcategories: [],
}
result[searchedCategory].push(searchedContent)
return result
}

export default initializeSearchState

0 comments on commit 9ad09e6

Please sign in to comment.