11'use strict' ;
22
3- /* global Util, Repository, Contributor */
4-
53class App {
64 constructor ( url ) {
7- this . mainContainer = null ;
85 this . initialize ( url ) ;
96 }
107
118 /**
12- * Initialization
139 * @param {string } url The GitHub URL for obtaining the organization's repositories.
1410 */
1511 async initialize ( url ) {
16- // Add code here to initialize your app
17- // 1. Create the fixed HTML elements of your page
18- // 2. Make an initial XMLHttpRequest using Util.fetchJSON() to populate your <select> element
19-
2012 const root = document . getElementById ( 'root' ) ;
21- const header = Util . createAndAppend ( 'header' , root , { class : 'header' } ) ;
22- this . mainContainer = Util . createAndAppend ( 'div' , root , { id : 'container' } ) ;
13+ Util . createAndAppend ( 'h1' , root , { text : 'REPOSITORIES' } ) ;
14+ Util . createAndAppend ( 'select' , root , { id : 'dropdown-select' } ) ;
15+ Util . createAndAppend ( 'div' , root , { id : 'details' } ) ;
16+ Util . createAndAppend ( 'div' , root , { id : 'contributors' } ) ;
2317
2418 try {
2519 const repos = await Util . fetchJSON ( url ) ;
2620 this . repos = repos . map ( repo => new Repository ( repo ) ) ;
27- // TODO: add your own code here
21+ this . repos . forEach ( repoDataObj => {
22+ repoDataObj . render ( document . getElementById ( 'dropdown-select' ) ) ;
23+ } ) ;
2824 } catch ( error ) {
2925 this . renderError ( error ) ;
3026 }
27+ document . getElementById ( 'dropdown-select' ) . addEventListener ( 'change' , event => {
28+ const selectedRepo = event . target . value ;
29+ const selectedData = this . repos . filter (
30+ repoData => repoData . repository . name === selectedRepo ,
31+ ) [ 0 ] ;
32+ const index = this . repos . indexOf ( selectedData ) ;
33+ this . fetchContributorsAndRender ( index ) ;
34+ } ) ;
3135 }
3236
3337 /**
34- * Removes all child elements from a container element
3538 * @param {* } container Container element to clear
3639 */
37- clearContainer ( ) {
38- while ( this . mainContainer . firstChild ) {
39- this . mainContainer . removeChild ( this . mainContainer . firstChild ) ;
40+ static clearContainer ( container ) {
41+ while ( container . firstChild ) {
42+ container . removeChild ( container . firstChild ) ;
4043 }
4144 }
4245
4346 /**
44- * Fetch contributor information for the selected repository and render the
45- * repo and its contributors as HTML elements in the DOM.
46- * @param {object } repo The selected repository object
47+ * @param {number } index The array index of the repository.
4748 */
48- async selectRepository ( repo ) {
49+ async fetchContributorsAndRender ( index ) {
4950 try {
50- this . clearContainer ( ) ;
51+ const repo = this . repos [ index ] ;
5152 const contributors = await repo . fetchContributors ( ) ;
52-
53- const repoContainer = Util . createAndAppend ( 'div' , this . mainContainer ) ;
54- const contributorContainer = Util . createAndAppend ( 'div' , this . mainContainer ) ;
55-
56- const contributorList = Util . createAndAppend ( 'ul' , contributorContainer ) ;
57-
58- repo . render ( repoContainer ) ;
59-
53+ const container = document . getElementById ( 'contributors' ) ;
54+ App . clearContainer ( container ) ;
55+ const leftDiv = Util . createAndAppend ( 'div' , container ) ;
56+ const rightDiv = Util . createAndAppend ( 'div' , container ) ;
57+ const contributorList = Util . createAndAppend ( 'div' , rightDiv ) ;
58+ repo . render ( leftDiv ) ;
6059 contributors
6160 . map ( contributor => new Contributor ( contributor ) )
6261 . forEach ( contributor => contributor . render ( contributorList ) ) ;
@@ -70,7 +69,7 @@ class App {
7069 * @param {Error } error An Error object describing the error.
7170 */
7271 renderError ( error ) {
73- console . error ( error ) ; // TODO: replace with your own code
72+ console . error ( error ) ;
7473 }
7574}
7675
0 commit comments