Skip to content

Commit d27bfdd

Browse files
committed
implements collaborators but needs authentication
1 parent ebc494a commit d27bfdd

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

docs/components/Collaborators.jsx

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

docs/example.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Branch from "./components/Branch";
77
import LatestRelease from "./components/LatestRelease";
88
import TaggedRelease from "./components/TaggedRelease";
99
import Forks from "./components/Forks";
10+
import Collaborators from "./components/Collaborators";
1011

1112
function App() {
1213
return (
@@ -25,6 +26,8 @@ function App() {
2526
<hr />
2627
<Forks />
2728
<hr />
29+
<Collaborators />
30+
<hr />
2831
</div>
2932
);
3033
}

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import useBranch from './lib/branch/useBranch'
55
import useLatestRelease from './lib/release/useLatestRelease'
66
import useTaggedRelease from './lib/release/useTaggedRelease'
77
import useForks from './lib/repository/useForks'
8+
import useCollaborators from './lib/repository/useCollaborators'
89

910
export {
1011
useRepos,
@@ -14,6 +15,7 @@ export {
1415
useLatestRelease,
1516
useTaggedRelease,
1617
useForks,
18+
useCollaborators,
1719
}
1820

1921
export default {
@@ -24,4 +26,5 @@ export default {
2426
useLatestRelease,
2527
useTaggedRelease,
2628
useForks,
29+
useCollaborators,
2730
}
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 default function useCollaborators(owner, repo) {
7+
const [collaborators, setCollaborators] = 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}/collaborators`)
16+
.then(res => res.json())
17+
.then(data => {
18+
setLoading(false)
19+
setCollaborators(data)
20+
setError(null)
21+
})
22+
.catch(e => {
23+
setLoading(false)
24+
setCollaborators([])
25+
setError(e)
26+
})
27+
}
28+
}, [owner, repo])
29+
30+
return {
31+
collaborators,
32+
loading,
33+
error
34+
}
35+
}

0 commit comments

Comments
 (0)