Advertisement
AviEzerzer

Untitled

Aug 27th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. {
  4.     function fetchJSON(url, cb) {
  5.         const xhr = new XMLHttpRequest();
  6.         xhr.open('GET', url);
  7.         xhr.responseType = 'json';
  8.         xhr.onload = () => {
  9.             if (xhr.status >= 200 && xhr.status <= 299) {
  10.                 cb(null, xhr.response);
  11.             } else {
  12.                 cb(new Error(`Network error: ${xhr.status} - ${xhr.statusText}`));
  13.             }
  14.         };
  15.         xhr.onerror = () => cb(new Error('Network request failed'));
  16.         xhr.send();
  17.     }
  18.  
  19.     function createAndAppend(name, parent, options = {}) {
  20.         const elem = document.createElement(name);
  21.         parent.appendChild(elem);
  22.         Object.keys(options).forEach(key => {
  23.             const value = options[key];
  24.             if (key === 'text') {
  25.                 elem.textContent = value;
  26.             } else {
  27.                 elem.setAttribute(key, value);
  28.             }
  29.         });
  30.         return elem;
  31.     }
  32.  
  33.     function main(url) {
  34.         fetchJSON(url, (err, repositories) => {
  35.             const root = document.getElementById('root');
  36.             if (err) {
  37.                 createAndAppend('div', root, { text: err.message, class: 'alert-error' });
  38.                 return;
  39.             }
  40.  
  41.             for (let repository of repositories) {
  42.                 showContributors(repository)
  43.             }
  44.  
  45.             // createAndAppend('pre', root, { text: JSON.stringify(repositories, null, 2) });
  46.         });
  47.     }
  48.  
  49.     function showContributors(data) {
  50.         let contributors = document.getElementById('contributors');
  51.         contributors.innerHTML = '';
  52.         fetchJSON(data.contributors_url, (err, listOfContributors) => {
  53.             if (listOfContributors != null && typeof listOfContributors == 'object') {
  54.                 console.log("TCL: showContributors -> listOfContributors", listOfContributors)
  55.  
  56.                 for (let contributor of listOfContributors) {
  57.                     createAndAppend('img', contributors, { src: contributor.avatar_url, class: 'contri' });
  58.                     createAndAppend('div', contributors, { text: contributor.login, class: 'contri' });
  59.                     createAndAppend('div', contributors, { text: contributor.contributions, class: 'contri' });
  60.                 }
  61.             }
  62.         })
  63.     }
  64.  
  65.     const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
  66.     window.onload = () => main(HYF_REPOS_URL);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement