Skip to content

Commit

Permalink
Merge pull request #3831 from owid/larsyencken/persist-search-query
Browse files Browse the repository at this point in the history
Persist search query in URL parameters
  • Loading branch information
larsyencken authored Aug 7, 2024
2 parents 470466c + eb4f5bb commit 212c223
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions adminSiteClient/ChartIndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class ChartIndexPage extends React.Component {

@action.bound onSearchInput(input: string) {
this.searchInput = input
this.setSearchInputInUrl(input)
}

@action.bound onShowMore() {
Expand Down Expand Up @@ -115,6 +116,23 @@ export class ChartIndexPage extends React.Component {
}

componentDidMount() {
this.searchInput = this.getSearchInputFromUrl()
void this.getData()
}

getSearchInputFromUrl(): string {
const params = new URLSearchParams(window.location.search)
return params.get("search") || ""
}

setSearchInputInUrl(searchInput: string) {
const params = new URLSearchParams(window.location.search)
if (searchInput) {
params.set("search", searchInput)
} else {
params.delete("search")
}
const newUrl = `${window.location.pathname}?${params.toString()}`
window.history.replaceState({}, "", newUrl)
}
}

0 comments on commit 212c223

Please sign in to comment.