-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclient.js
More file actions
109 lines (94 loc) · 3.07 KB
/
client.js
File metadata and controls
109 lines (94 loc) · 3.07 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
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
const searchInput = document.querySelector('.search-input');
const searchResults = document.querySelector('.search-results');
const searchError = document.querySelector('.search-error');
const searchButton = document.querySelector('.search');
const login = document.querySelector('.login');
const loginButton = document.querySelector('.login-button');
const loginText = document.querySelector('.login-text');
const authType = document.querySelector('.auth-type');
const authTarget = document.querySelector('.auth-target');
const hitsRemaining = document.querySelector('.hits-remaining');
const hitsTotal = document.querySelector('.hits-total');
const scheme = document.querySelector('.scheme');
let localState = {};
// TODO change from javascript handler to <form>
loginButton && loginButton.addEventListener('click', (evt) => {
window.location = `https://github.com/login/oauth/authorize?scope=repo&client_id=${localState.clientId}&state=${localState.oAuthState}`;
console.log(localState.clientId);
console.log(localState.oAuthState);
});
searchInput && searchInput.addEventListener('input', (evt) => {
const val = evt.target.value;
if (!val) {
searchResults.innerHTML = '';
searchError.hidden = true;
}
});
searchButton && searchButton.addEventListener('click', (user) => {
if (searchInput.value === '') return;
searchResults.innerHTML = '';
searchError.hidden = true;
search()
.then(data => data.json())
.then(showResults)
.then(syncState)
.catch(err => {
searchError.innerHTML = 'Error encountered while searching.'
searchError.hidden = false;
});
});
function search() {
return fetch(`/search/${searchInput.value}`, {
headers: {
"Content-Type": "application/json",
}
});
};
function showResults(results) {
// just one result from User API
if (!results.items && !results.items.length) {
if (results.login) {
searchResults.innerHTML = `This user <a href="${results.html_url}">was found</a> on GitHub`;
}
else {
searchResults.innerHTML = 'This user could not be found on GitHub.';
}
}
// array of results from Search API
else if (results.items.length) {
results.items.forEach(createRow);
}
}
function createRow(result) {
let node = document.createElement('li');
let text = document.createTextNode(result.login)
node.appendChild(text);
searchResults.appendChild(node);
}
function updateUI() {
authType.innerHTML = localState.authType;
authTarget.innerHTML = localState.authTarget;
hitsRemaining.innerHTML = `(${localState.rateLimitRemaining} /`;
hitsTotal.innerHTML = ` ${localState.rateLimitTotal})`;
if (localState.oAuthToken) {
loginText.innerHTML = 'Logged in.';
loginButton.disabled = true;
}
if (localState.rateLimitRemaining) {
scheme.hidden = false;
}
}
function syncState() {
fetch(`/state`)
.then(data => data.json())
.then(remoteState => {
localState = remoteState;
updateUI();
});
}
// this executes immediately
(() => {
// await this.getRateLimits(this.getQueryAuthToken());
scheme.hidden = true;
syncState();
})();