Skip to content

Commit

Permalink
implements useLatestRelease hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbch committed Jul 20, 2019
1 parent 299c9bf commit 8d75f18
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
30 changes: 30 additions & 0 deletions docs/components/LatestRelease.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from "react";
import { useLatestRelease } from "../../src";

const LatestRelease = () => {
const { release, loading, error } = useLatestRelease("facebook", "react");

return (
<div>
<h1>Latest release by repo</h1>
{loading && <div>Loading release from Github</div>}
{error && <div>{error}</div>}
{release && (
<div>
<h4>
<a
href={release.html_url}
target="_blank"
rel="nofollow noreferrer"
>
{release.name}
</a>
</h4>
<div>{release.body}</div>
</div>
)}
</div>
);
};

export default LatestRelease;
3 changes: 3 additions & 0 deletions docs/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Repos from "./components/Repos";
import User from "./components/User";
import Branches from "./components/Branches";
import Branch from "./components/Branch";
import LatestRelease from "./components/LatestRelease";

function App() {
return (
Expand All @@ -16,6 +17,8 @@ function App() {
<hr />
<Branch />
<hr />
<LatestRelease />
<hr />
</div>
);
}
Expand Down
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import useRepos from './lib/useRepos'
import useUser from './lib/useUser'
import {
useBranches,
} from './lib/branch/useBranches'
import {
useBranch
} from './lib/branch/useBranch'
import useBranches from './lib/branch/useBranches'
import useBranch from './lib/branch/useBranch'
import useLatestRelease from './lib/repository/useLatestRelease'

export {
useRepos,
useUser,
useBranches,
useBranch,
useLatestRelease,
}

export default {
useRepos,
useUser,
useBranches,
useBranch,
useLatestRelease,
}
2 changes: 1 addition & 1 deletion src/lib/branch/useBranch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useEffect
} from 'react'

export const useBranch = (owner, repo, branchName) => {
export default function useBranch(owner, repo, branchName) {
const [branch, setBranch] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/branch/useBranches.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useEffect
} from 'react'

export const useBranches = (owner, repo) => {
export default function useBranches(owner, repo) {
const [branches, setBranches] = useState([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
Expand Down
35 changes: 35 additions & 0 deletions src/lib/repository/useLatestRelease.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
useState,
useEffect
} from 'react'

export default function useLatestRelease(owner, repo) {
const [release, setRelease] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)

useEffect(() => {
if (owner && owner.length > 0 && repo && repo.length > 0) {
setError(null)
setLoading(true)

fetch(`https://api.github.com/repos/${owner}/${repo}/releases/latest`)
.then(res => res.json())
.then(data => {
setLoading(false)
setRelease(data)
})
.catch(e => {
setLoading(false)
setRelease(null)
setError(e)
})
}
}, [owner, repo])

return {
release,
loading,
error
}
}

0 comments on commit 8d75f18

Please sign in to comment.