1
+ 'use strict' ;
2
+ {
3
+ function fetchJSON ( url ) {
4
+ return new Promise ( function ( resolve , reject ) {
5
+ const xhr = new XMLHttpRequest ( ) ;
6
+ xhr . open ( 'GET' , url ) ;
7
+ xhr . responseType = 'json' ;
8
+ xhr . onload = ( ) => {
9
+ if ( xhr . status < 400 ) {
10
+ resolve ( xhr . response ) ;
11
+ } else {
12
+ reject ( new Error ( `Network error: ${ xhr . status } - ${ xhr . statusText } ` ) ) ;
13
+ }
14
+ }
15
+ xhr . onerror = ( ) => reject ( new Error ( 'Network request failed' ) ) ;
16
+ xhr . send ( ) ;
17
+ } ) ; // end promise
18
+ } //end fetchJSON
19
+
20
+ function createAndAppend ( name , parent , options = { } ) {
21
+ const elem = document . createElement ( name ) ;
22
+ parent . appendChild ( elem ) ;
23
+ Object . keys ( options ) . forEach ( ( key ) => {
24
+ const value = options [ key ] ;
25
+ if ( key === 'text' ) {
26
+ elem . innerText = value ;
27
+ } else {
28
+ elem . setAttribute ( key , value ) ;
29
+ }
30
+ } ) ;
31
+ return elem ;
32
+ }
33
+
34
+ function main ( url ) {
35
+ fetchJSON ( HYF_REPOS_URL )
36
+ . then ( data => createStuff ( data ) )
37
+ . catch ( err => createAndAppend ( 'div' , root , { text : err . message , class : 'alert-error' } ) ,
38
+ createAndAppend ( 'img' , root , { id : 'catImage' , src : 'https://us.123rf.com/450wm/photodeti/photodeti1702/photodeti170200132/72587923-cat-holding-stop-sign-isolated-on-white-background-.jpg?ver=6' } ) ) ;
39
+ } //end main
40
+
41
+ function createStuff ( data ) {
42
+ fetchJSON ( 'https://api.github.com/repos/HackYourFuture/alumni/contributors' )
43
+ . then ( data => createStuff2 ( data ) )
44
+
45
+ let newArray = [ ] ;
46
+ let forkArray = [ ] ;
47
+ let languageArray = [ ] ;
48
+ let descriptionArray = [ ] ;
49
+ let updatedAt = [ ] ;
50
+ let htmlArray = [ ] ;
51
+ const root = document . getElementById ( 'root' ) ;
52
+ data . sort ( ( a , b ) => ( a . name ) . localeCompare ( b . name ) ) ;
53
+
54
+ for ( let i = 0 ; i < data . length ; i ++ ) {
55
+ newArray . push ( data [ i ] . name ) ;
56
+ descriptionArray . push ( data [ i ] . description ) ;
57
+ forkArray . push ( data [ i ] . forks ) ;
58
+ languageArray . push ( data [ i ] . language ) ;
59
+ updatedAt . push ( data [ i ] . updated_at ) ;
60
+ htmlArray . push ( data [ i ] . html_url ) ;
61
+ var date = new Date ( ( data [ i ] . updated_at ) ) ;
62
+ date = date . toUTCString ( ) ;
63
+ }
64
+
65
+ while ( root . firstChild ) {
66
+ root . removeChild ( root . firstChild ) ;
67
+ }
68
+
69
+ createAndAppend ( 'h1' , root , { text : "Hack Your Future Repositories" , class : 'title' } ) ;
70
+ createAndAppend ( 'h3' , root , { text : "Select a repository: " , class : 'subtitle' } ) ;
71
+ const selectList = createAndAppend ( 'select' , root , { id : "mySelect" } ) ;
72
+ const headerDiv = createAndAppend ( 'div' , root , { class : 'headerdiv' } ) ;
73
+ createAndAppend ( 'h3' , headerDiv , { text : "Repository Information" , class : 'subtitle' , id : 'repoHeader' } ) ;
74
+ createAndAppend ( 'h3' , headerDiv , { text : "Contributors" , class : 'subtitle' , id :'contributorHeader' } ) ;
75
+ const container = createAndAppend ( 'div' , root , { class : 'container' } ) ;
76
+ const card = createAndAppend ( 'div' , container , { class : 'card' } ) ;
77
+ const ul = createAndAppend ( 'ul' , card , { id : "myUl" , } ) ;
78
+ const contributorsCard = createAndAppend ( 'div' , container , { class : 'card' } ) ;
79
+ const contributorsUl = createAndAppend ( 'ul' , contributorsCard , { id : 'contributorsUl' } ) ;
80
+ const Index0Name = createAndAppend ( 'li' , ul , { text : "Repository: " , class : 'nameInContainer' } ) ;
81
+ const Index0Link = createAndAppend ( 'a' , Index0Name , { text : newArray [ 0 ] , target : "_blank" , href : htmlArray [ 0 ] } ) ;
82
+ const Index0Description = createAndAppend ( 'li' , ul , { text : "Description: " + descriptionArray [ 0 ] , class :"descriptionInContainer" } ) ;
83
+ const Index0Fork = createAndAppend ( 'li' , ul , { text : "Number of Forks: " + forkArray [ 0 ] , class : 'forksInContainer' } ) ;
84
+ const Index0Language = createAndAppend ( 'li' , ul , { text : "Language: " + languageArray [ 0 ] , class : 'updatedAtInContainer' } ) ;
85
+ const Index0UpdatedAt = createAndAppend ( 'li' , ul , { text : "Updated at: " + date , class : 'updatedAtInContainer' } )
86
+
87
+ function createStuff2 ( data ) {
88
+ for ( let i = 0 ; i < data . length ; i ++ ) {
89
+ let Image0Link = createAndAppend ( 'li' , contributorsUl , { } )
90
+ let contributor0Name = createAndAppend ( 'img' , Image0Link , { src : data [ i ] . avatar_url , class : 'imageSrc' } ) ;
91
+ let contributor0Link = createAndAppend ( 'a' , Image0Link , { text : data [ i ] . login , target : "_blank" , href : data [ i ] . html_url , id : 'link' } ) ;
92
+ let contributor0Badge = createAndAppend ( 'li' , Image0Link , { text :"Contributions: " + data [ i ] . contributions , class : 'badge' } ) ;
93
+ } //end for
94
+
95
+ data . forEach ( ( repo ) => {
96
+ for ( let i = 0 ; i < newArray . length ; i ++ ) {
97
+ createAndAppend ( 'option' , selectList , { id : "myOption" , value : i , text : newArray [ i ] } ) ;
98
+ }
99
+ } ) ;
100
+
101
+ function removeNodes ( container ) {
102
+ while ( ul . hasChildNodes ( ) ) {
103
+ ul . removeChild ( ul . firstChild ) ;
104
+ }
105
+ while ( contributorsUl . hasChildNodes ( ) ) {
106
+ contributorsUl . removeChild ( contributorsUl . firstChild ) ;
107
+ }
108
+ } //end removeNodes
109
+
110
+ selectList . onchange = function ( selectedIndex ) {
111
+ fetchJSON ( 'https://api.github.com/repos/HackYourFuture/' + newArray [ this . selectedIndex ] + '/contributors' )
112
+ . then ( data => createStuff3 ( data ) )
113
+
114
+ let repoName = createAndAppend ( 'li' , ul , { text : "Repository: " , class : 'nameInContainer' , function : removeNodes ( ) } ) ;
115
+ createAndAppend ( 'a' , repoName , { text : newArray [ this . selectedIndex ] , id : 'linkInContainer' , target : "_blank" , href : htmlArray [ this . selectedIndex ] } ) ;
116
+ createAndAppend ( 'li' , ul , { text : "Description: " + descriptionArray [ this . selectedIndex ] , class : 'descriptionInContainer' } ) ;
117
+ createAndAppend ( 'li' , ul , { text : "Number of Forks: " + forkArray [ this . selectedIndex ] , class : 'forksInContainer' } ) ;
118
+ createAndAppend ( 'li' , ul , { text : "Language: " + languageArray [ this . selectedIndex ] , class : 'languageInContainer' } ) ;
119
+ let dates = new Date ( updatedAt [ this . selectedIndex ] ) ;
120
+ dates = dates . toUTCString ( ) ;
121
+ createAndAppend ( 'li' , ul , { text : "Updated at: " + dates , class : 'updatedAtInContainer' } ) ;
122
+
123
+ } // end of onchange
124
+ } // end createStuff2
125
+ } //end createStuff
126
+
127
+ function createStuff3 ( data ) {
128
+ for ( let i = 0 ; i < data . length ; i ++ ) {
129
+ let ImageLink = createAndAppend ( 'li' , contributorsUl , { } )
130
+ let contributorName = createAndAppend ( 'img' , ImageLink , { src : data [ i ] . avatar_url , class : 'imageSrc' } ) ;
131
+ let contributorLink = createAndAppend ( 'a' , ImageLink , { text : data [ i ] . login , target : "_blank" , href : data [ i ] . html_url , id : 'link' } ) ;
132
+ let contributorBadge = createAndAppend ( 'li' , ImageLink , { text :"Contributions: " + data [ i ] . contributions , class : 'badge' } ) ;
133
+ } //end for
134
+ } //end createStuff3
135
+
136
+ const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100' ;
137
+ window . onload = ( ) => main ( HYF_REPOS_URL ) ;
138
+
139
+ }
0 commit comments