generated from elc1090/project2a
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
72 lines (57 loc) · 2.89 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
// Get the GitHub username input form
const gitHubForm = document.getElementById('gitHubForm');
// 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 the repo input field on the DOM
let repoInput = document.getElementById('repoInput');
// Get the value of the repo input field
let userRepository = repoInput.value;
// Run GitHub API function, passing in the GitHub username
requestUserRepoCommits(gitHubUsername, userRepository)
.then(response => response.json()) // parse response into json
.then(data => {
// update html with data from github
if (data.message === "Not Found") {
let ul = document.getElementById('userRepoCommits');
ul.innerHTML = '';
// Create variable that will create li's to be added to ul
let li = document.createElement('li');
// Add Bootstrap list item class to each li
li.classList.add('list-group-item')
// Create the html markup for each li
li.innerHTML = (`
<p><strong>No account exists with username:</strong> ${gitHubUsername}</p>
`);
// Append each li to the ul
ul.appendChild(li);
} else {
let ul = document.getElementById('userRepoCommits');
ul.innerHTML = '';
for (let i in data) {
// Create variable that will create li's to be added to ul
let li = document.createElement('li');
// Add Bootstrap list item class to each li
li.classList.add('list-group-item')
// Create the html markup for each li
li.innerHTML = (`
<h3><strong>Author:</strong> ${data[i].commit.committer.name}</h3>
<p><strong>Commit Msg:</strong> ${data[i].commit.message}</p>
<p><strong>Data:</strong> ${data[i].commit.committer.date}</p>
`);
// Append each li to the ul
ul.appendChild(li);
}
}
});
})
function requestUserRepoCommits(username, reponame) {
// create a variable to hold the `Promise` returned from `fetch`
// return Promise.resolve(fetch(`https://api.github.com/users/${username}/repos`));
return Promise.resolve(fetch(`https://api.github.com/repos/${username}/${reponame}/commits`));
}