|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +// Using promise fetch the URL. |
3 | 4 | {
|
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 < 400) { |
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(); |
| 5 | + function fetchUrl(url) { |
| 6 | + return fetch(url) |
| 7 | + .then(result => { |
| 8 | + return result.json(); |
| 9 | + }) |
| 10 | + .then(data => { |
| 11 | + return data; |
| 12 | + }) |
| 13 | + .catch(error => { |
| 14 | + console.log(`check the URL address${error}`); |
| 15 | + }); |
17 | 16 | }
|
18 | 17 |
|
19 | 18 | function createAndAppend(name, parent, options = {}) {
|
|
30 | 29 | return elem;
|
31 | 30 | }
|
32 | 31 |
|
33 |
| - function main(url) { |
34 |
| - fetchJSON(url, (err, data) => { |
35 |
| - const root = document.getElementById('root'); |
36 |
| - if (err) { |
37 |
| - createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
38 |
| - } else { |
39 |
| - createAndAppend('pre', root, { text: JSON.stringify(data, null, 2) }); |
| 32 | + // Load the Contributors information into section2 div element. |
| 33 | + |
| 34 | + function addContributors(contribution) { |
| 35 | + const divElement = []; |
| 36 | + |
| 37 | + divElement.push('<div id="header">', `${'<h3>'} Contributions: ${'</h3>'} ${'</div>'}`); |
| 38 | + |
| 39 | + contribution.forEach(details => { |
| 40 | + divElement.push( |
| 41 | + '<ul>', |
| 42 | + `${'<li>'} ${'<img src="'}${details.avatar_url}" width="40" height="40">`, |
| 43 | + `</li>`, |
| 44 | + `<li>${details.login}</li>`, |
| 45 | + `<li>${details.contributions}</li>`, |
| 46 | + '</ul>', |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + const htmlString = divElement.join(''); |
| 51 | + document.getElementById('contributor-information').innerHTML = htmlString; |
| 52 | + } |
| 53 | + |
| 54 | + // Load the Repository information into section1 div element. |
| 55 | + |
| 56 | + function loadRepoDetails(repoInfo, optionValue) { |
| 57 | + const templateElement = []; |
| 58 | + |
| 59 | + Object.keys(repoInfo).forEach(key => { |
| 60 | + if (optionValue === repoInfo[key].name) { |
| 61 | + // format the date |
| 62 | + const upDate = new Date(repoInfo[key].updated_at); |
| 63 | + const amOrPm = upDate.getHours() < 12 ? 'AM' : 'PM'; |
| 64 | + const dateHours = upDate.getHours() % 12 || 12; |
| 65 | + const formatedUpdate = `${upDate.getMonth()}/${upDate.getDate()}/${upDate.getFullYear()} ${dateHours} |
| 66 | + : ${upDate.getMinutes()}:${upDate.getSeconds()} ${amOrPm}`; |
| 67 | + templateElement.push( |
| 68 | + '<div id="row">', |
| 69 | + `${'<p id="name-info">'} Repository name : ${'<a href="'}${ |
| 70 | + repoInfo[key].html_url |
| 71 | + }"${'/>'} ${repoInfo[key].name}</a></p>`, |
| 72 | + `${'<p id="desc">'} Description : ${repoInfo[key].description}</p>`, |
| 73 | + `${'<p id="forks">'} Forks : ${repoInfo[key].forks_count}</p>`, |
| 74 | + |
| 75 | + `${'<p id="updated">'} Updated : ${formatedUpdate}</p>`, |
| 76 | + '</div>', |
| 77 | + ); |
| 78 | + fetchUrl(repoInfo[key].contributors_url).then(responseData => { |
| 79 | + addContributors(responseData); |
| 80 | + }); |
40 | 81 | }
|
41 | 82 | });
|
| 83 | + const htmlString = templateElement.join(''); |
| 84 | + document.getElementById('repo-details').innerHTML = htmlString; |
| 85 | + } |
| 86 | + |
| 87 | + // Create options under 'SELECT' element which should have HYF Repositories. |
| 88 | + |
| 89 | + function loadSelectionValues(userRepo) { |
| 90 | + const sortRepoName = []; |
| 91 | + |
| 92 | + const selectRepo = document.getElementById('select-repo'); |
| 93 | + |
| 94 | + // push all the HYP repo names to sort array. |
| 95 | + |
| 96 | + Object.keys(userRepo).forEach(key => { |
| 97 | + sortRepoName.push(userRepo[key].name); |
| 98 | + }); |
| 99 | + |
| 100 | + // sort the repo name using sort function and localeComapare for uppercase and lowercase sorting. |
| 101 | + sortRepoName.sort((a, b) => a.localeCompare(b)); |
| 102 | + |
| 103 | + // Create Option under Select element and attach the same with SELECT element. |
| 104 | + |
| 105 | + sortRepoName.forEach(repo => { |
| 106 | + const option = document.createElement('option'); |
| 107 | + option.value = repo; |
| 108 | + option.text = repo; |
| 109 | + selectRepo.appendChild(option); |
| 110 | + }); |
| 111 | + |
| 112 | + const selectBox = document.getElementById('select-repo'); |
| 113 | + |
| 114 | + // Load Repository information for the choose repository name in the select box. |
| 115 | + loadRepoDetails(userRepo, selectBox.value); |
| 116 | + selectBox.onchange = function() { |
| 117 | + loadRepoDetails(userRepo, selectBox.value); |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + function main(url) { |
| 122 | + const BodyEl = document.body; |
| 123 | + const root = document.getElementById('root'); |
| 124 | + |
| 125 | + // Call the fetchUrl function to fetch Repository information. The function will in tern returns promise. |
| 126 | + const data = fetchUrl(url).then(responseData => { |
| 127 | + loadSelectionValues(responseData); |
| 128 | + }); |
| 129 | + |
| 130 | + // Create div element 'select' in document body to hold the label element and list box. |
| 131 | + |
| 132 | + createAndAppend('div', BodyEl, { id: 'select-container' }); |
| 133 | + const select = document.getElementById('select-container'); |
| 134 | + createAndAppend('LABEL', select, { |
| 135 | + text: 'HYF Repositories: ', |
| 136 | + id: 'label', |
| 137 | + for: 'repo', |
| 138 | + }); |
| 139 | + createAndAppend('select', select, { id: 'select-repo' }); |
| 140 | + |
| 141 | + // Create two div elements section1 and section2 under 'Root' div to have |
| 142 | + // section1 - Repository Information. |
| 143 | + // section2 - Contributions. |
| 144 | + |
| 145 | + createAndAppend('div', root, { id: 'repo-details' }); |
| 146 | + createAndAppend('div', root, { id: 'contributor-information' }); |
| 147 | + |
| 148 | + // Insert section1 before section2 div element under 'root' div. |
| 149 | + const newNode = document.getElementById('contributor-information'); |
| 150 | + const referenceNode = document.querySelector('repo-details'); |
| 151 | + root.insertBefore(newNode, referenceNode); |
| 152 | + |
| 153 | + // Insert Select div first in the body before root div. |
| 154 | + BodyEl.insertBefore(select, document.getElementById('root')); |
| 155 | + |
| 156 | + loadSelectionValues(data); |
42 | 157 | }
|
43 | 158 |
|
44 | 159 | const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100';
|
|
0 commit comments