Skip to content

Commit 2948a4f

Browse files
committed
implementation for branch hooks
1 parent 41e0971 commit 2948a4f

7 files changed

Lines changed: 133 additions & 1 deletion

File tree

docs/components/Branch.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react";
2+
import { useBranch } from "../../src";
3+
4+
const Branch = () => {
5+
const { branch, loading, error } = useBranch("facebook", "react", "master");
6+
7+
return (
8+
<div>
9+
<h1>Branch by repo</h1>
10+
{loading && <div>Loading branch from Github</div>}
11+
{error && <div>{error}</div>}
12+
{branch && (
13+
<div>
14+
{branch.name} - {branch.commit.sha}
15+
</div>
16+
)}
17+
</div>
18+
);
19+
};
20+
21+
export default Branch;

docs/components/Branches.jsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from "react";
2+
import { useBranches } from "../../src";
3+
4+
const Branches = () => {
5+
const { branches, loading, error } = useBranches("facebook", "react");
6+
7+
return (
8+
<div>
9+
<h1>Branches by repo</h1>
10+
{loading && <div>Loading branches from Github</div>}
11+
{error && <div>{error}</div>}
12+
{branches &&
13+
branches.length > 0 &&
14+
branches.map(branch => (
15+
<div key={branch.commit.sha}>
16+
<h4>
17+
{branch.name} - {branch.commit.sha}
18+
</h4>
19+
</div>
20+
))}
21+
</div>
22+
);
23+
};
24+
25+
export default Branches;

docs/example.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from "react";
22
import { render } from "react-dom";
33
import Repos from "./components/Repos";
44
import User from "./components/User";
5+
import Branches from "./components/Branches";
6+
import Branch from "./components/Branch";
57

68
function App() {
79
return (
@@ -10,6 +12,10 @@ function App() {
1012
<hr />
1113
<User />
1214
<hr />
15+
<Branches />
16+
<hr />
17+
<Branch />
18+
<hr />
1319
</div>
1420
);
1521
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"scripts": {
2121
"publish": "parcel build docs/index.html",
22-
"dev": "parcel docs/index.html",
22+
"start": "parcel docs/index.html",
2323
"release": "np"
2424
},
2525
"devDependencies": {

src/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import useRepos from './lib/useRepos'
22
import useUser from './lib/useUser'
3+
import {
4+
useBranches,
5+
} from './lib/branch/useBranches'
6+
import {
7+
useBranch
8+
} from './lib/branch/useBranch'
39

410
export {
511
useRepos,
612
useUser,
13+
useBranches,
14+
useBranch,
715
}
816

917
export default {
1018
useRepos,
1119
useUser,
20+
useBranches,
21+
useBranch,
1222
}

src/lib/branch/useBranch.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
useState,
3+
useEffect
4+
} from 'react'
5+
6+
export const useBranch = (owner, repo, branchName) => {
7+
const [branch, setBranch] = useState(null)
8+
const [loading, setLoading] = useState(false)
9+
const [error, setError] = useState(null)
10+
11+
useEffect(() => {
12+
if (repo && repo.length > 0 && owner && owner.length && branchName && branchName.length > 0) {
13+
setLoading(true)
14+
setError(null)
15+
fetch(`https://api.github.com/repos/${owner}/${repo}/branches/${branchName}`)
16+
.then(res => res.json())
17+
.then(data => {
18+
setLoading(false)
19+
setBranch(data)
20+
setError(null)
21+
})
22+
.catch(e => {
23+
setLoading(false)
24+
setBranch(null)
25+
setError(e)
26+
})
27+
}
28+
}, [owner, repo, branchName])
29+
30+
return {
31+
branch,
32+
loading,
33+
error
34+
}
35+
}

src/lib/branch/useBranches.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
useState,
3+
useEffect
4+
} from 'react'
5+
6+
export const useBranches = (owner, repo) => {
7+
const [branches, setBranches] = useState([])
8+
const [loading, setLoading] = useState(false)
9+
const [error, setError] = useState(null)
10+
11+
useEffect(() => {
12+
if (repo && repo.length > 0 && owner && owner.length) {
13+
setLoading(true)
14+
setError(null)
15+
fetch(`https://api.github.com/repos/${owner}/${repo}/branches`)
16+
.then(res => res.json())
17+
.then(data => {
18+
setLoading(false)
19+
setBranches(data)
20+
setError(null)
21+
})
22+
.catch(e => {
23+
setLoading(false)
24+
setBranches([])
25+
setError(e)
26+
})
27+
}
28+
}, [owner, repo])
29+
30+
return {
31+
branches,
32+
loading,
33+
error
34+
}
35+
}

0 commit comments

Comments
 (0)