Skip to content

Commit

Permalink
feat: sync search using atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Nov 21, 2024
1 parent 77f3e0d commit 8f90f27
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/app/explorer/atoms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { atom } from "jotai";

export const searchAtom = atom<string | undefined>(undefined);
12 changes: 7 additions & 5 deletions src/app/explorer/components/InfiniteScrollGuilds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import { Button } from "@/components/ui/Button";
import { env } from "@/lib/env";
import { useInfiniteQuery } from "@tanstack/react-query";
import { useIntersection } from "foxact/use-intersection";
import { useSearchParams } from "next/navigation";
import { useAtomValue } from "jotai";
import { useCallback, useEffect } from "react";
import { searchAtom } from "../atoms";
import { GuildCard } from "./GuildCard";

const pageSize = 24;

export const InfiniteScrollGuilds = () => {
const searchParams = useSearchParams();
const search = useAtomValue(searchAtom);
const fetchGuilds = useCallback(
async ({ pageParam }: { pageParam: number }) =>
(
await fetch(
`${env.NEXT_PUBLIC_API}/guild/search?page=${pageParam}&pageSize=${pageSize}&search=${searchParams?.get("search") || ""}`,
`${env.NEXT_PUBLIC_API}/guild/search?page=${pageParam}&pageSize=${pageSize}&search=${search}`,
)
).json() as Promise<PaginatedResponse<Guild>>,
[searchParams],
[search],
);

const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
useInfiniteQuery({
queryKey: ["guilds", searchParams.toString()],
queryKey: ["guilds", search],
queryFn: fetchGuilds,
initialPageParam: 1,
enabled: search !== undefined,
getNextPageParam: (lastPage) =>
lastPage.total / lastPage.pageSize <= lastPage.page
? undefined
Expand Down
9 changes: 8 additions & 1 deletion src/app/explorer/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import { Input } from "@/components/ui/Input";
import { useDebouncedValue } from "foxact/use-debounced-value";
import { useSetAtom } from "jotai";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useEffect, useState } from "react";
import { searchAtom } from "../atoms";

export const Search = () => {
const router = useRouter();
Expand All @@ -12,7 +14,12 @@ export const Search = () => {
const [value, setValue] = useState(
searchParams?.get("search")?.toString() || "",
);
const _debouncedValue = useDebouncedValue(value, 200);

const debouncedValue = useDebouncedValue(value, 200);
const setSearch = useSetAtom(searchAtom);
useEffect(() => {

Check warning on line 20 in src/app/explorer/components/Search.tsx

View workflow job for this annotation

GitHub Actions / quality-assurance

lint/correctness/useExhaustiveDependencies

This hook does not specify all of its dependencies: setSearch
setSearch(debouncedValue);
}, [debouncedValue]);

useEffect(() => {

Check warning on line 24 in src/app/explorer/components/Search.tsx

View workflow job for this annotation

GitHub Actions / quality-assurance

lint/correctness/useExhaustiveDependencies

This hook does not specify all of its dependencies: pathname

Check warning on line 24 in src/app/explorer/components/Search.tsx

View workflow job for this annotation

GitHub Actions / quality-assurance

lint/correctness/useExhaustiveDependencies

This hook does not specify all of its dependencies: router.replace
const newSearchParams = new URLSearchParams(
Expand Down

0 comments on commit 8f90f27

Please sign in to comment.