|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +/* global Util, Repository, Contributor */ |
| 4 | + |
| 5 | +class App { |
| 6 | + constructor(url) { |
| 7 | + this.initialize(url); |
| 8 | + } |
| 9 | + |
| 10 | + /** |
| 11 | + * Initialization |
| 12 | + * @param {string} url The GitHub URL for obtaining the organization's repositories. |
| 13 | + */ |
| 14 | + async initialize(url) { |
| 15 | + // Add code here to initialize your app |
| 16 | + // 1. Create the fixed HTML elements of your page |
| 17 | + |
| 18 | + //Design the page by using DOM manipulation by calling a function from Util : |
| 19 | + Util.createPage(); |
| 20 | + |
| 21 | + // 2. Make an initial XMLHttpRequest using Util.fetchJSON() to populate your <select> element |
| 22 | + |
| 23 | + Util.fetchJSON(HYF_REPOS_URL); |
| 24 | + |
| 25 | + // ... |
| 26 | + |
| 27 | + try { |
| 28 | + // ... |
| 29 | + const repos = await Util.fetchJSON(url); |
| 30 | + Util.fillCombobox(repos); |
| 31 | + Util.comboChangeListener(repos); |
| 32 | + Util.dataRender(repos); |
| 33 | + // ... |
| 34 | + } catch (error) { |
| 35 | + this.renderError(error); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Removes all child elements from a container element |
| 41 | + * @param {*} container Container element to clear |
| 42 | + */ |
| 43 | + clearContainer(container) { |
| 44 | + while (container.firstChild) { |
| 45 | + container.removeChild(container.firstChild); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Fetch contributor information for the selected repository and render the |
| 51 | + * repo and its contributors as HTML elements in the DOM. |
| 52 | + * @param {number} index The array index of the repository. |
| 53 | + */ |
| 54 | + async fetchContributorsAndRender(index) { |
| 55 | + try { |
| 56 | + const repo = this.repos[index]; |
| 57 | + const contributors = await repo.fetchContributors(); |
| 58 | + |
| 59 | + const container = document.getElementById("container"); |
| 60 | + this.clearContainer(container); |
| 61 | + |
| 62 | + const leftDiv = Util.createAndAppend("div", container); |
| 63 | + const rightDiv = Util.createAndAppend("div", container); |
| 64 | + |
| 65 | + const contributorList = Util.createAndAppend("ul", rightDiv); |
| 66 | + |
| 67 | + repo.render(leftDiv); |
| 68 | + |
| 69 | + contributors |
| 70 | + .map(contributor => new Contributor(contributor)) |
| 71 | + .forEach(contributor => contributor.render(contributorList)); |
| 72 | + } catch (error) { |
| 73 | + this.renderError(error); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Render an error to the DOM. |
| 79 | + * @param {Error} error An Error object describing the error. |
| 80 | + */ |
| 81 | + renderError(error) { |
| 82 | + alert(error); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const HYF_REPOS_URL = |
| 87 | + "https://api.github.com/orgs/HackYourFuture/repos?per_page=100"; |
| 88 | + |
| 89 | +window.onload = () => new App(HYF_REPOS_URL); |
0 commit comments