-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
38 lines (35 loc) · 1.54 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
let searchParam = "";
let githubAPI = "https://api.github.com/users/";
let searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-btn");
const form = document.getElementById("form");
const avatar = document.getElementById("avatar");
const userLogin = document.getElementById("user-login");
const userName = document.getElementById("user-name");
const bio = document.getElementById("bio");
form.addEventListener("submit", (e) => {
e.preventDefault();
//call the getUserData function
getUserData();
//reset all fields
searchInput.value = "";
searchParam = "";
githubAPI = "https://api.github.com/users/";
});
function getUserData() {
// If searchInput is not empty, set searchParam to searchInput's value, else return searchParam
searchInput ? searchParam = searchInput.value: searchParam;
// If searchParam is not empty, concatenate githubAPI with searchParam, else return githubAPI
searchParam ? githubAPI = `${githubAPI}${searchParam}` : githubAPI;
// fetch the user details using github API
fetch(githubAPI)
.then(response => response.json())
.then(data => {
avatar.setAttribute("src", data.avatar_url);
userLogin.innerText = `Login: ${data.login}`;
userName.innerText = `Name: ${data.name}`;
data.bio !== null ? bio.innerText = `Bio: ${data.bio}` : bio.innerText = "Bio: No bio available";
console.log(data)
})
.catch(err => console.log(err))
}