Skip to content

Commit 8a5c3cd

Browse files
committed
wip
1 parent f2575b2 commit 8a5c3cd

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/GitHub/GitHub.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import Loading from '../Components/Loading';
33
import ProjectListSelector from '../Components/ProjectListSelector';
4-
import { withGithubRepositories } from './withGitHubRepositories';
4+
import { withGithubRepositories, fetchGitHub } from './withGitHubRepositories';
55
import LABELS_TO_ADD from '../Labels';
66
import invertColor from '../invertColor';
77
import './GitHub.css';
@@ -13,12 +13,31 @@ class GitHub extends React.Component {
1313
this.state = {
1414
selectedOption: null,
1515
applying: false,
16+
applyedLabels: []
1617
}
1718
}
1819

1920
handleApply(selectedOption) {
20-
console.log(selectedOption)
2121
this.setState({ selectedOption, applying: true })
22+
this.applyChangesToRepository(selectedOption.value)
23+
}
24+
25+
async applyChangesToRepository(repoName) {
26+
27+
const createLabelsPromices = LABELS_TO_ADD.map(l => this.createLabel(repoName, l))
28+
29+
}
30+
31+
async createLabel(repoName, { name, color }) {
32+
fetchGitHub(
33+
`https://api.github.com/repos/${repoName}/labels`,
34+
'POST',
35+
{ name, color }
36+
)
37+
38+
this.setState(({ applyedLabels }) => ({
39+
applyedLabels: [...applyedLabels, name]
40+
}))
2241
}
2342

2443
render() {
@@ -53,7 +72,7 @@ class GitHub extends React.Component {
5372
<label alt={name} className="label-item"
5473
style={{ backgroundColor: '#' + color, color: invertColor('#' + color, true) }}
5574
>
56-
<input disabled type="checkbox" />
75+
<input disabled type="checkbox" checked={this.state.applyedLabels.indexOf(name) > -1} />
5776
{name}
5877
</label>
5978
</div>

src/GitHub/withGitHubRepositories.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import React from 'react';
22

3-
export function withGithubRepositories(Component) {
3+
const fetchGitHub = (url, method, body) => fetch(url, {
4+
method: method || 'GET',
5+
body,
6+
headers: new Headers({
7+
Authorization: `token ${sessionStorage.getItem('github-token')}`,
8+
Accept: 'application/json',
9+
})
10+
})
11+
12+
function withGithubRepositories(Component) {
413
return class extends React.Component {
514
constructor(props) {
615
super(props);
@@ -20,16 +29,7 @@ export function withGithubRepositories(Component) {
2029

2130
async fetchNextPage(page) {
2231
const paging = page ? `?page=${page}` : '';
23-
24-
const resp = await fetch(new Request(
25-
`https://api.github.com/user/repos${paging}`,
26-
{
27-
headers: new Headers({
28-
Authorization: `token ${sessionStorage.getItem('github-token')}`,
29-
Accept: 'application/json',
30-
})
31-
}
32-
));
32+
const resp = await fetchGitHub(`https://api.github.com/user/repos${paging}`);
3333

3434
let repos = await resp.json();
3535
if (repos.length === 0) {
@@ -46,4 +46,6 @@ export function withGithubRepositories(Component) {
4646
/>
4747
}
4848
}
49-
}
49+
}
50+
51+
export { withGithubRepositories, fetchGitHub }

0 commit comments

Comments
 (0)