|
36 | 36 | return elem; |
37 | 37 | } |
38 | 38 |
|
| 39 | + function compare(a, b) { |
| 40 | + if (a.name<b.name){ |
| 41 | + return -1; |
| 42 | + } |
| 43 | + if (a.name>b.name){ |
| 44 | + return 1 |
| 45 | + } |
| 46 | + return 0; |
| 47 | + } |
39 | 48 |
|
40 | 49 | function main(url) { |
41 | 50 | fetchJSON(url) |
42 | 51 | .then(repositoryData => { |
43 | 52 | const select = createAndAppend('select', root); |
44 | | - createAndAppend('option', select, { text: 'Click here to choose a Repository' }); |
| 53 | + // createAndAppend('option', select, { text: 'Click here to choose a Repository' }); |
45 | 54 |
|
46 | | - repositoryData.forEach(repo => { |
| 55 | + let alphabeticalSorting = repositoryData.sort(compare); |
| 56 | + alphabeticalSorting.forEach(repo => { |
47 | 57 | const name = repo.name; |
48 | 58 | createAndAppend('option', select, { text: name }); |
49 | 59 | }); |
50 | 60 |
|
51 | 61 | const repoInfo = createAndAppend('div', forRepoBlock); |
52 | 62 | const contribs = createAndAppend('div', forContributorsBlock); |
| 63 | + |
53 | 64 | select.addEventListener('change', evt => { |
54 | 65 | const selectedRepo = evt.target.value; |
55 | | - const repo = repositoryData.filter(r => r.name == selectedRepo)[0]; |
56 | | - console.log(repo); |
57 | | - repoInfo.innerHTML = ''; |
58 | | - contribs.innerHTML = ''; |
59 | | - |
60 | | - const addInfo = (label, value) => { |
61 | | - const container = createAndAppend('div', repoInfo); |
62 | | - createAndAppend('span', container, { text: label }); |
63 | | - createAndAppend('span', container, { text: value }); |
64 | | - }; |
65 | | - addInfo('Name: ', repo.name); |
66 | | - addInfo('Desciption: ', repo.description); |
67 | | - addInfo('Number of forks: ', repo.forks); |
68 | | - addInfo('Updated: ', new Date(repo.updated_at)); |
69 | | - |
70 | | - const contribsUrl = repo.contributors_url; |
71 | | - |
72 | | - fetchJSON(contribsUrl) |
73 | | - .then((contribData) => { |
74 | | - contribData.forEach(contributor => { |
75 | | - // createAndAppend('p', contribs, { text: 'hej'}); |
76 | | - createAndAppend('img', contribs, { src: contributor.avatar_url, height: 40, class: 'picture' }); |
77 | | - createAndAppend('span', contribs, { text: contributor.login, class: 'contributorName' }); |
78 | | - createAndAppend('span', contribs, { text: contributor.contributions, class: 'numberContributions'}); |
79 | | - createAndAppend('div', contribs, { text: '\n'}); |
80 | | - }); |
81 | | - }) |
82 | | - .catch((err) => { |
83 | | - const root = document.getElementById('root'); |
84 | | - createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
85 | | - }) |
| 66 | + const repo = alphabeticalSorting.filter(r => r.name == selectedRepo)[0]; |
| 67 | + getRepoData(repo, repoInfo, contribs); |
86 | 68 | }); |
| 69 | + |
| 70 | + let firstRepo = alphabeticalSorting[0] |
| 71 | + getRepoData(firstRepo, repoInfo, contribs); |
87 | 72 | }) |
88 | 73 | .catch((err) => { |
89 | 74 | const root = document.getElementById('root'); |
90 | 75 | createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
91 | 76 | }) |
92 | 77 | } |
93 | | - |
| 78 | + |
| 79 | + function getRepoData(repo, repoInfo, contribs) { |
| 80 | + |
| 81 | + // console.log(repo); |
| 82 | + repoInfo.innerHTML = ''; |
| 83 | + contribs.innerHTML = ''; |
| 84 | + |
| 85 | + const addInfo = (label, value) => { |
| 86 | + const container = createAndAppend('div', repoInfo); |
| 87 | + createAndAppend('span', container, { text: label }); |
| 88 | + createAndAppend('span', container, { text: value }); |
| 89 | + }; |
| 90 | + addInfo('Name: ', repo.name); |
| 91 | + addInfo('Desciption: ', repo.description); |
| 92 | + addInfo('Number of forks: ', repo.forks); |
| 93 | + addInfo('Updated: ', new Date(repo.updated_at)); |
| 94 | + |
| 95 | + const contribsUrl = repo.contributors_url; |
| 96 | + |
| 97 | + fetchJSON(contribsUrl) |
| 98 | + .then((contribData) => { |
| 99 | + contribData.forEach(contributor => { |
| 100 | + // createAndAppend('p', contribs, { text: 'hej'}); |
| 101 | + createAndAppend('img', contribs, { src: contributor.avatar_url, height: 40, class: 'picture' }); |
| 102 | + createAndAppend('span', contribs, { text: contributor.login, class: 'contributorName' }); |
| 103 | + createAndAppend('span', contribs, { text: contributor.contributions, class: 'numberContributions'}); |
| 104 | + createAndAppend('div', contribs, { text: '\n'}); |
| 105 | + }); |
| 106 | + }) |
| 107 | + .catch((err) => { |
| 108 | + const root = document.getElementById('root'); |
| 109 | + createAndAppend('div', root, { text: err.message, class: 'alert-error' }); |
| 110 | + }) |
| 111 | + |
| 112 | + } |
94 | 113 |
|
95 | 114 | const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100'; |
96 | 115 |
|
97 | 116 | window.onload = () => main(HYF_REPOS_URL); |
98 | 117 | } |
| 118 | + |
| 119 | + |
0 commit comments