Skip to content

Commit 68efeab

Browse files
authored
Merge pull request rescript-lang#139 from fhammerschmidt/packages-search-query
Package index: Add search query parameter.
2 parents 72dd0c3 + b24599a commit 68efeab

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed

bindings/Next.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,18 @@ module Router = {
102102
query: Js.Dict.t<string>,
103103
}
104104

105+
type pathObj = {
106+
pathname: string,
107+
query: Js.Dict.t<string>,
108+
}
109+
105110
@bs.send external push: (router, string) => unit = "push"
111+
@bs.send external pushObj: (router, pathObj) => unit = "push"
106112

107113
@bs.module("next/router") external useRouter: unit => router = "useRouter"
114+
108115
@bs.send external replace: (router, string) => unit = "replace"
116+
@bs.send external replaceObj: (router, pathObj) => unit = "replace"
109117
}
110118

111119
module Head = {

re_pages/Packages.js

Lines changed: 40 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

re_pages/Packages.res

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ let default = (props: props) => {
457457
| value => Filtered(value)
458458
}
459459
})
460-
()
461460
}
462461

463462
let searchValue = switch state {
@@ -516,6 +515,40 @@ let default = (props: props) => {
516515
</Category>
517516
}
518517

518+
let router = Next.Router.useRouter()
519+
520+
// On first render, the router query is undefined so we set a flag.
521+
let firstRenderDone = React.useRef(false)
522+
523+
React.useEffect0(() => {
524+
firstRenderDone.current = true
525+
526+
None
527+
})
528+
529+
// On second render, this hook runs one more time to actually trigger the search.
530+
React.useEffect1(() => {
531+
router.query->Js.Dict.get("search")->Belt.Option.forEach(onValueChange)
532+
533+
None
534+
}, [firstRenderDone.current])
535+
536+
let updateQuery = value =>
537+
router->Next.Router.replaceObj({
538+
pathname: router.pathname,
539+
query: value === "" ? Js.Dict.empty() : Js.Dict.fromArray([("search", value)]),
540+
})
541+
542+
// When the search term changes, update the router query accordingly.
543+
React.useEffect1(() => {
544+
switch state {
545+
| All => updateQuery("")
546+
| Filtered(value) => updateQuery(value)
547+
}
548+
549+
None
550+
}, [state])
551+
519552
let overlayState = React.useState(() => false)
520553
<>
521554
<Meta

0 commit comments

Comments
 (0)