Advertisement
Guest User

GitHub Searcher

a guest
Apr 1st, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. const fetchRepos = search => {
  2. const url = `https://api.github.com/search/repositories?q=${search}&sort=stars&order=desc0`;
  3.  
  4. fetch(url)
  5. .then(response => response.json())
  6. .then(json => json.items)
  7. .then(reposArray => {
  8. reposArray.length = Math.min(reposArray.length, 12);
  9.  
  10. const lisRepos = reposArray.reduce((accumulator, repo) => {
  11. const shortName = repo.name.substr(0, 26);
  12.  
  13. accumulator += `
  14. <div class="card text-center border-dark">
  15. <div class="card-header text-white bg-dark">${shortName}</div>
  16. <div class="card-body">
  17. <h6 class="card-subtitle mb-2 text-muted">${repo.owner.login}</h6>
  18. <div>
  19. <img class="card-img-top w-25"src="./imgs/GitHub-Mark-black.png" alt="OctoCat">
  20. </div>
  21. <br /> <a href="${repo.html_url}" class="btn btn-info">Go Repo</a>
  22. </div>
  23. <div class="card-footer text-muted bg-light">
  24. updated at: ${repo.updated_at.slice(1, 10)}
  25. </div>
  26. </div>
  27. </div>
  28. `
  29. return accumulator
  30. }, '')
  31.  
  32. if (reposArray.length === 0) {
  33. const ul = document.querySelector('[data-js="repos"]');
  34. const error = `
  35. <div class="alert alert-danger" role="alert">Not Matched Results!</div>
  36. `
  37. ul.innerHTML = error
  38. } else {
  39.  
  40. const ul = document.querySelector('[data-js="repos"]')
  41. ul.innerHTML = lisRepos
  42. }
  43.  
  44. })
  45. }
  46.  
  47. const button = document.getElementById("button-addon1");
  48.  
  49. button.addEventListener("click", function () {
  50. const inputValue = document.getElementById("input").value;
  51. fetchRepos(inputValue)
  52. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement