|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | { |
| 4 | + const REPOS_UL_CLASS_NAME = 'repos-ul'; |
| 5 | + const LI_CLASS_NAME = 'li-class' |
| 6 | + const UL_TABLE_CLASS_NAME = 'ul-table'; |
| 7 | + const ALERT_ERROR = 'alert-error'; |
| 8 | + const HEAD_CLASS_NAME = 'head'; |
4 | 9 | function fetchJSON(url, cb) { |
5 | 10 | const xhr = new XMLHttpRequest(); |
6 | 11 | xhr.open('GET', url); |
|
29 | 34 | return elem; |
30 | 35 | } |
31 | 36 |
|
| 37 | + function createTable(repo,tableClassName) { |
| 38 | + const table = document.createElement('table'); |
| 39 | + table.setAttribute('class' , tableClassName); |
| 40 | + const tableInfo = `<tbody> |
| 41 | + <tr> |
| 42 | + <td>Repository:</td> |
| 43 | + <td><a href = "">${repo.name}</a></td> |
| 44 | + </tr> |
| 45 | + <tr> |
| 46 | + <td>Description:</td> |
| 47 | + <td>${repo.description}</td> |
| 48 | + </tr> |
| 49 | + <tr> |
| 50 | + <td>Forks:</td> |
| 51 | + <td>${repo.forks}</td> |
| 52 | + </tr> |
| 53 | + <tr> |
| 54 | + <td>Updated:</td> |
| 55 | + <td>${repo.updated_at}</td> |
| 56 | + </tr> |
| 57 | + </tbody>` |
| 58 | + table.innerHTML = tableInfo; |
| 59 | + return table; |
| 60 | + } |
32 | 61 | function renderRepoDetails(repo, ul) { |
33 | | - createAndAppend('li', ul, { text: repo.name }); |
| 62 | + createAndAppend('li', ul , { class : LI_CLASS_NAME}); |
| 63 | + const liItem = document.querySelector('li:last-child'); |
| 64 | + liItem.appendChild(createTable(repo , UL_TABLE_CLASS_NAME ) ); |
34 | 65 | } |
35 | 66 |
|
36 | | - function main(url) { |
37 | | - fetchJSON(url, (err, repos) => { |
| 67 | + function main(url,numberOfRepos) { |
| 68 | + fetchJSON(`${url}?per_page=${numberOfRepos}`, (err, repos) => { |
38 | 69 | const root = document.getElementById('root'); |
| 70 | + const head = createAndAppend('div', root, { class : HEAD_CLASS_NAME }) |
| 71 | + head.innerText = 'HYF Repositories'; |
39 | 72 | if (err) { |
40 | 73 | createAndAppend('div', root, { |
41 | 74 | text: err.message, |
42 | | - class: 'alert-error', |
| 75 | + class: ALERT_ERROR, |
43 | 76 | }); |
44 | 77 | return; |
45 | 78 | } |
46 | | - const ul = createAndAppend('ul', root); |
| 79 | + const ul = createAndAppend('ul', root, { class : REPOS_UL_CLASS_NAME}); |
47 | 80 | repos.forEach(repo => renderRepoDetails(repo, ul)); |
48 | 81 | }); |
49 | 82 | } |
50 | 83 |
|
51 | 84 | const HYF_REPOS_URL = |
52 | | - 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
53 | | - window.onload = () => main(HYF_REPOS_URL); |
| 85 | + 'https://api.github.com/orgs/HackYourFuture/repos'; |
| 86 | + window.onload = () => main(HYF_REPOS_URL,10); |
54 | 87 | } |
0 commit comments