From 18c0e58faad99e5725ce3864e22df38c3d90c6c1 Mon Sep 17 00:00:00 2001 From: Neven Dyulgerov Date: Mon, 16 Dec 2024 18:05:23 +0200 Subject: [PATCH] feat(apps/web): Remove usePaginationParams and useQueryParams custom hooks --- apps/web/src/hooks/usePaginationParams.tsx | 46 --------------------- apps/web/test/hooks/useQueryParams.test.tsx | 41 ------------------ 2 files changed, 87 deletions(-) delete mode 100644 apps/web/src/hooks/usePaginationParams.tsx delete mode 100644 apps/web/test/hooks/useQueryParams.test.tsx diff --git a/apps/web/src/hooks/usePaginationParams.tsx b/apps/web/src/hooks/usePaginationParams.tsx deleted file mode 100644 index 2a173682d..000000000 --- a/apps/web/src/hooks/usePaginationParams.tsx +++ /dev/null @@ -1,46 +0,0 @@ -import { usePathname, useRouter, useSearchParams } from "next/navigation"; -import { pathOr } from "ramda"; -import { useCallback, useMemo } from "react"; -import { useQueryParams } from "./useQueryParams"; - -export const limitBounds = { - "10": 10, - "20": 20, - "30": 30, -} as const; - -export type LimitBound = (typeof limitBounds)[keyof typeof limitBounds]; - -export type UsePaginationReturn = [ - { limit: LimitBound; page: number }, - (page: number, limit: LimitBound) => void, -]; - -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 updateParams = useCallback( - (page: number, limit: number): void => { - const urlSearchParams = new URLSearchParams({ - query: query.toString(), - pg: page.toString(), - lt: limit.toString(), - }); - router.push(`${pathName}?${urlSearchParams.toString()}`, { - scroll: false, - }); - }, - [query, router, pathName], - ); - return useMemo( - () => [{ page, limit }, updateParams], - [limit, page, updateParams], - ); -}; diff --git a/apps/web/test/hooks/useQueryParams.test.tsx b/apps/web/test/hooks/useQueryParams.test.tsx deleted file mode 100644 index 41df8e559..000000000 --- a/apps/web/test/hooks/useQueryParams.test.tsx +++ /dev/null @@ -1,41 +0,0 @@ -import { renderHook } from "@testing-library/react"; -import { ReadonlyURLSearchParams, useSearchParams } from "next/navigation"; -import { describe, expect, it, vi } from "vitest"; -import { useQueryParams } from "../../src/hooks/useQueryParams"; - -vi.mock("next/navigation", async () => { - const navigation = await vi.importActual("next/navigation"); - return { - ...(navigation as any), - usePathname: vi.fn(() => "/"), - useRouter: () => ({ - push: vi.fn(), - }), - useSearchParams: vi.fn(() => new URLSearchParams()), - }; -}); - -const queryAddress = "0xdd1f9B83507327f29C2C1Bb42011faD5fb482dc6"; -const mockedUseSearchParams = vi.mocked(useSearchParams); - -describe("useQueryParams", () => { - beforeEach(() => { - // Reset the mock before each test - vi.restoreAllMocks(); - }); - - it("should return empty query on the first render", () => { - const { result } = renderHook(() => useQueryParams()); - expect(result.current.query).toBe(""); - }); - - it("should return the correct query address", () => { - mockedUseSearchParams.mockReturnValue( - new URLSearchParams( - `?query=${queryAddress}`, - ) as ReadonlyURLSearchParams, - ); - const { result } = renderHook(() => useQueryParams()); - expect(result.current.query).toBe(queryAddress); - }); -});