-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
109 lines (82 loc) · 3.47 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Listen for submissions on GitHub username input form
gitHubForm.addEventListener('submit', e => {
// Prevent default form submission action
e.preventDefault()
// Get the GitHub username input field on the DOM
let usernameInput = document.getElementById('usernameInput')
// Get the value of the GitHub username input field
let gitHubUsername = usernameInput.value
// Get tag input type submit
let bInput = document.getElementById('bInput')
// Run GitHub API function, passing in the GitHub username
// Disable the input type submit
bInput.disabled = true
// Clear the input type text
usernameInput = document.getElementById('usernameInput').value = ''
requestUser(gitHubUsername)
})
function requestUser(username) {
// Create new XMLHttpRequest object
const xhr = new XMLHttpRequest()
// GitHub endpoint, dynamically passing in specified username
const url = `https://api.github.com/users/${username}`
// Open a new connection, using a GET request via URL endpoint
// Providing 3 arguments (GET/POST, The URL, Async True/False)
xhr.open('GET', url, true)
// When request is received
// Process it here
xhr.onload = function () {
// Parse API data into JSON
const data = JSON.parse(this.response)
// Get the ul with id of of userRepos
let ul = document.getElementById('user')
// Create variable that will create li's to be added to ul
let li = document.createElement('li')
// Create the html markup for each li
li.innerHTML = `
<img style="
width: 10rem;
height: 10rem;
object-fit: cover;
border-radius: 460px;"
src="${data.html_url}.png" alt="avatar"/>
<p>${data.name}</p>
<p><img src="images/github.svg" alt="logo github" /> <a href="${data.html_url}" target="_blank">${data.login}</a></p>
<p style="max-width: 291px; text-align: justify;">${data.bio}</p>
<a href="./index.html">BACK to App Name Tag</a>
`
// Append each li to the ul
ul.appendChild(li)
}
// Send the request to the server
xhr.send()
}
// function requestUser(username) {
// // Create new XMLHttpRequest object
// const xhr = new XMLHttpRequest()
// // GitHub endpoint, dynamically passing in specified username
// const url = `https://api.github.com/users/${username}`
// // Open a new connection, using a GET request via URL endpoint
// // Providing 3 arguments (GET/POST, The URL, Async True/False)
// xhr.open('GET', url, true)
// // When request is received
// // Process it here
// xhr.onload = function () {
// // Parse API data into JSON
// const data = JSON.parse(this.response)
// // Log the response
// console.log(data)
// // Log the user name
// console.log('Name:', data.name)
// // Log the repo user login
// console.log('Login:', data.login)
// // Log the html url
// console.log('URL:', data.html_url)
// // Log the bio
// console.log('Bio:', data.bio)
// }
// // Send the request to the server
// xhr.send()
// }
// // Call function passing in 'marceloicampos' as GitHub username
// requestUser('marceloicampos')