Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (51 loc) · 1.57 KB
/
Copy pathscript.js
File metadata and controls
56 lines (51 loc) · 1.57 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// getData()
// search input
const search = document.querySelector('#search');
// button for the search input
const btn = document.querySelector('#btn');
const box = document.querySelector('.box');
// keypress event listener for search input
search.addEventListener('keypress', function (e) {
// if we press enter key
if (e.code == "Enter") {
// console.log('yey');
searchGitHubUser(this.value.trim())
this.value = '';
}
})
// if the button for search input is on click..
btn.addEventListener('click', function (e) {
// send the search input value which it's the previous element
const me = searchGitHubUser(this.previousElementSibling.value.trim());
this.previousElementSibling.value = '';
// then we generate card
})
async function searchGitHubUser(keyword) {
// console.log(keyword)
const me = await getData(keyword);
// console.log(me)
generateCard(me);
}
// get user data by using github api
function getData(keyword) {
return fetch(`https://api.github.com/search/users?q=${keyword}`)
.then(response => response.json())
.then(response => response)
}
// generate card
function generateCard({ items }) {
let cards = ``;
items.forEach(({ avatar_url, login }) => {
cards += `
<div class="card">
<div class="imgWrapper">
<img src="${avatar_url}" alt="">
</div>
<a href="https://github.com/${login}">
<h4 id="name">${login}</h4>
</a>
</div>
`
});
box.innerHTML = cards;
}