From 64dd2e745514dfb39b13f7fd1b34418ce2459c56 Mon Sep 17 00:00:00 2001 From: hugobbi Date: Sat, 22 Jun 2024 14:00:08 -0300 Subject: [PATCH 1/2] fix: increasing number of contributors per page in github api call --- src/hooks/useGithubContributors/useGithubContributors.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useGithubContributors/useGithubContributors.tsx b/src/hooks/useGithubContributors/useGithubContributors.tsx index 71f7d46c..7f994448 100644 --- a/src/hooks/useGithubContributors/useGithubContributors.tsx +++ b/src/hooks/useGithubContributors/useGithubContributors.tsx @@ -12,7 +12,7 @@ function useGithubContributors(owner: string, repo: string) { setLoading(true); axios .get( - `https://api.github.com/repos/${owner}/${repo}/contributors`, + `https://api.github.com/repos/${owner}/${repo}/contributors?per_page=100`, config ) .then(({ data }) => setData(data)) From cd4e3d3769808371b9cc7146704a4fd1e8341655 Mon Sep 17 00:00:00 2001 From: hugobbi Date: Sat, 22 Jun 2024 14:22:46 -0300 Subject: [PATCH 2/2] fix: fetching contributors from every page in github api --- .../useGithubContributors.tsx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/hooks/useGithubContributors/useGithubContributors.tsx b/src/hooks/useGithubContributors/useGithubContributors.tsx index 7f994448..47af2aab 100644 --- a/src/hooks/useGithubContributors/useGithubContributors.tsx +++ b/src/hooks/useGithubContributors/useGithubContributors.tsx @@ -8,15 +8,22 @@ function useGithubContributors(owner: string, repo: string) { const [data, setData] = useState([]); const refresh = useCallback( - (config?: AxiosRequestConfig) => { + async (config?: AxiosRequestConfig, page=1, allData=[]) => { setLoading(true); - axios + const response = await axios .get( - `https://api.github.com/repos/${owner}/${repo}/contributors?per_page=100`, + `https://api.github.com/repos/${owner}/${repo}/contributors?per_page=100&page=${page}`, config - ) - .then(({ data }) => setData(data)) - .finally(() => setLoading(false)); + ); + const newData = allData.concat(response.data); + const linkHeader = response.headers.link; + if (linkHeader && linkHeader.includes('rel="next"')) { + const nextPage = page + 1; + return refresh(config, nextPage, newData); + } else { + setData(newData); + setLoading(false); + } }, [owner, repo] );