2929 } ) ;
3030 return elem ;
3131 }
32+
3233
34+
3335 function main ( url ) {
34- fetchJSON ( url , ( err , data ) => {
35- const root = document . getElementById ( 'root' ) ;
36- if ( err ) {
37- createAndAppend ( 'div' , root , { text : err . message , class : 'alert-error' } ) ;
38- } else {
39- createAndAppend ( 'pre' , root , { text : JSON . stringify ( data , null , 2 ) } ) ;
36+ while ( root . firstChild ) {
37+ root . removeChild ( root . firstChild ) ;
38+ }
39+
40+ fetchJSON ( HYF_REPOS_URL , ( err , data ) => {
41+ let newArray = [ ] ;
42+ let forkArray = [ ] ;
43+ let languageArray = [ ] ;
44+ let newURLArray = [ ] ;
45+ let contributorsArray = [ ] ;
46+ for ( let i = 0 ; i < data . length ; i ++ ) {
47+ newArray . push ( data [ i ] . name ) ;
48+ newArray . sort ( ) ;
49+ forkArray . push ( data [ i ] . forks ) ;
50+ languageArray . push ( data [ i ] . language ) ;
51+ contributorsArray . push ( data [ i ] . contributors_url ) ;
52+ contributorsArray . sort ( ) ;
53+ newURLArray . push ( data [ i ] . html_url ) ;
54+ newURLArray . sort ( ) ;
55+ }
56+
57+ let app = document . getElementById ( 'root' ) ;
58+ const header = createAndAppend ( 'h1' , app , { text : "Hack Your Future Repositories" , class : 'title' } ) ;
59+ const subHeader = createAndAppend ( 'h3' , app , { text : "Select a repository: " , class : 'subtitle' } ) ;
60+ const selectList = createAndAppend ( 'select' , app , { text : 'Select a Repo' , id : "mySelect" } ) ;
61+ const container = createAndAppend ( 'div' , app , { class : 'container' } ) ;
62+ const card = createAndAppend ( 'div' , container , { text : "Description of this repository " , class : 'card' } ) ;
63+ const ul = createAndAppend ( 'ul' , card , { id : "myUl" , } ) ;
64+ const contributorsheader = createAndAppend ( 'h1' , root , { text : "Contributors" , class : 'title' } ) ;
65+ const contributorsContainer = createAndAppend ( 'div' , root , { class : 'container' } )
66+ const contributorsCard = createAndAppend ( 'div' , contributorsContainer , { text : "Contributors to this Repository" , class : 'card' } ) ;
67+ const contributorsUl = createAndAppend ( 'ul' , contributorsCard , { id : 'contributorsUl' } ) ;
68+
69+ data . forEach ( ( repo ) => {
70+ for ( let i = 0 ; i < newArray . length ; i ++ ) {
71+ createAndAppend ( 'option' , selectList , { id : "myOption" , value : i , text : newArray [ i ] } ) ;
72+ }
73+ } ) ;
74+
75+ function removeNodes ( container ) {
76+ while ( ul . hasChildNodes ( ) ) {
77+ ul . removeChild ( ul . firstChild ) ;
78+ }
79+ while ( contributorsUl . hasChildNodes ( ) ) {
80+ contributorsUl . removeChild ( contributorsUl . firstChild ) ;
81+ }
4082 }
83+
84+ selectList . onchange = function ( selectedIndex ) {
85+ createAndAppend ( 'li' , ul , { text : "Repository: " + newArray [ this . selectedIndex ] , class : 'nameInContainer' , function : removeNodes ( ) } ) ;
86+ createAndAppend ( 'li' , ul , { text : "Number of Forks: " + forkArray [ this . selectedIndex ] , class : 'forksInContainer' } ) ;
87+ createAndAppend ( 'li' , ul , { text : "Language: " + languageArray [ this . selectedIndex ] , class : 'languageInContainer' } ) ;
88+ createAndAppend ( 'li' , contributorsUl , { text : newURLArray [ this . selectedIndex ] , class : 'contributorsInContainer' } ) ;
89+
90+ }
91+
4192 } ) ;
4293 }
4394
4495 const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100' ;
45-
96+ const theContributors_URL = 'https://api.github.com/repos/HackYourFuture/contributors' ;
4697 window . onload = ( ) => main ( HYF_REPOS_URL ) ;
47- }
98+ window . onload = ( ) => main ( theContributors_URL ) ;
99+ }
0 commit comments