Skip to content

Commit

Permalink
chore: fix top repos api
Browse files Browse the repository at this point in the history
  • Loading branch information
hdJerry committed Aug 9, 2023
1 parent 296f472 commit 6118786
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions cra-rxjs-styled-components/src/constants/url.constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import convertObjectToQueryString from '../helpers/objectToQueryString';
import { IssueType, State } from '../types/types';

export const API_URL_BASE = process.env.REACT_APP_API_URL;
Expand Down Expand Up @@ -30,6 +31,17 @@ export const ORG_REPO_LIST = (user: string) =>
export const USER_REPO_LIST = (user: string, page: string = '1') =>
`${GITHUB_URL_BASE}/users/${user}/repos?sort=pushed&page=${page}&type=all`;

export const USER_TOP_REPO_LIST = (page: string = '1') => {
const params = {
sort: 'updated',
affiliation: 'owner, collaborator, organization_member',
page,
per_page: 20,
};
const queryStrings = convertObjectToQueryString(params);
return `${GITHUB_URL_BASE}/user/repos?${queryStrings}`;
};

export const GISTS_URL = (user: string) =>
`${GITHUB_URL_BASE}/users/${user}/gists?per_page=10`;

Expand Down
5 changes: 5 additions & 0 deletions cra-rxjs-styled-components/src/helpers/objectToQueryString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default function convertObjectToQueryString(
object: Record<string, any>
) {
return new URLSearchParams(object).toString();
}
12 changes: 9 additions & 3 deletions cra-rxjs-styled-components/src/hooks/repositories/use-repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ import {
} from '../../helpers/extract-branch-count';
import { useEffect, useState } from 'react';

import { USER_REPO_LIST } from '../../constants/url.constants';
import {
USER_REPO_LIST,
USER_TOP_REPO_LIST,
} from '../../constants/url.constants';
import { fromFetchWithAuth } from '../auth/from-fetch-with-auth';
import parse from 'parse-link-header';

export function useRepos(username: string | undefined): UseRepo {
export function useRepos(
username: string | undefined,
isTopRepos?: boolean
): UseRepo {
const [state, setState] = useState<RepositoryWithBranchCount[]>([]);
const [paginationPages, setPaginationPages] = useState<Pagination>({
prevPage: '',
Expand All @@ -36,7 +42,7 @@ export function useRepos(username: string | undefined): UseRepo {
useEffect(() => {
if (username) {
const subscription: Subscription = fromFetchWithAuth<Repository[]>(
USER_REPO_LIST(username, page),
isTopRepos ? USER_TOP_REPO_LIST(page) : USER_REPO_LIST(username, page),
{
selector: (response: Response) => {
const links = parse(response.headers.get('Link'));
Expand Down
3 changes: 2 additions & 1 deletion cra-rxjs-styled-components/src/routes/user-top-repos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const NetlifyBadgeContainer = styled.div`
export default function TopRepos() {
const context = useUser();
const user = context?.user;
const { repositories } = useRepos(user?.login);
const isTopRepos: boolean = true;
const { repositories } = useRepos(user?.login, isTopRepos);
const { gists, loadingGist } = useGists();
const topRepositories = [...repositories]
.sort((a, b) => b.stargazers_count - a.stargazers_count)
Expand Down

0 comments on commit 6118786

Please sign in to comment.