Skip to content

Commit

Permalink
refactor: move explorer fetchers to server actions
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-stumpf committed Dec 2, 2024
1 parent bf249a8 commit cfc5ed4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
16 changes: 16 additions & 0 deletions src/app/(dashboard)/explorer/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use server";

import { env } from "@/lib/env";
import type { Guild } from "@/lib/schemas/guild";
import type { PaginatedResponse } from "@/lib/types";
import { fetcher } from "../../../lib/fetcher";
import { PAGE_SIZE } from "./constants";

export const getGuildSearch = async ({
pageParam,
search,
}: { pageParam: number; search: string }) => {
return fetcher<PaginatedResponse<Guild>>(
`${env.NEXT_PUBLIC_API}/guild/search?page=${pageParam}&pageSize=${PAGE_SIZE}&search=${search}`,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import { useInfiniteQuery } from "@tanstack/react-query";
import { useIntersection } from "foxact/use-intersection";
import { useAtomValue } from "jotai";
import { useCallback, useEffect } from "react";
import { getGuildSearch } from "../actions";
import { searchAtom } from "../atoms";
import { PAGE_SIZE } from "../constants";
import { getGuildSearch } from "../fetchers";
import { GuildCard, GuildCardSkeleton } from "./GuildCard";

export const InfiniteScrollGuilds = () => {
const search = useAtomValue(searchAtom);
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } =
useInfiniteQuery({
queryKey: ["guilds", search || ""],
queryFn: getGuildSearch(search),
queryFn: ({ pageParam }) =>
getGuildSearch({ search: search || "", pageParam }),
initialPageParam: 1,
staleTime: Number.POSITIVE_INFINITY,
enabled: search !== undefined,
Expand Down
13 changes: 0 additions & 13 deletions src/app/(dashboard)/explorer/fetchers.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/app/(dashboard)/explorer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type { Guild } from "@/lib/schemas/guild";
import type { PaginatedResponse } from "@/lib/types";
import { HydrationBoundary, dehydrate } from "@tanstack/react-query";
import { Suspense } from "react";
import { getGuildSearch } from "./actions";
import { CreateGuildLink } from "./components/CreateGuildLink";
import { GuildCard, GuildCardSkeleton } from "./components/GuildCard";
import { HeaderBackground } from "./components/HeaderBackground";
import { InfiniteScrollGuilds } from "./components/InfiniteScrollGuilds";
import { StickyNavbar } from "./components/StickyNavbar";
import { StickySearch } from "./components/StickySearch";
import { ACTIVE_SECTION } from "./constants";
import { getGuildSearch } from "./fetchers";

const getAssociatedGuilds = async ({ userId }: { userId: string }) => {
const request = `${env.NEXT_PUBLIC_API}/guild/search?page=1&pageSize=${Number.MAX_SAFE_INTEGER}&sortBy=name&reverse=false&customQuery=@owner:{${userId}}`;
Expand All @@ -27,7 +27,7 @@ export default async function Explorer() {
await queryClient.prefetchInfiniteQuery({
queryKey: ["guilds", ""],
initialPageParam: 1,
queryFn: getGuildSearch(""),
queryFn: () => getGuildSearch({ search: "", pageParam: 1 }),
});

return (
Expand Down

0 comments on commit cfc5ed4

Please sign in to comment.