Skip to content

Commit

Permalink
refactor(apps/web): add useQueryParams hook
Browse files Browse the repository at this point in the history
  • Loading branch information
dandheedge committed Feb 12, 2024
1 parent 8493182 commit e12bd48
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions apps/web/src/hooks/usePaginationParams.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { pathOr } from "ramda";
import { useCallback } from "react";

import { useQueryParams } from "./useQueryParams";
export const limitBounds = {
"10": 10,
"20": 20,
Expand All @@ -19,29 +19,26 @@ export const usePaginationParams = (): UsePaginationReturn => {
const searchParams = useSearchParams();
const router = useRouter();
const pathName = usePathname();
const { query } = useQueryParams();
const urlSearchParams = new URLSearchParams(searchParams);
const pg = parseInt(urlSearchParams.get("pg") ?? "");
const lt = urlSearchParams.get("lt") ?? limitBounds[10];
const limit = pathOr(limitBounds[10], [lt], limitBounds);
const page = isNaN(pg) ? 1 : pg;
const query = urlSearchParams.get("query") ?? "";

const updateParams = useCallback(
(page: number, limit: number): void => {
const urlSearchParams = new URLSearchParams({
query,
pg: page.toString(),
lt: limit.toString(),
});
if (query !== "") {
urlSearchParams.append("query", query);
}

router.push(`${pathName}?${urlSearchParams.toString()}`, {
scroll: false,
});
},
[router, pathName],
);

return [{ page, limit }, updateParams];
};

0 comments on commit e12bd48

Please sign in to comment.