1
+ 'use strict' ;
2
+
3
+ {
4
+ function createAndAppend ( name , parent , options = { } ) {
5
+ const elem = document . createElement ( name ) ;
6
+ parent . appendChild ( elem ) ;
7
+ Object . keys ( options ) . forEach ( ( key ) => {
8
+ const value = options [ key ] ;
9
+ if ( key === 'text' ) {
10
+ elem . innerText = value ;
11
+ } else {
12
+ elem . setAttribute ( key , value ) ;
13
+ }
14
+ } ) ;
15
+ return elem ;
16
+ }
17
+ function main ( url ) {
18
+ fetch ( HYF_REPOS_URL )
19
+ . then ( response => response . json ( ) )
20
+ . then ( json => createStuff ( json ) )
21
+ . catch ( err => createAndAppend ( 'div' , root , { text : err . message , class : 'alert-error' } ) )
22
+
23
+ } //end main
24
+
25
+ function createStuff ( data ) {
26
+ let newArray = [ ] ;
27
+ let forkArray = [ ] ;
28
+ let languageArray = [ ] ;
29
+ let descriptionArray = [ ] ;
30
+ let updatedAt = [ ] ;
31
+ let htmlArray = [ ] ;
32
+ data . sort ( ( a , b ) => ( a . name ) . localeCompare ( b . name ) ) ;
33
+
34
+ for ( let i = 0 ; i < data . length ; i ++ ) {
35
+ newArray . push ( data [ i ] . name ) ;
36
+ descriptionArray . push ( data [ i ] . description ) ;
37
+ forkArray . push ( data [ i ] . forks ) ;
38
+ languageArray . push ( data [ i ] . language ) ;
39
+ updatedAt . push ( data [ i ] . updated_at ) ;
40
+ htmlArray . push ( data [ i ] . html_url ) ;
41
+ var date = new Date ( ( data [ i ] . updated_at ) ) ;
42
+ date = date . toUTCString ( ) ;
43
+ }
44
+
45
+ const root = document . getElementById ( 'root' ) ;
46
+
47
+ while ( root . firstChild ) {
48
+ root . removeChild ( root . firstChild ) ;
49
+ }
50
+
51
+ createAndAppend ( 'h1' , root , { text : "Hack Your Future Repositories" , class : 'title' } ) ;
52
+ createAndAppend ( 'h3' , root , { text : "Select a repository: " , class : 'subtitle' } ) ;
53
+ const selectList = createAndAppend ( 'select' , root , { id : "mySelect" } ) ;
54
+ const headerDiv = createAndAppend ( 'div' , root , { class : 'headerdiv' } ) ;
55
+ createAndAppend ( 'h3' , headerDiv , { text : "Repository Information" , class : 'subtitle' , id : 'repoHeader' } ) ;
56
+ createAndAppend ( 'h3' , headerDiv , { text : "Contributors" , class : 'subtitle' , id :'contributorHeader' } ) ;
57
+ const container = createAndAppend ( 'div' , root , { class : 'container' } ) ;
58
+ const card = createAndAppend ( 'div' , container , { class : 'card' } ) ;
59
+ const ul = createAndAppend ( 'ul' , card , { id : "myUl" , } ) ;
60
+ const contributorsCard = createAndAppend ( 'div' , container , { class : 'card' } ) ;
61
+ const contributorsUl = createAndAppend ( 'ul' , contributorsCard , { id : 'contributorsUl' } ) ;
62
+ const Index0Name = createAndAppend ( 'li' , ul , { text : "Repository: " , class : 'nameInContainer' } ) ;
63
+ const Index0Link = createAndAppend ( 'a' , Index0Name , { text : newArray [ 0 ] , target : "_blank" , href : htmlArray [ 0 ] } ) ;
64
+ const Index0Description = createAndAppend ( 'li' , ul , { text : "Description: " + descriptionArray [ 0 ] , class :"descriptionInContainer" } ) ;
65
+ const Index0Fork = createAndAppend ( 'li' , ul , { text : "Number of Forks: " + forkArray [ 0 ] , class : 'forksInContainer' } ) ;
66
+ const Index0Language = createAndAppend ( 'li' , ul , { text : "Language: " + languageArray [ 0 ] , class : 'updatedAtInContainer' } ) ;
67
+ const Index0UpdatedAt = createAndAppend ( 'li' , ul , { text : "Updated at: " + date , class : 'updatedAtInContainer' } )
68
+
69
+ fetch ( 'https://api.github.com/repos/HackYourFuture/' + newArray [ 0 ] + '/contributors' )
70
+ . then ( response => response . json ( ) )
71
+ . then ( json => createStuff2 ( json ) )
72
+ . catch ( err => createAndAppend ( 'div' , root , { text : err . message , class : 'alert-error' } ) )
73
+
74
+ function createStuff2 ( data ) {
75
+ for ( let i = 0 ; i < data . length ; i ++ ) {
76
+ let Image0Link = createAndAppend ( 'li' , contributorsUl , { } )
77
+ let contributor0Name = createAndAppend ( 'img' , Image0Link , { src : data [ i ] . avatar_url , class : 'imageSrc' } ) ;
78
+ let contributor0Link = createAndAppend ( 'a' , Image0Link , { text : data [ i ] . login , target : "_blank" , href : data [ i ] . html_url , id : 'link' } ) ;
79
+ let contributor0Badge = createAndAppend ( 'li' , Image0Link , { text :"Contributions: " + data [ i ] . contributions , class : 'badge' } ) ;
80
+ } //end for
81
+
82
+
83
+
84
+ data . forEach ( ( repo ) => {
85
+ for ( let i = 0 ; i < newArray . length ; i ++ ) {
86
+ createAndAppend ( 'option' , selectList , { id : "myOption" , value : i , text : newArray [ i ] } ) ;
87
+ }
88
+ } ) ;
89
+
90
+ function removeNodes ( container ) {
91
+ while ( ul . hasChildNodes ( ) ) {
92
+ ul . removeChild ( ul . firstChild ) ;
93
+ }
94
+ while ( contributorsUl . hasChildNodes ( ) ) {
95
+ contributorsUl . removeChild ( contributorsUl . firstChild ) ;
96
+ }
97
+ } //end removeNodes
98
+
99
+ selectList . onchange = function ( selectedIndex ) {
100
+ fetch ( 'https://api.github.com/repos/HackYourFuture/' + newArray [ this . selectedIndex ] + '/contributors' )
101
+ . then ( response => response . json ( ) )
102
+ . then ( json => createStuff3 ( json ) )
103
+ . catch ( err => createAndAppend ( 'div' , root , { text : err . message , class : 'alert-error' } ) )
104
+
105
+ function createStuff3 ( data ) {
106
+ for ( let i = 0 ; i < data . length ; i ++ ) {
107
+ let ImageLink = createAndAppend ( 'li' , contributorsUl , { } )
108
+ let contributorName = createAndAppend ( 'img' , ImageLink , { src : data [ i ] . avatar_url , class : 'imageSrc' } ) ;
109
+ let contributorLink = createAndAppend ( 'a' , ImageLink , { text : data [ i ] . login , target : "_blank" , href : data [ i ] . html_url , id : 'link' } ) ;
110
+ let contributorBadge = createAndAppend ( 'li' , ImageLink , { text :"Contributions: " + data [ i ] . contributions , class : 'badge' } ) ;
111
+ } //end for
112
+ } //end createStuff3
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
+ const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100' ;
128
+ window . onload = ( ) => main ( HYF_REPOS_URL ) ;
129
+
130
+ }
0 commit comments