Skip to content

Commit 7282f77

Browse files
committed
wip
1 parent b6a2b77 commit 7282f77

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

src/App.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import React from 'react';
2+
import { Switch, Route } from 'react-router-dom'
3+
24
import './App.css';
35
import CoderockrLogo from './assets/images/hand-yellow.svg'
46
import Home from './Home'
57
import Footer from './Footer';
6-
import { Switch, Route } from 'react-router-dom'
8+
import GitHub from './GitHub/GitHub';
79

810
const App = () => (
911
<div className="CWSPApp">
@@ -16,6 +18,7 @@ const App = () => (
1618
<div className=" container">
1719
<Switch>
1820
<Route exact path="/" component={Home} />
21+
<Route path="/github/" component={GitHub} />
1922
</Switch>
2023
</div>
2124
<Footer />

src/Components/Loading.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import { Progress } from 'reactstrap'
3+
4+
const Loading = () => (
5+
<Progress animated color="primary" striped value={100} />
6+
);
7+
8+
export default Loading

src/GitHub/GitHub.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react'
2+
import Loading from '../Components/Loading';
3+
import { githubAccessTokenUrl } from './constants'
4+
5+
class GitHub extends React.Component {
6+
constructor(props) {
7+
super(props);
8+
9+
const params = new URLSearchParams(props.location.search);
10+
11+
this.state = {
12+
loading: true,
13+
code: params.get('code')
14+
};
15+
}
16+
17+
async componentDidMount() {
18+
fetch(new Request(
19+
'https://api.github.com/users/lucassabreu',
20+
{
21+
method: "GET",
22+
headers: new Headers({ accept: 'application/json' })
23+
})
24+
)
25+
.then(r => r.json())
26+
.then(console.log)
27+
28+
fetch(new Request(
29+
githubAccessTokenUrl(this.state.code),
30+
{
31+
method: 'POST',
32+
headers: new Headers({ Accept: 'application/json' })
33+
}
34+
))
35+
.then(r => {
36+
if (r.ok === false) {
37+
throw new Error(`${r.status} - ${r.statusText}`)
38+
}
39+
return r.json()
40+
})
41+
.then(console.log())
42+
}
43+
44+
render() {
45+
const { loading } = this.state;
46+
return (
47+
<div className="GitHub">
48+
{loading ?
49+
<section>
50+
<h2>Loading...</h2>
51+
<Loading />
52+
</section>
53+
:
54+
<section>
55+
</section>
56+
}
57+
</div>
58+
)
59+
}
60+
}
61+
62+
export default GitHub

src/GitHub/constants.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const GITHUB_CLIENT_ID = '0e8a63320fba47de145c';
2+
const GITHUB_CLIENT_SECRET = '<it will not work anyway>';
3+
4+
const githubAccessTokenUrl = (code) => (
5+
`https://github.com/login/oauth/access_token?client_id=${GITHUB_CLIENT_ID}&client_secret=${GITHUB_CLIENT_SECRET}&code=${code}`
6+
);
7+
8+
const githubAuthorizeUrl = (callback) => (
9+
`https://github.com/login/oauth/authorize?client_id=${GITHUB_CLIENT_ID}&scope=repos&redirect_uri=${callback}`
10+
)
11+
12+
export {
13+
GITHUB_CLIENT_ID,
14+
GITHUB_CLIENT_SECRET,
15+
githubAccessTokenUrl,
16+
githubAuthorizeUrl,
17+
}
18+
19+
20+

src/Home.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import githubLogo from './assets/images/github.svg'
55
import gitlabLogo from './assets/images/gitlab.svg'
66
import trelloLogo from './assets/images/trello.svg'
77
import './Home.css'
8+
import { githubAuthorizeUrl } from './GitHub/constants';
89

910
const callbackUrl = (setupable) => `${window.location.href}${setupable}/callback`
1011
const SETUPABLE = [
1112
{
1213
name: "GitHub",
1314
logo: githubLogo,
1415
enabled: true,
15-
url: 'https://github.com/login/oauth/authorize?client_id=0e8a63320fba47de145c&scope=repos&redirect_uri=' + callbackUrl('github'),
16+
url: githubAuthorizeUrl(callbackUrl('github')),
1617
},
1718
{
1819
name: "GitLab",

0 commit comments

Comments
 (0)