1
1
import React from "react" ;
2
2
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" ;
5
12
6
13
const octokit = new Octokit ( ) ;
7
14
@@ -21,37 +28,65 @@ class ProfileComponent extends React.Component {
21
28
}
22
29
}
23
30
24
- export default class ContributorsComponent extends React . Component {
31
+ export default class Contributors extends React . Component {
25
32
constructor ( props ) {
26
33
super ( props ) ;
27
34
this . state = {
28
35
contributors : [ ]
29
36
} ;
30
37
}
31
38
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
+
32
66
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
+ } ) ;
50
85
}
51
86
52
87
render ( ) {
53
88
return (
54
- < Contributors >
89
+ < ContributorsList >
55
90
{ this . state . contributors && this . state . contributors . length
56
91
? this . state . contributors . map ( ( contributor , key ) => {
57
92
return (
@@ -64,7 +99,7 @@ export default class ContributorsComponent extends React.Component {
64
99
) ;
65
100
} )
66
101
: undefined }
67
- </ Contributors >
102
+ </ ContributorsList >
68
103
) ;
69
104
}
70
105
}
0 commit comments