generated from elc1090/project2a
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (51 loc) · 1.91 KB
/
app.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
const gitHubForm = document.getElementById('gitHubForm');
let usernameInput = document.getElementById('username');
usernameInput.addEventListener('blur', () => {
let username = usernameInput.value;
requestUserRepos(username)
.then(response => response.json())
.then(data => {
let select = document.getElementById('repository')
if (data.message === "Not Found") {
//TODO: show error message
select.innerHTML = buildSelectOptions([]);
select.disabled = true;
} else {
select.innerHTML = buildSelectOptions(data);
select.disabled = false;
}
})
})
gitHubForm.addEventListener('submit', (e) => {
e.preventDefault();
let username = usernameInput.value;
let repository = document.getElementById('repository').value
requestCommitsFromRepos(username, repository)
.then(response => response.json())
.then(data => {
let list = document.getElementById('commits')
list.innerHTML = buildCommits(data);
})
})
function buildSelectOptions(repositories) {
let optionsHTML = "<option selected>Choose a repository</option>";
for (let repository of repositories) {
optionsHTML += `<option value="${repository.name}">${repository.name}</option>`;
}
return optionsHTML;
}
function buildCommits(commits) {
let commitsHTML = '';
for (let commitData of commits) {
commitsHTML += `<li class="list-group-item">
<p class="h6">${commitData.commit.message}</p>
</li>`;
}
return commitsHTML;
}
function requestUserRepos(username) {
return Promise.resolve(fetch(`https://api.github.com/users/${username}/repos`));
}
function requestCommitsFromRepos(username, repository) {
return Promise.resolve(fetch(`https://api.github.com/repos/${username}/${repository}/commits`));
}