Skip to content

Commit 6ea4d5a

Browse files
committed
added a custom hook to fetch the repository data using the github api
1 parent 15ae2a1 commit 6ea4d5a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

client/src/hooks/useFetchGitHub.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { useState } from "react";
2+
import axios from "axios";
3+
import { RepositoryData } from "../types/github";
4+
5+
export const useFetchGitHub = (): RepositoryData => {
6+
const [data, setData] = useState<RepositoryData>({
7+
forks: "0",
8+
stargazers: "0",
9+
});
10+
11+
axios
12+
.get("https://api.github.com/repos/0l1v3rr/github-readme-tech-stack")
13+
.then((res) => {
14+
const rd: any = res.data;
15+
setData({
16+
stargazers: rd.stargazers_count,
17+
forks: rd.forks_count,
18+
});
19+
});
20+
21+
return data;
22+
};

client/src/sections/Header.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import ButtonLink from "../components/buttons/ButtonLink";
22
import Title from "../components/repo/Title";
33
import { AiOutlineStar, AiOutlineGithub } from "react-icons/ai";
44
import { BiGitRepoForked } from "react-icons/bi";
5+
import { useFetchGitHub } from "../hooks/useFetchGitHub";
56

67
const Header = () => {
8+
const ghStats = useFetchGitHub();
9+
710
return (
811
<header className="flex flex-col justify-center items-center w-full mt-5">
912
<Title repoName="github-readme-tech-stack" username="0l1v3rr" />
@@ -19,14 +22,14 @@ const Header = () => {
1922
<ButtonLink
2023
link="https://github.com/0l1v3rr/github-readme-tech-stack/network/members"
2124
icon={BiGitRepoForked}
22-
num={6}
25+
num={Number(ghStats.forks)}
2326
text="Fork"
2427
/>
2528

2629
<ButtonLink
2730
link="https://github.com/0l1v3rr/github-readme-tech-stack/stargazers"
2831
icon={AiOutlineStar}
29-
num={37}
32+
num={Number(ghStats.stargazers)}
3033
text="Star"
3134
/>
3235
</div>

client/src/types/github.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface RepositoryData {
2+
stargazers: string;
3+
forks: string;
4+
}

0 commit comments

Comments
 (0)