Skip to content

Commit c4a6622

Browse files
authored
Merge pull request #7 from bdbch/1-branch-hooks
1 branch hooks
2 parents 41e0971 + 9162406 commit c4a6622

8 files changed

Lines changed: 159 additions & 22 deletions

File tree

README.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@ import { useRepos } from "@d2k/react-github";
2222
function MyComponent() {
2323
const { repos, loading, error } = useRepos("nat");
2424

25-
return (
26-
<div>
27-
{loading && <div>Loading repositories</div>}
28-
{error && <div>{err}</div>}
29-
{repos &&
30-
repos.length > 0 &&
31-
repos.map(repo => <div key={repo.id}>{repo.name}</div>)}
32-
</div>
33-
);
25+
// your component
3426
}
3527
```
3628

@@ -42,17 +34,30 @@ import { useUser } from "@d2k/react-github";
4234
function MyComponent() {
4335
const { user, loading, error } = useUser("nat");
4436

45-
return (
46-
<div>
47-
{loading && <div>Loading repositories</div>}
48-
{error && <div>{err}</div>}
49-
{user && (
50-
<div>
51-
<h1>{user.login}</h1>
52-
<img src={user.avatar_url} />
53-
</div>
54-
)}
55-
</div>
56-
);
37+
// your component
38+
}
39+
```
40+
41+
### Get branches from repo
42+
43+
```js
44+
import { useBranches } from "@d2k/react-github";
45+
46+
function MyComponent() {
47+
const { branches, loading, error } = useBranches("facebook", "react");
48+
49+
// your component
50+
}
51+
```
52+
53+
### Get branch from repo
54+
55+
```js
56+
import { useBranch } from "@d2k/react-github";
57+
58+
function MyComponent() {
59+
const { branch, loading, error } = useBranch("facebook", "react", "master");
60+
61+
// your component
5762
}
5863
```

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)