From e96549537824e0ce471ad1d0068a8c56c6e4437c Mon Sep 17 00:00:00 2001 From: Yara Alkassar Date: Mon, 1 Jun 2020 22:24:59 +0300 Subject: [PATCH] Done! Co-authored-by: Ahmed Alassaf Co-authored-by: RANDGHASSAN --- index.html | 13 ++++++++----- js/index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index bd2d837..727fa21 100644 --- a/index.html +++ b/index.html @@ -1,12 +1,14 @@ + - - - + + + Document - + +
@@ -14,7 +16,7 @@

GitHub Search

- +
@@ -30,4 +32,5 @@

GitHub Search

+ \ No newline at end of file diff --git a/js/index.js b/js/index.js index e69de29..7cc1b78 100644 --- a/js/index.js +++ b/js/index.js @@ -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 = `${user.login} +
avatar +
+
+ Profile Link +
+
+ +
+ `; + 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 = ` + ${repo.name} + `; + repoList.appendChild(repoListItem); + }) + ); + } +});