11'use strict' ;
22
33{
4- function fetchJSON ( url , cb ) {
5- const xhr = new XMLHttpRequest ( ) ;
6- xhr . open ( 'GET' , url ) ;
7- xhr . responseType = 'json' ;
8- xhr . onload = ( ) => {
9- if ( xhr . status < 400 ) {
10- cb ( null , xhr . response ) ;
11- } else {
12- cb ( new Error ( `Network error: ${ xhr . status } - ${ xhr . statusText } ` ) ) ;
4+ function fetchJSON ( url ) {
5+ return new Promise ( ( resolve , reject ) => {
6+ let xhr = new XMLHttpRequest ( ) ;
7+ xhr . open ( 'GET' , url ) ;
8+ xhr . responseType = 'json' ;
9+ xhr . onload = function ( ) {
10+ if ( xhr . status < 400 ) {
11+ resolve ( xhr . response ) ;
12+ } else {
13+ reject ( Error ( xhr . status ) ) ;
14+ }
15+ } ;
16+ xhr . onerror = ( ) => {
17+ reject ( Error ( 'Network request failed' ) ) ;
1318 }
14- } ;
15- xhr . onerror = ( ) => cb ( new Error ( 'Network request failed' ) ) ;
16- xhr . send ( ) ;
19+ xhr . send ( ) ;
20+ } ) ;
1721 }
1822
1923 function createAndAppend ( name , parent , options = { } ) {
3034 return elem ;
3135 }
3236
33- 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 ) } ) ;
40- }
41- } ) ;
37+ function createLayout ( ) {
38+ const root = document . getElementById ( 'root' ) ;
39+ createAndAppend ( 'select' , root , { id : 'repo-select' } ) ;
40+ createAndAppend ( 'option' , document . getElementById ( 'repo-select' ) , { text : 'Choose a Repository' } ) ;
41+ createAndAppend ( 'div' , root , { id : 'body-container' } ) ;
42+ createAndAppend ( 'div' , document . getElementById ( 'body-container' ) , { id : 'repo-details' } ) ;
43+ createAndAppend ( 'div' , document . getElementById ( 'body-container' ) , { id : 'contributors' } ) ;
44+
45+ }
46+
47+ function getRepoDataFromOrgAndAddToDOM ( ) {
48+ const REPOS_URL = 'https://api.github.com/orgs/foocoding/repos?per_page=100' ;
49+ fetchJSON ( REPOS_URL ) . then ( function ( response ) {
50+ response . sort ( ( a , b ) => ( a . name . toLowerCase ( ) > b . name . toLowerCase ( ) ) ? 1 : - 1 ) ;
51+ response . forEach ( repoDataObj => {
52+ createAndAppend ( 'option' , document . getElementById ( 'repo-select' ) , { text : repoDataObj . name } )
53+ } )
54+ addListenerOnSelect ( response ) ;
55+ } )
4256 }
4357
44- const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100' ;
58+ function addDataToRepoDetails ( data ) {
59+ const repoDetailsDiv = document . getElementById ( 'repo-details' ) ;
60+ repoDetailsDiv . innerHTML = '' ;
61+ createAndAppend ( 'div' , repoDetailsDiv , { id : "repoNameID" } ) ;
62+ createAndAppend ( 'a' , document . getElementById ( "repoNameID" ) , { href : data . html_url , target : "_blank" , text : data . name } )
63+ createAndAppend ( 'div' , repoDetailsDiv , { text : `Repository Description: ${ data . description } ` } ) ;
64+ createAndAppend ( 'div' , repoDetailsDiv , { text : `Number of Forks: ${ data . forks } ` } ) ;
65+ }
66+
67+ function getContributors ( data ) {
68+ const contributorsDiv = document . getElementById ( 'contributors' ) ;
69+ contributorsDiv . innerHTML = 'Contributors:' ;
70+ fetchJSON ( data . contributors_url ) . then ( function ( response ) {
71+ response . forEach ( repoObj => {
72+ let key = repoObj . login ;
73+ createAndAppend ( 'div' , contributorsDiv , { id : key } ) ;
74+ createAndAppend ( 'a' , document . getElementById ( key ) , { href : repoObj . html_url , target : "_blank" , text : key } ) ;
75+ } )
76+ } )
77+ }
78+
79+ function addListenerOnSelect ( arrayOfRepoData ) {
80+ document . getElementById ( 'repo-select' ) . addEventListener ( 'change' , event => {
81+ const selectedRepo = event . target . value ;
82+ const selectedData = arrayOfRepoData . filter ( repoData => repoData . name === selectedRepo ) [ 0 ] ;
83+ addDataToRepoDetails ( selectedData ) ;
84+ getContributors ( selectedData ) ;
85+ } )
86+ }
87+
88+
89+
90+ function main ( ) {
91+ createLayout ( ) ;
92+ getRepoDataFromOrgAndAddToDOM ( ) ;
93+ }
4594
46- window . onload = ( ) => main ( HYF_REPOS_URL ) ;
47- }
95+ window . onload = ( ) => main ( ) ;
96+ }
0 commit comments