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
13 changes: 8 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<html>

<head>
<meta charset='UTF-8'/>
<meta name='viewport' content='width=device-width, initial-scale=1.0'/>
<meta http-equiv='X-UA-Compatible' content='ie=edge'/>
<meta charset='UTF-8' />
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<meta http-equiv='X-UA-Compatible' content='ie=edge' />
<title>Document</title>
<script src='js/index.js'></script>
<link rel='stylesheet' href='index.css'/>
<link rel='stylesheet' href='index.css' />
</head>

<body>
<div id='main'>

<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' />
</form>

<div id='github-container'>
Expand All @@ -30,4 +32,5 @@ <h2>GitHub Search</h2>
</div>

</body>

</html>
49 changes: 49 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
document.addEventListener("DOMContentLoaded", function () {
const form = document.getElementsByTagName("form")[0];
const userList = document.getElementById("user-list");
const repoList = document.getElementById("repos-list");
const inputVal = document.getElementById("search");
form.addEventListener("submit", searchUser);
function searchUser(e) {
e.preventDefault();
userList.innerHTML = "";
repoList.innerHTML = "";
return fetch(`https://api.github.com/search/users?q=${inputVal.value}`)
.then((response) => response.json())
.then((profile) => {
profile.items.forEach((user) => {
let listItem = document.createElement("li");
listItem.innerHTML = `<b>${user.login}</b>
<div style="margin:20px"> <img src="${user.avatar_url}" alt="avatar" height="150px">
</div>
<div style="margin:20px">
<a href="${user.html_url}">Profile Link</a>
</div>
<div style="margin:20px">
<button type="button" class="repo-btn">Click Me to show Repos!</button>
</div>
`;
let btn = listItem.querySelector(".repo-btn");
btn.addEventListener("click", function () {
searchRepo(user);
});
userList.appendChild(listItem);
});
inputVal.value = "";
});
}

function searchRepo(user) {
return fetch(`https://api.github.com/users/${user.login}/repos`)
.then((response) => response.json())
.then((json) =>
json.forEach((repo) => {
let repoListItem = document.createElement("li");
repoListItem.innerHTML = `
<a href="${repo.html_url}">${repo.name}</a>
`;
repoList.appendChild(repoListItem);
})
);
}
});