Skip to content

Commit

Permalink
Fix useTopContributors hook
Browse files Browse the repository at this point in the history
  • Loading branch information
foxyblocks committed Jul 20, 2023
1 parent 25fd6fb commit 64a7d15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions lib/hooks/useFetchTopContributors.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import useSWR, { Fetcher } from "swr";
import { useState } from "react";
import publicApiFetcher from "lib/utils/public-api-fetcher";

type TopContributorsResponse = {
data: { login: string }[];
meta: Meta;
};

const useFetchTopContributors = () => {
const defaultOptions = {
limit: 10,
};

const useFetchTopContributors = (options: typeof defaultOptions) => {
const { limit } = { ...defaultOptions, ...options };
const [page, setPage] = useState(1);

const pageQuery = page ? `page=${page}` : "";
const limitQuery = limit ? `&limit=${limit}` : "";

const { data, error, mutate } = useSWR<TopContributorsResponse, Error>(
"users/top",
`users/top?${pageQuery}${limitQuery}`,
publicApiFetcher as Fetcher<TopContributorsResponse, Error>
);

return {
data: data?.data ?? [],
meta: data?.meta ?? { itemCount: 0, limit: 0, page: 0, hasNextPage: false, hasPreviousPage: false, pageCount: 0 },
isLoading: !error && !data,
isError: !!error,
setPage,
mutate,
};
};
Expand Down
2 changes: 1 addition & 1 deletion pages/404.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { getAvatarByUsername } from "lib/utils/github";
import BubbleBG from "../img/bubble-bg.svg";

export default function Custom404() {
const { data } = useFetchTopContributors();
const { data } = useFetchTopContributors({ limit: 20 });
const [cards, setCards] = useState<DevCardProps[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(true);
const [initialCardIndex, setInitialCardIndex] = useState<number | undefined>();
Expand Down

0 comments on commit 64a7d15

Please sign in to comment.