11import React from 'react'
2+ import { Alert } from 'reactstrap' ;
23import Loading from '../Components/Loading' ;
34import ProjectListSelector from '../Components/ProjectListSelector' ;
45import { withGithubRepositories , fetchGitHub } from './withGitHubRepositories' ;
56import LABELS_TO_ADD from '../Labels' ;
67import invertColor from '../invertColor' ;
78import './GitHub.css' ;
89
10+ const LABELS_TO_REMOVE = [
11+ { name : "bug" , color : "d73a4a" } ,
12+ { name : "duplicate" , color : "cfd3d7" } ,
13+ { name : "enhancement" , color : "a2eeef" } ,
14+ { name : "good first issue" , color : "7057ff" } ,
15+ { name : "help wanted" , color : "008672" } ,
16+ { name : "invalid" , color : "e4e669" } ,
17+ { name : "question" , color : "d876e3" } ,
18+ { name : "wontfix" , color : "ffffff" }
19+ ] ;
20+
21+ const LabelList = ( { header, labels, className, applyedLabels } ) => (
22+ < div className = { `row labels ${ className } ` } >
23+ < h2 className = "col-md-12" > { header } </ h2 >
24+ { labels . map ( ( { name, color } ) => (
25+ < div key = { name } className = "col-md-4" >
26+ < label alt = { name } className = "label-item"
27+ style = { { backgroundColor : '#' + color , color : invertColor ( '#' + color ) } }
28+ >
29+ < i className = "material-icons label-icon" >
30+ { applyedLabels . indexOf ( name ) > - 1 ? 'check_box' : 'check_box_outline_blank' }
31+ </ i >
32+ < span className = "label-name" children = { name } />
33+ </ label >
34+ </ div >
35+ ) ) }
36+ </ div >
37+ )
38+
939class GitHub extends React . Component {
1040
1141 constructor ( props ) {
1242 super ( props )
1343 this . state = {
1444 selectedOption : null ,
1545 applying : false ,
16- applyedLabels : [ ]
46+ applyedLabels : [ ] ,
47+ applyingStatus : null ,
48+ alert : null ,
1749 }
1850 }
1951
@@ -23,20 +55,57 @@ class GitHub extends React.Component {
2355 }
2456
2557 async applyChangesToRepository ( repoName ) {
58+ this . setState ( {
59+ applyedLabels : [ ] ,
60+ alert : null
61+ } )
2662
2763 const createLabelsPromices = LABELS_TO_ADD . map ( l => this . createLabel ( repoName , l ) )
64+ const removeLabelPromices = LABELS_TO_REMOVE . map ( l => this . removeLabel ( repoName , l ) )
65+ try {
2866
67+ await Promise . all ( [ ...createLabelsPromices , ...removeLabelPromices ] )
68+ this . setState ( {
69+ applying : false ,
70+ alert : { type : 'success' , message : 'Setup completed !' }
71+ } ) ;
72+ } catch ( error ) {
73+ this . setState ( {
74+ applying : false ,
75+ alert : { type : 'danger' , message : error . message }
76+ } ) ;
77+ }
78+ }
79+
80+ async removeLabel ( repoName , { name } ) {
81+ await fetchGitHub (
82+ `https://api.github.com/repos/${ repoName } /labels/${ name } ` ,
83+ 'DELETE'
84+ ) ;
85+
86+ this . setState ( ( { applyedLabels } ) => ( {
87+ applyedLabels : [ ...applyedLabels , name ] ,
88+ applyingStatus : `${ name } removed` ,
89+ } ) )
2990 }
3091
3192 async createLabel ( repoName , { name, color } ) {
32- fetchGitHub (
93+ const resp = await fetchGitHub (
3394 `https://api.github.com/repos/${ repoName } /labels` ,
3495 'POST' ,
3596 { name, color }
36- )
97+ ) ;
98+
99+ if ( resp . status === 422 ) {
100+ const message = await resp . json ( ) ;
101+ if ( ! message . errors . find ( ( { code } ) => code === 'already_exists' ) ) {
102+ throw new Error ( JSON . stringify ( message . errors ) ) ;
103+ }
104+ }
37105
38106 this . setState ( ( { applyedLabels } ) => ( {
39- applyedLabels : [ ...applyedLabels , name ]
107+ applyedLabels : [ ...applyedLabels , name ] ,
108+ applyingStatus : `${ name } created` ,
40109 } ) )
41110 }
42111
@@ -61,23 +130,29 @@ class GitHub extends React.Component {
61130 } ) ) }
62131 onApply = { ( selected ) => this . handleApply ( selected ) }
63132 />
133+ { ! this . state . alert ? null :
134+ < Alert
135+ color = { this . state . alert . type }
136+ children = { this . state . alert . message }
137+ />
138+ }
64139 { ! this . state . applying ? null :
65140 < div className = "row applying-status" >
66- < div className = "col-md-12" > < Loading /> </ div >
141+ < div className = "col-md-12" > < Loading status = { this . state . applyingStatus } /> </ div >
67142 </ div >
68143 }
69- < div className = "row labels" >
70- { LABELS_TO_ADD . map ( ( { name , color } ) => (
71- < div key = { name } className = "col-md-4" >
72- < label alt = { name } className = "label-item"
73- style = { { backgroundColor : '#' + color , color : invertColor ( '#' + color , true ) } }
74- >
75- < input disabled type = "checkbox" checked = { this . state . applyedLabels . indexOf ( name ) > - 1 } />
76- { name }
77- </ label >
78- </ div >
79- ) ) }
80- </ div >
144+ < LabelList
145+ header = "Labels to Remove:"
146+ className = "labels-to-remove"
147+ labels = { LABELS_TO_REMOVE }
148+ applyedLabels = { this . state . applyedLabels }
149+ / >
150+ < LabelList
151+ header = "Labels to Add:"
152+ className = "labels-to-add"
153+ labels = { LABELS_TO_ADD }
154+ applyedLabels = { this . state . applyedLabels }
155+ / >
81156 </ section >
82157 }
83158 </ div >
0 commit comments