1
1
"use client" ;
2
- import { useState } from "react " ;
2
+ import { useQueryState } from "nuqs " ;
3
3
import { Country } from "../types/types" ;
4
4
import CountryCard from "./country-card" ;
5
5
import RegionSelect from "./region-select" ;
6
6
import SearchCountry from "./country-search" ;
7
7
import { usePathname } from "next/navigation" ;
8
8
import { FavouriteCountry } from "@prisma/client" ;
9
9
import { ArrowUpDown } from "lucide-react" ;
10
+ import { parseAsString , parseAsStringLiteral } from "nuqs" ;
10
11
11
12
type Props = {
12
13
data : Country [ ] ;
@@ -16,9 +17,12 @@ type Props = {
16
17
} ;
17
18
18
19
const CountryList = ( { data, removeFavourite, countries, userId } : Props ) => {
19
- const [ search , setSearch ] = useState ( "" ) ;
20
- const [ selectedRegion , setSelectedRegion ] = useState ( "all" ) ;
21
- const [ sortDirection , setSortDirection ] = useState < "asc" | "desc" > ( "asc" ) ;
20
+ const [ search , setSearch ] = useQueryState ( 'search' , parseAsString . withDefault ( "" ) ) ;
21
+ const [ selectedRegion , setSelectedRegion ] = useQueryState ( 'region' , parseAsString . withDefault ( "all" ) ) ;
22
+ const [ sortDirection , setSortDirection ] = useQueryState (
23
+ 'sort' ,
24
+ parseAsStringLiteral ( [ 'asc' , 'desc' ] ) . withDefault ( "asc" )
25
+ ) ;
22
26
23
27
const pathname = usePathname ( ) ;
24
28
@@ -40,10 +44,10 @@ const CountryList = ({ data, removeFavourite, countries, userId }: Props) => {
40
44
return (
41
45
< div >
42
46
< div className = "flex flex-wrap md:flex-nowrap justify-between gap-5" >
43
- < SearchCountry search = { search } setSearch = { setSearch } />
47
+ < SearchCountry search = { search || "" } setSearch = { setSearch } />
44
48
45
49
< RegionSelect
46
- selectedRegion = { selectedRegion }
50
+ selectedRegion = { selectedRegion || "all" }
47
51
setSelectedRegion = { setSelectedRegion }
48
52
/>
49
53
</ div >
0 commit comments