Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h2>GitHub Search</h2>

<form id='github-form'>
<input id='search' type='text' name='search'>
<input type='submit' name='submit'/>
<input type='submit' name='submit' id="submit"/>
</form>

<div id='github-container'>
Expand Down
78 changes: 78 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

window.onload = function(){
//first fetch
function fetchUserData(queryStrig = undefined){
let url = "https://api.github.com/search/users?q=octocat";
if(queryStrig) url = "https://api.github.com/search/users?q="+queryStrig;
return fetch(url)
.then(function(response){
return response.json();
})
.then(function(json){
userEndPoint(json);
})
}
//appending first fetch data to the DOM
function userEndPoint(data){
let ul = document.querySelector('#user-list');
ul.innerHTML="";
for(const user of data.items){
let lis = document.createElement('li');
let div = document.createElement('div');

let p = document.createElement('p');
p.innerHTML = ` ${user.login}`;
div.appendChild(p);

let img = document.createElement('img');
img.src = user.avatar_url;
div.appendChild(img);

let p2 = document.createElement('p');
let btn= document.createElement('button');
btn.innerHTML = "User Repo";
p2.appendChild(btn);
div.appendChild(p2);
btn.addEventListener('click',fetchUserRepo);
repoList.innerHTML="";
btn.user = user.login;

lis.appendChild(div);
ul.appendChild(lis);
}
}


//addng event listener to submit btn
let input = document.querySelector('#submit');
input.addEventListener('click',function(e){
e.preventDefault();
let nameInput = document.getElementById('search');
fetchUserData(nameInput.value);
});


//second fetch
function fetchUserRepo(e){
return fetch('https://api.github.com/users/'+e.target.user+'/repos')
.then(function(response)
{
return response.json();
})

.then(function(json){

repoEndPoint(json);
})
}
//appending second fetch data to the DOM
let repoList = document.querySelector('#repos-list');
function repoEndPoint(data){
for(const repo of data){
let lis = document.createElement('li');
lis.innerHTML = repo.name;
repoList.appendChild(lis);
}
}
}