Skip to content

Commit 23b3788

Browse files
author
Evert Arias
committed
Add support for multiple repos of the same owner
1 parent 3732821 commit 23b3788

File tree

1 file changed

+57
-22
lines changed

1 file changed

+57
-22
lines changed

src/index.js

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import React from "react";
22
import { Octokit } from "@octokit/rest";
3-
// import styles from "./styles.module.css";
4-
import { Contributors, Profile, Photo, Intro, Name, Subtitle } from "./styles";
3+
import _ from "lodash";
4+
import {
5+
ContributorsList,
6+
Profile,
7+
Photo,
8+
Intro,
9+
Name,
10+
Subtitle
11+
} from "./styles";
512

613
const octokit = new Octokit();
714

@@ -21,37 +28,65 @@ class ProfileComponent extends React.Component {
2128
}
2229
}
2330

24-
export default class ContributorsComponent extends React.Component {
31+
export default class Contributors extends React.Component {
2532
constructor(props) {
2633
super(props);
2734
this.state = {
2835
contributors: []
2936
};
3037
}
3138

39+
loadContributors(owner, repo) {
40+
return new Promise((resolve, reject) => {
41+
octokit.repos
42+
.listContributors({
43+
owner: owner,
44+
repo: repo
45+
})
46+
.then(
47+
result => {
48+
this.setState({
49+
contributors: [
50+
..._.orderBy(
51+
_.unionBy(this.state.contributors, result.data, "login"),
52+
"contributions",
53+
"desc"
54+
)
55+
]
56+
});
57+
resolve();
58+
},
59+
reason => {
60+
reject(reason);
61+
}
62+
);
63+
});
64+
}
65+
3266
componentDidMount() {
33-
octokit.repos
34-
.listContributors({
35-
owner: this.props.owner,
36-
repo: this.props.repo
37-
})
38-
.then(
39-
result => {
40-
this.setState({
41-
contributors: [...result.data]
42-
});
43-
},
44-
() => {
45-
this.setState({
46-
contributors: []
47-
});
48-
}
49-
);
67+
// No repo props provided
68+
if (!this.props.repo) {
69+
this.setState({
70+
contributors: []
71+
});
72+
return;
73+
}
74+
75+
// If repo props provided but contains a single repo name
76+
if (this.props.repo && !Array.isArray(this.props.repo)) {
77+
this.loadContributors(this.props.owner, this.props.repo);
78+
return;
79+
}
80+
81+
// repo props provided as array
82+
this.props.repo.map(repo => {
83+
this.loadContributors(this.props.owner, repo);
84+
});
5085
}
5186

5287
render() {
5388
return (
54-
<Contributors>
89+
<ContributorsList>
5590
{this.state.contributors && this.state.contributors.length
5691
? this.state.contributors.map((contributor, key) => {
5792
return (
@@ -64,7 +99,7 @@ export default class ContributorsComponent extends React.Component {
6499
);
65100
})
66101
: undefined}
67-
</Contributors>
102+
</ContributorsList>
68103
);
69104
}
70105
}

0 commit comments

Comments
 (0)