File tree Expand file tree Collapse file tree 2 files changed +41
-3
lines changed
src/domains/recipe/components/main Expand file tree Collapse file tree 2 files changed +41
-3
lines changed Original file line number Diff line number Diff line change 1+ import { getApi } from '@/app/api/config/appConfig' ;
12import SelectBox from '@/shared/components/select-box/SelectBox' ;
3+ import { useRouter , useSearchParams } from 'next/navigation' ;
4+ import { Dispatch , SetStateAction } from 'react' ;
5+ import { Cocktail } from '../../types/types' ;
26
3- function CocktailFilter ( { cocktailsEA } : { cocktailsEA : string } ) {
7+ interface Props {
8+ cocktailsEA : string ;
9+ setData : Dispatch < SetStateAction < Cocktail [ ] > > ;
10+ }
11+
12+ function CocktailFilter ( { cocktailsEA, setData } :Props ) {
13+
14+ const sortMap = {
15+ 최신순 : 'recent' ,
16+ 인기순 : 'popular' ,
17+ 댓글순 : 'comments' ,
18+ }
19+ const searchParams = useSearchParams ( ) ;
20+ const query = searchParams . get ( 'sortBy' ) ;
21+ const router = useRouter ( ) ;
22+ const handleChange = async ( selectTitle : string ) => {
23+ if ( ! query ) return ;
24+ try {
25+ const res = await fetch ( `${ getApi } /cocktails` )
26+ const json = await res . json ( )
27+ setData ( json . data )
28+ } catch {
29+ console . error ( )
30+ console . log ( selectTitle )
31+ }
32+ } ;
33+
434 return (
535 < div className = "h-10 flex justify-between items-center mt-3 border-b-1 border-gray-light" >
636 < p > { cocktailsEA } 개</ p >
7- < SelectBox option = { [ '최신순' , '댓글순' , '인기순' ] } title = "최신순" />
37+ < SelectBox
38+ option = { [ '최신순' , '댓글순' , '인기순' ] }
39+ title = "최신순"
40+ onChange = { ( value ) => {
41+ const sortValue = sortMap [ value as keyof typeof sortMap ] ;
42+ handleChange ( value ) ;
43+ router . push ( `?sortBy=${ sortValue } ` ) ;
44+ } }
45+ />
846 </ div >
947 ) ;
1048}
Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ function Cocktails() {
8080 < CocktailSearchBar value = { inputValue } onChange = { onInputChange } />
8181 </ div >
8282
83- < CocktailFilter cocktailsEA = { countLabel } />
83+ < CocktailFilter cocktailsEA = { countLabel } setData = { setData } />
8484
8585 < section className = "mt-5" >
8686 { isSearching && noResults ? (
You can’t perform that action at this time.
0 commit comments