-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 검색 필터링 조건 및 검색어 쿼리 스트링 값으로 반영하여 검색 결과 유지, useSearchPageParams 구현 …
- Loading branch information
Showing
6 changed files
with
141 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ChangeEvent, useCallback, useState } from 'react'; | ||
import { useSearchParams } from 'react-router-dom'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { searchKeywordState } from 'utils/atom'; | ||
|
||
export const useSearchPageParams = () => { | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
const keywordParam = searchParams.get('keyword'); | ||
const typeParam = searchParams.get('searchType'); | ||
const sortParam = searchParams.get('sort'); | ||
const openSortParam = searchParams.get('open-sort'); | ||
const keywordFromPrevPage = useRecoilValue(searchKeywordState); | ||
const initialKeyword = keywordParam ?? keywordFromPrevPage; | ||
const initialSearchType = typeParam ?? 'counselor'; | ||
const initialSortType = | ||
sortParam === 'rating' ? 2 : sortParam === 'popular' ? 1 : 0; | ||
const initialOpenSortType = | ||
openSortParam === 'comments' ? 2 : openSortParam === 'likes' ? 1 : 0; | ||
const initialTabState = typeParam === 'open-consult' ? 2 : 1; | ||
const [input, setInput] = useState(keywordFromPrevPage); | ||
const [keyword, setKeyword] = useState(initialKeyword); | ||
const [searchType, setSearchType] = useState<string>(initialSearchType); | ||
const [openSortType, setOpenSortType] = useState<number>(initialOpenSortType); | ||
const [sortType, setSortType] = useState<number>(initialSortType); | ||
const [tabState, setTabState] = useState<number>(initialTabState); | ||
const handleClickOpenConsult = useCallback(() => { | ||
setSearchType('open-consult'); | ||
searchParams.set('searchType', 'open-consult'); | ||
setTabState(2); | ||
setSearchParams(searchParams); | ||
}, []); | ||
const handleClickCounselor = useCallback(() => { | ||
setSearchType('counselor'); | ||
searchParams.set('searchType', 'counselor'); | ||
setSearchParams(searchParams); | ||
setTabState(1); | ||
console.log('hi'); | ||
}, []); | ||
|
||
const handleClickTab = useCallback( | ||
(tabState: number) => { | ||
if (tabState === 1) { | ||
handleClickCounselor(); | ||
} else { | ||
handleClickOpenConsult(); | ||
} | ||
}, | ||
[tabState], | ||
); | ||
const handleChangeInput = (event: ChangeEvent<HTMLInputElement>) => { | ||
setInput(event.target.value); | ||
}; | ||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
setKeyword(input); | ||
searchParams.set('keyword', input); | ||
setSearchParams(searchParams); | ||
}; | ||
|
||
return { | ||
handleClickTab, | ||
searchParams, | ||
openSortType, | ||
setOpenSortType, | ||
setSearchParams, | ||
input, | ||
tabState, | ||
setTabState, | ||
keyword, | ||
setKeyword, | ||
handleSubmit, | ||
keywordFromPrevPage, | ||
searchType, | ||
sortType, | ||
setSortType, | ||
handleChangeInput, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters