-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: connect contributors page to API (#1113)
- Loading branch information
1 parent
e9f5056
commit e51c51e
Showing
17 changed files
with
157 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useState } from "react"; | ||
import useSWR, { Fetcher } from "swr"; | ||
import { useRouter } from "next/router"; | ||
|
||
import publicApiFetcher from "lib/utils/public-api-fetcher"; | ||
import getFilterQuery from "lib/utils/get-filter-query"; | ||
|
||
interface PaginatedResponse { | ||
readonly data: DbRepoPR[]; | ||
readonly meta: Meta; | ||
} | ||
|
||
/** | ||
* Fetch contributors based on pull requests. | ||
* Replace with contributors API endpoint when available. | ||
* | ||
* @param intialLimit | ||
* @param repoIds | ||
* @param range | ||
* @returns | ||
*/ | ||
const useContributors = (intialLimit = 10, repoIds: number[] = [], range = 30) => { | ||
const router = useRouter(); | ||
const [page, setPage] = useState(1); | ||
const [limit, setLimit] = useState(intialLimit); | ||
const { filterName, selectedFilter } = router.query; | ||
const topic = filterName as string; | ||
const filterQuery = getFilterQuery(selectedFilter); | ||
const query = new URLSearchParams(filterQuery); | ||
|
||
if (Number.isNaN(Number(topic))) { | ||
query.set("topic", topic); | ||
} | ||
|
||
if (page) { | ||
query.set("page", `${page}`); | ||
} | ||
|
||
if (limit) { | ||
query.set("limit", `${limit}`); | ||
} | ||
|
||
if (repoIds?.length > 0) { | ||
query.set("repoIds", repoIds.join(",")); | ||
} | ||
|
||
query.set("range", `${range}`); | ||
|
||
const baseEndpoint = "prs/search"; | ||
const endpointString = `${baseEndpoint}?${query.toString()}`; | ||
|
||
const { data, error, mutate } = useSWR<PaginatedResponse, Error>( | ||
endpointString, | ||
publicApiFetcher as Fetcher<PaginatedResponse, 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, | ||
mutate, | ||
page, | ||
setPage, | ||
setLimit | ||
}; | ||
}; | ||
|
||
export default useContributors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.