-
Notifications
You must be signed in to change notification settings - Fork 128
Add basic search functionality #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
7c572c2
3474894
b91a02f
6a44981
53be0bd
7031525
5edc1b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { useCategories } from "../hooks/useCategories"; | ||
| import { useAppContext } from "../contexts/AppContext"; | ||
|
|
||
| const SearchFilters = () => { | ||
| const { category, setCategory } = useAppContext(); | ||
| const { fetchedCategories } = useCategories(); | ||
|
|
||
| return ( | ||
| <div className="search-filters"> | ||
| <select value={category} onChange={(e) => setCategory(e.target.value)}> | ||
| <option value="">All Categories</option> | ||
| {fetchedCategories.map((cat, idx) => ( | ||
| <option key={idx} value={cat}> | ||
| {cat} | ||
| </option> | ||
| ))} | ||
| </select> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default SearchFilters; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,31 @@ | ||
| import { SearchIcon } from "./Icons"; | ||
| import { useState, useCallback } from "react"; | ||
| import { useSearchParams, useNavigate } from "react-router-dom"; | ||
|
|
||
| const SearchInput = () => { | ||
| const navigate = useNavigate(); | ||
| const [searchParams, setSearchParams] = useSearchParams(); | ||
| const [searchValue, setSearchValue] = useState(searchParams.get("q") || ""); | ||
|
|
||
| const debouncedSearch = useCallback( | ||
| debounce((query: string) => { | ||
| if (query) { | ||
| setSearchParams({ q: query }); | ||
| navigate(`/search?q=${encodeURIComponent(query.trim().toLowerCase())}`); | ||
|
||
| } else { | ||
| setSearchParams({}); | ||
| navigate("/"); | ||
| } | ||
| }, 200), | ||
| [setSearchParams, navigate] | ||
| ); | ||
|
|
||
| const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const value = e.target.value; | ||
| setSearchValue(value); | ||
| debouncedSearch(value); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="search-field"> | ||
| <label htmlFor="search"> | ||
|
|
@@ -9,11 +34,25 @@ const SearchInput = () => { | |
| <input | ||
| type="search" | ||
| id="search" | ||
| value={searchValue} | ||
| onChange={handleSearch} | ||
| placeholder="Search here..." | ||
| autoComplete="off" | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| // Debounce utility function | ||
| function debounce<T extends (...args: any[]) => any>( | ||
| func: T, | ||
| wait: number | ||
| ): (...args: Parameters<T>) => void { | ||
| let timeout: ReturnType<typeof setTimeout>; | ||
| return (...args: Parameters<T>) => { | ||
| clearTimeout(timeout); | ||
| timeout = setTimeout(() => func(...args), wait); | ||
| }; | ||
| } | ||
|
|
||
| export default SearchInput; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,10 @@ export const useSnippets = () => { | |
| `/data/${slugify(language.lang)}.json` | ||
| ); | ||
|
|
||
| const fetchedSnippets = data | ||
| ? data.find((item) => item.categoryName === category)?.snippets | ||
| const fetchedSnippets: SnippetType[] = data | ||
|
||
| ? category === "All snippets" | ||
| ? data.flatMap((item) => item.snippets) | ||
| : (data.find((item) => item.categoryName === category)?.snippets ?? []) | ||
| : []; | ||
|
|
||
| return { fetchedSnippets, loading, error }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { useAppContext } from "../contexts/AppContext"; | ||
| import SnippetList from "../components/SnippetList"; | ||
| import Sidebar from "../layouts/Sidebar"; | ||
|
|
||
| const HomePage = () => { | ||
| const { category } = useAppContext(); | ||
|
|
||
| return ( | ||
| <> | ||
| <Sidebar /> | ||
| <section className="flow"> | ||
| <h2 className="section-title"> | ||
| {category ? category : "Select a category"} | ||
| </h2> | ||
| <SnippetList /> | ||
| </section> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default HomePage; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { useSearchParams } from "react-router-dom"; | ||
| import SnippetList from "../components/SnippetList"; | ||
| import Sidebar from "../layouts/Sidebar"; | ||
|
|
||
| const SearchPage = () => { | ||
| const [searchParams] = useSearchParams(); | ||
| const query = searchParams.get("q"); | ||
|
|
||
| return ( | ||
| <> | ||
| <Sidebar /> | ||
| <section className="flow"> | ||
| <h2 className="section-title">Search Results for: {query}</h2> | ||
| <SnippetList query={query} /> | ||
| </section> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default SearchPage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
react-router-domis not used anywhere in the project, it was just added inpackage.json. So I think, it will be better to use the latest version.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure where you're seeing it being added to the package.json.
react-router-domwas part of the initial commit.If you wish to have the dependencies updated, I recommend making a new PR for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you update it in your PR ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was added in the initial commit, but its not being used anywhere. Its just there.
Since you are using it in this PR, you can just make it like its being added for the first time and update it to the latest version.