|
| 1 | +'use strict'; |
| 2 | + |
| 3 | + |
| 4 | +const promiseToGetXMLHttpRequest = new Promise( function( resolve , reject ){ |
| 5 | + |
| 6 | + // Make a promise by get a XMLHttpRequest response |
| 7 | + |
| 8 | + const xhr = new XMLHttpRequest(); |
| 9 | + xhr.open( 'GET' , 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100%27' ); |
| 10 | + xhr.onload = () => { |
| 11 | + if (xhr.status == 200 ) { // If successful, resolve the promise by passing back the request response as a string |
| 12 | + resolve(xhr.responseText); |
| 13 | + } else { // If it fails, reject the promise with a error message as a string |
| 14 | + reject("Network error:" + xhr.status +' - '+ xhr.statusText); |
| 15 | + } |
| 16 | + }; |
| 17 | + xhr.onerror = () => reject("Network error:" + xhr.status +' - '+ xhr.statusText); |
| 18 | + xhr.send(); |
| 19 | + |
| 20 | +} ); |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +promiseToGetXMLHttpRequest.then( function(responseText){ // In state of "fulfilled" |
| 25 | + const repos = JSON.parse( responseText ); |
| 26 | + // |
| 27 | + var output = ''; |
| 28 | + for(let i in repos){ |
| 29 | + output += '<option value="' + repos[i].name +'">' + repos[i].name + '</option>'; |
| 30 | + } |
| 31 | + // |
| 32 | + document.getElementById('reposs').innerHTML = output; |
| 33 | + document.getElementById('users').classList.remove('NetworkError'); |
| 34 | + |
| 35 | +} ).catch( function(status){ // In state of "rejected" |
| 36 | + document.getElementById('users').classList.add('NetworkError'); |
| 37 | + document.getElementById('users').innerHTML = status; |
| 38 | +} ); |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +document.getElementById('reposs').addEventListener("change" , loadUsers); |
| 45 | + |
| 46 | +document.getElementById('reposs').addEventListener("change" , loadUsers2); |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +function loadUsers() { |
| 52 | + const option = this.options[this.selectedIndex].value; |
| 53 | + const promiseToGetXMLHttpRequest = new Promise( function( resolve , reject ){ // Make a promise by get a XMLHttpRequest response |
| 54 | + const xhr = new XMLHttpRequest(); |
| 55 | + xhr.open( 'GET' , 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100%27' ); |
| 56 | + xhr.onload = () => { |
| 57 | + if (xhr.status == 200 ) { // If successful, resolve the promise by passing back the request response as a string |
| 58 | + resolve(xhr.responseText); |
| 59 | + } else { // If it fails, reject the promise with a error message as a string |
| 60 | + reject("Network error:" + xhr.status +' - '+ xhr.statusText); |
| 61 | + } |
| 62 | + }; |
| 63 | + xhr.onerror = () => reject("Network error:" + xhr.status +' - '+ xhr.statusText); |
| 64 | + xhr.send(); |
| 65 | + |
| 66 | + } ); |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + promiseToGetXMLHttpRequest.then( function(responseText){ // In state of "fulfilled" |
| 71 | + const repos = JSON.parse( responseText ); |
| 72 | + // |
| 73 | + var output = ''; |
| 74 | + for(let i in repos){ |
| 75 | + if(option == repos[i].name) { |
| 76 | + output = '<div class="user">' + |
| 77 | + '<ul>' + |
| 78 | + '<li><span>Repository:</span> ' + repos[i].name +'</li>' + |
| 79 | + '<li><span>Description:</span> ' + repos[i].description +'</li>' + |
| 80 | + '<li><span>Forks:</span> ' + repos[i].forks +'</li>' + |
| 81 | + '<li><span>Updated:</span> ' + repos[i].updated_at +'</li>' + |
| 82 | + '</ul>' + |
| 83 | + '</div>'; |
| 84 | + } |
| 85 | + } |
| 86 | + // |
| 87 | + document.getElementById('users').innerHTML = output; |
| 88 | + document.getElementById('users').classList.remove('NetworkError'); |
| 89 | + |
| 90 | + } ).catch( function(status){ // In state of "rejected" |
| 91 | + document.getElementById('users').classList.add('NetworkError'); |
| 92 | + document.getElementById('users').innerHTML = status; |
| 93 | + } ); |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +function loadUsers2(){ |
| 100 | + const option = this.options[this.selectedIndex].value; |
| 101 | + const url = `https://api.github.com/repos/HackYourFuture/${option}/contributors`; |
| 102 | + |
| 103 | + const promiseToGetXMLHttpRequest = new Promise( function( resolve , reject ){ // Make a promise by get a XMLHttpRequest response |
| 104 | + |
| 105 | + const xhr = new XMLHttpRequest(); |
| 106 | + xhr.open( 'GET' , url ); |
| 107 | + xhr.onload = () => { |
| 108 | + if (xhr.status == 200 ) { // If successful, resolve the promise by passing back the request response as a string |
| 109 | + resolve(xhr.responseText); |
| 110 | + } else { // If it fails, reject the promise with a error message as a string |
| 111 | + reject("Network error:" + xhr.status +' - '+ xhr.statusText); |
| 112 | + } |
| 113 | + }; |
| 114 | + xhr.onerror = () => reject("Network error:" + xhr.status +' - '+ xhr.statusText); |
| 115 | + xhr.send(); |
| 116 | + |
| 117 | + } ); |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + promiseToGetXMLHttpRequest.then( function(responseText){ // In state of "fulfilled" |
| 122 | + const repos = JSON.parse( responseText ); |
| 123 | + // |
| 124 | + var output = ''; |
| 125 | + for(let i in repos){ |
| 126 | + output += '<div class="user">' + |
| 127 | + '<img src="' + repos[i].avatar_url + '" width="70" height="70">' + |
| 128 | + '<ul>' + |
| 129 | + '<li><span>name:</span> ' + repos[i].login +'</li>' + |
| 130 | + '<li><span>contributions:</span> ' + repos[i].contributions +'</li>' + |
| 131 | + '</ul>' + |
| 132 | + '</div>'; |
| 133 | + } |
| 134 | + // |
| 135 | + document.getElementById('users2').innerHTML = output; |
| 136 | + document.getElementById('users2').classList.remove('NetworkError'); |
| 137 | + |
| 138 | + |
| 139 | + } ).catch( function(status){ // In state of "rejected" |
| 140 | + document.getElementById('users2').classList.add('NetworkError'); |
| 141 | + document.getElementById('users2').innerHTML = status; |
| 142 | + } ); |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
0 commit comments