|
| 1 | +const searchInput = document.querySelector('.search-input'); |
| 2 | +const searchResults = document.querySelector('.search-results'); |
| 3 | +const searchError = document.querySelector('.search-error'); |
| 4 | +const searchButton = document.querySelector('.search'); |
| 5 | +const login = document.querySelector('.login'); |
| 6 | +const loginButton = document.querySelector('.login-button'); |
| 7 | +const loginText = document.querySelector('.login-text'); |
| 8 | +const authType = document.querySelector('.auth-type'); |
| 9 | +const authTarget = document.querySelector('.auth-target'); |
| 10 | +const hitsRemaining = document.querySelector('.hits-remaining'); |
| 11 | +const hitsTotal = document.querySelector('.hits-total'); |
| 12 | +const scheme = document.querySelector('.scheme'); |
| 13 | + |
| 14 | +let localState = {}; |
| 15 | + |
| 16 | +// TODO change from javascript handler to <form> |
| 17 | +loginButton && loginButton.addEventListener('click', (evt) => { |
| 18 | + |
| 19 | + window.location = `https://github.com/login/oauth/authorize?scope=repo&client_id=${localState.clientId}&state=${localState.oAuthState}`; |
| 20 | + console.log(localState.clientId); |
| 21 | + console.log(localState.oAuthState); |
| 22 | +}); |
| 23 | + |
| 24 | +searchInput && searchInput.addEventListener('input', (evt) => { |
| 25 | + const val = evt.target.value; |
| 26 | + if (!val) { |
| 27 | + searchResults.innerHTML = ''; |
| 28 | + searchError.hidden = true; |
| 29 | + } |
| 30 | +}); |
| 31 | + |
| 32 | +searchButton && searchButton.addEventListener('click', (user) => { |
| 33 | + if (searchInput.value === '') return; |
| 34 | + searchResults.innerHTML = ''; |
| 35 | + searchError.hidden = true; |
| 36 | + search() |
| 37 | + .then(data => data.json()) |
| 38 | + .then(showResults) |
| 39 | + .then(syncState) |
| 40 | + .catch(err => { |
| 41 | + searchError.innerHTML = 'Error encountered while searching.' |
| 42 | + searchError.hidden = false; |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +function search() { |
| 47 | + return fetch(`/search/${searchInput.value}`, { |
| 48 | + headers: { |
| 49 | + "Content-Type": "application/json", |
| 50 | + } |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +function showResults(results) { |
| 55 | + // just one result from User API |
| 56 | + if (!results.items && !results.items.length) { |
| 57 | + if (results.login) { |
| 58 | + searchResults.innerHTML = `This user <a href="${results.html_url}">was found</a> on GitHub`; |
| 59 | + } |
| 60 | + else { |
| 61 | + searchResults.innerHTML = 'This user could not be found on GitHub.'; |
| 62 | + } |
| 63 | + } |
| 64 | + // array of results from Search API |
| 65 | + else if (results.items.length) { |
| 66 | + results.items.forEach(createRow); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function createRow(result) { |
| 71 | + let node = document.createElement('li'); |
| 72 | + let text = document.createTextNode(result.login) |
| 73 | + node.appendChild(text); |
| 74 | + searchResults.appendChild(node); |
| 75 | +} |
| 76 | + |
| 77 | +function updateUI() { |
| 78 | + authType.innerHTML = localState.authType; |
| 79 | + authTarget.innerHTML = localState.authTarget; |
| 80 | + hitsRemaining.innerHTML = `(${localState.rateLimitRemaining} /`; |
| 81 | + hitsTotal.innerHTML = ` ${localState.rateLimitTotal})`; |
| 82 | + |
| 83 | + if (localState.oAuthToken) { |
| 84 | + loginText.innerHTML = 'Logged in.'; |
| 85 | + loginButton.disabled = true; |
| 86 | + } |
| 87 | + |
| 88 | + if (localState.rateLimitRemaining) { |
| 89 | + scheme.hidden = false; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +function syncState() { |
| 94 | + fetch(`/state`) |
| 95 | + .then(data => data.json()) |
| 96 | + .then(remoteState => { |
| 97 | + localState = remoteState; |
| 98 | + updateUI(); |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +// this executes immediately |
| 103 | +(() => { |
| 104 | + // await this.getRateLimits(this.getQueryAuthToken()); |
| 105 | + scheme.hidden = true; |
| 106 | + syncState(); |
| 107 | +})(); |
| 108 | + |
| 109 | + |
0 commit comments