-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show and load filters from URL (#23)
* Add param filter utils * Use param filter utils
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { ColumnFiltersState } from "@tanstack/react-table"; | ||
import { ReadonlyURLSearchParams } from "next/navigation"; | ||
|
||
export const getFiltersFromParams = (params: ReadonlyURLSearchParams) => { | ||
let filters: ColumnFiltersState = []; | ||
|
||
const operationName = params.get("operationName"); | ||
operationName && filters.push({ id: "operationName", value: operationName }); | ||
|
||
const tags = params.get("tags"); | ||
if (tags) { | ||
try { | ||
const tagsMap: Map<string, string> = new Map(JSON.parse(tags)); | ||
filters.push({ id: "tags", value: tagsMap }); | ||
} catch {} | ||
} | ||
|
||
return filters; | ||
}; | ||
|
||
export const setParamsFromFilters = (filters: ColumnFiltersState) => { | ||
const params = new URLSearchParams(); | ||
|
||
const operationName = | ||
(filters.find((filter) => filter.id === "operationName")?.value as | ||
| string | ||
| undefined) ?? undefined; | ||
|
||
const tags = | ||
(filters.find((filter) => filter.id === "tags")?.value as | ||
| Map<string, string> | ||
| undefined) ?? undefined; | ||
|
||
operationName | ||
? params.set("operationName", operationName) | ||
: params.delete("operationName"); | ||
tags | ||
? params.set("tags", JSON.stringify(Array.from(tags))) | ||
: params.delete("tags"); | ||
|
||
window.history.replaceState(null, "", `?${params.toString()}`); | ||
}; |