-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (48 loc) · 1.62 KB
/
script.js
File metadata and controls
62 lines (48 loc) · 1.62 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
57
58
59
60
61
62
const userName = document.querySelector('.user')
const searchBtn = document.querySelector('.searchBtn')
const repositories = document.querySelector('.repositories')
const loading = document.querySelector('.hidden')
const withoutRepos = document.querySelector('.without-repos')
function hiddenElement(element, targetClass) {
element.classList.add(targetClass)
}
function showElement(element, targetClass) {
element.classList.remove(targetClass)
}
searchBtn.onclick = ev => {
ev.preventDefault()
useApi(user.value)
repositories.innerText = ""
withoutRepos.removeAttribute('id', 'with-repos')
showElement(loading, 'hidden')
}
function getName(response) {
/* for(let repo of response.data) {
const { name } = repo
console.log(name)
} */
for (let repo of response.data) {
const repository = document.createElement('li')
const a = document.createElement('a')
a.setAttribute('href', `${repo.html_url}`)
a.setAttribute('target', "_blank")
a.innerText = `${repo.name}`
repository.appendChild(a)
repositories.appendChild(repository)
}
}
async function useApi(user) {
try {
let response = await axios.get(`https://api.github.com/users/${user}/repos`)
getName(response)
response.data.length ?
withoutRepos.setAttribute('id', 'with-repos')
: alert('No momento este usuário não possui repositórios públicos!')
}
catch (error) {
if (error.response.status === 404) {
alert('Usuario Não Encontrado!')
}
}
hiddenElement(loading, 'hidden')
}