-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c574cd
commit 6bae709
Showing
6 changed files
with
75 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use client"; | ||
import React, { useEffect } from "react"; | ||
import { useRouter, useSearchParams } from "next/navigation"; | ||
import { Input } from "@/components/ui/input"; | ||
import { debounce } from "@/utils/debounce"; | ||
const Search = () => { | ||
const [searchQuery, setSearchQuery] = React.useState<string>(); | ||
const router = useRouter(); | ||
const searchParams = useSearchParams(); | ||
const SearchQueryUpdate = debounce((searchQuery) => { | ||
const params = new URLSearchParams(searchParams); | ||
if (searchQuery) { | ||
params.set("search", searchQuery); | ||
} else { | ||
params.delete("search"); | ||
} | ||
router.push(`?${params}`, { scroll: false }); | ||
}, 300); | ||
|
||
// retrieving state from url, could be useful to share the filtered posts | ||
useEffect(() => { | ||
setSearchQuery(searchParams.get("search") || ""); | ||
}, []); | ||
|
||
// calling filter upon change in search keyword | ||
useEffect(() => { | ||
SearchQueryUpdate(searchQuery); | ||
}, [searchQuery]); | ||
return ( | ||
<div> | ||
<Input | ||
className="w-full sm:w-auto" | ||
placeholder="Search Postss" | ||
onChange={(e) => setSearchQuery(e.target.value as string)} | ||
value={searchQuery} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Search; |
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