-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version2: Add certificate search logic (#167)
* Add certificate search logic * Fix useSearch * Fix search input * Fix search input state --------- Co-authored-by: alex-slobodian <[email protected]>
- Loading branch information
1 parent
86be410
commit dc0a890
Showing
5 changed files
with
69 additions
and
32 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 @@ | ||
export * from "./useSearchList"; |
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,52 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { CertificateProps } from "../../types"; | ||
|
||
export function useSearchList(certificates: CertificateProps[]) { | ||
const [searchedText, setSearchedText] = useState( | ||
new URLSearchParams(window.location.search).get("search") || "" | ||
); | ||
|
||
const handleSearch = (text: string) => { | ||
const url = new URL(window.location.href); | ||
|
||
if (text?.length) { | ||
url.searchParams.set("search", text); | ||
} else { | ||
url.searchParams.delete("search"); | ||
} | ||
|
||
window.history.pushState(null, "", url); | ||
setSearchedText(text); | ||
}; | ||
|
||
useEffect(() => { | ||
const handleUrlChange = () => { | ||
const searchParams = new URLSearchParams(window.location.search); | ||
setSearchedText(searchParams.get("search") || ""); | ||
}; | ||
|
||
window.addEventListener("popstate", handleUrlChange); | ||
|
||
return () => { | ||
window.removeEventListener("popstate", handleUrlChange); | ||
}; | ||
}, []); | ||
|
||
const list = useMemo( | ||
() => | ||
searchedText | ||
? certificates.filter(({ label }) => | ||
label | ||
?.toLocaleLowerCase() | ||
.includes(searchedText.toLocaleLowerCase()) | ||
) | ||
: certificates, | ||
[searchedText, certificates] | ||
); | ||
|
||
return { | ||
searchedText, | ||
list, | ||
handleSearch, | ||
}; | ||
} |