@@ -14,77 +14,51 @@ gitHubForm.addEventListener('submit', (e) => {
1414 let gitHubUsername = usernameInput . value ;
1515
1616 // Run GitHub API function, passing in the GitHub username
17- requestUserRepos ( gitHubUsername ) ;
18-
19- } )
20-
21-
22- function requestUserRepos ( username ) {
23-
24- // Create new XMLHttpRequest object
25- const xhr = new XMLHttpRequest ( ) ;
26-
27- // GitHub endpoint, dynamically passing in specified username
28- const url = `https://api.github.com/users/${ username } /repos` ;
29-
30- // Open a new connection, using a GET request via URL endpoint
31- // Providing 3 arguments (GET/POST, The URL, Async True/False)
32- xhr . open ( 'GET' , url , true ) ;
17+ requestUserRepos ( gitHubUsername )
18+ . then ( response => response . json ( ) ) // parse response into json
19+ . then ( data => {
20+ // update html with data from github
21+ for ( let i in data ) {
22+ // Get the ul with id of userRepos
3323
34- // When request is received
35- // Process it here
36- xhr . onload = function ( ) {
24+ if ( data . message === "Not Found" ) {
25+ let ul = document . getElementById ( 'userRepos' ) ;
3726
38- // Parse API data into JSON
39- const data = JSON . parse ( this . response ) ;
40- let root = document . getElementById ( 'userRepos' ) ;
41- while ( root . firstChild ) {
42- root . removeChild ( root . firstChild ) ;
43- }
44- if ( data . message === "Not Found" ) {
45- let ul = document . getElementById ( 'userRepos' ) ;
27+ // Create variable that will create li's to be added to ul
28+ let li = document . createElement ( 'li' ) ;
4629
47- // Create variable that will create li's to be added to ul
48- let li = document . createElement ( 'li' ) ;
30+ // Add Bootstrap list item class to each li
31+ li . classList . add ( 'list-group-item' )
32+ // Create the html markup for each li
33+ li . innerHTML = ( `
34+ <p><strong>No account exists with username:</strong> ${ gitHubUsername } </p>` ) ;
35+ // Append each li to the ul
36+ ul . appendChild ( li ) ;
37+ } else {
4938
50- // Add Bootstrap list item class to each li
51- li . classList . add ( 'list-group-item' )
52- // Create the html markup for each li
53- li . innerHTML = ( `
54- <p><strong>No account exists with username:</strong> ${ username } </p>` ) ;
55- // Append each li to the ul
56- ul . appendChild ( li ) ;
57- } else {
39+ let ul = document . getElementById ( 'userRepos' ) ;
5840
59- // Get the ul with id of of userRepos
60- let ul = document . getElementById ( 'userRepos' ) ;
61- let p = document . createElement ( 'p' ) ;
62- p . innerHTML = ( `<p><strong>Number of Public Repos:${ data . length } </p>` )
63- ul . appendChild ( p ) ;
64- // Loop over each object in data array
65- for ( let i in data ) {
66- // Create variable that will create li's to be added to ul
67- let li = document . createElement ( 'li' ) ;
41+ // Create variable that will create li's to be added to ul
42+ let li = document . createElement ( 'li' ) ;
6843
69- // Add Bootstrap list item class to each li
70- li . classList . add ( 'list-group-item' )
44+ // Add Bootstrap list item class to each li
45+ li . classList . add ( 'list-group-item' )
7146
72- // Create the html markup for each li
73- li . innerHTML = ( `
47+ // Create the html markup for each li
48+ li . innerHTML = ( `
7449 <p><strong>Repo:</strong> ${ data [ i ] . name } </p>
7550 <p><strong>Description:</strong> ${ data [ i ] . description } </p>
7651 <p><strong>URL:</strong> <a href="${ data [ i ] . html_url } ">${ data [ i ] . html_url } </a></p>
7752 ` ) ;
7853
79- // Append each li to the ul
80- ul . appendChild ( li ) ;
81-
54+ // Append each li to the ul
55+ ul . appendChild ( li ) ;
56+ }
8257 }
58+ } )
59+ } )
8360
84- }
85- }
86-
87- // Send the request to the server
88- xhr . send ( ) ;
89-
90- }
61+ function requestUserRepos ( username ) {
62+ // create a variable to hold the `Promise` returned from `fetch`
63+ return Promise . resolve ( fetch ( `https://api.github.com/users/${ username } /repos` ) ) ;
64+ }
0 commit comments