Created
December 4, 2018 18:25
-
-
Save DiegoPinho/bc5eac94877a507225c55a9dacc42947 to your computer and use it in GitHub Desktop.
Avanade Academy | JulianaAugusta/github-wars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urlString = window.location.href; | |
var url = new URL(urlString); | |
var usuario1 = url.searchParams.get("user1"); | |
var usuario2 = url.searchParams.get("user2"); | |
document.getElementById('jogador1').innerHTML = usuario1; | |
document.getElementById('jogador2').innerHTML = usuario2; | |
fotoUsuario1 = document.getElementById('img_user1'); | |
fotoUsuario2 = document.getElementById('img_user2'); | |
getApi(1, usuario1, function() { | |
getApi(2, usuario2, function() { | |
// TODO: fazer código que compara e mostra quem venceu | |
}); | |
}); | |
function getApi(index, usersName, callback) { | |
var usuario = new XMLHttpRequest(); | |
var usuarioRepo = new XMLHttpRequest(); | |
usuario.open('GET', 'https://api.github.com/users/' + usersName, true); | |
usuarioRepo.open('GET', 'https://api.github.com/users/' + usersName + '/repos', true); | |
usuario.send(); | |
usuarioRepo.send(); | |
usuario.onreadystatechange = function () { | |
if (usuario.readyState == 4 && usuario.status == 200) { | |
usuarioRepo.onreadystatechange = function () { | |
if (usuarioRepo.readyState == 4 && usuarioRepo.status == 200) { | |
var repositorio = JSON.parse(usuarioRepo.responseText); | |
var dados = JSON.parse(usuario.responseText); | |
getPoints(index, dados, repositorio); | |
if(callback) callback(); | |
} | |
} | |
} | |
} | |
} | |
function getPoints(index, dados, repositorio) { | |
var fotoUsuario = document.getElementById('img_user' + index); | |
fotoUsuario.setAttribute('src', dados.avatar_url); | |
var countStar = 0; | |
for (let i = 0; i < repositorio.length; i++) { | |
let stars = repositorio[i].stargazers_count; | |
countStar += stars; | |
} | |
var pontosRepos = dados.public_repos * 20 | |
var pontosSeguidores = dados.followers * 10; | |
var pontosSeguindo = dados.following * 5; | |
var pontosGists = dados.public_gists * 5; | |
var bio = dados.bio; | |
var total = pontosRepos + pontosSeguidores + pontosSeguindo + countStar + pontosGists; | |
return escreve( | |
index, | |
pontosRepos, | |
pontosSeguidores, | |
pontosSeguindo, | |
countStar, | |
pontosGists, | |
bio, | |
total | |
); | |
} | |
function escreve(index, pontosRepos, pontosSeguidores, pontosSeguindo, countStar, pontosGists, bio, total) { | |
document.getElementById('public-repos' + index).innerHTML = pontosRepos; | |
document.getElementById('seguidores' + index).innerHTML = pontosSeguidores; | |
document.getElementById('seguindo' + index).innerHTML = pontosSeguindo; | |
document.getElementById('estrelas' + index).innerHTML = countStar; | |
document.getElementById('gists' + index).innerHTML = pontosGists | |
if (bio === null) { | |
document.getElementById('bio' + index).innerHTML = 0; | |
} | |
else { | |
document.getElementById('bio' + index).innerHTML = bio.length; | |
} | |
document.getElementById('total' + index).innerHTML = total | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment