44
55class App {
66 constructor ( url ) {
7- this . mainContainer = null ;
87 this . initialize ( url ) ;
98 }
109
@@ -13,49 +12,57 @@ class App {
1312 * @param {string } url The GitHub URL for obtaining the organization's repositories.
1413 */
1514 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-
2015 const root = document . getElementById ( 'root' ) ;
21- const header = Util . createAndAppend ( 'header' , root , { class : 'header' } ) ;
22- this . mainContainer = Util . createAndAppend ( 'div' , root , { id : 'container' } ) ;
16+ Util . createAndAppend ( 'select' , root , { id : 'dropdown-select' } ) ;
17+ Util . createAndAppend ( 'div' , root , { id : 'details' } ) ;
18+ Util . createAndAppend ( 'div' , root , { id : 'contributors' } ) ;
19+ Util . createAndAppend ( 'h1' , root , { text : 'REPOSITORIES' } ) ;
2320
2421 try {
2522 const repos = await Util . fetchJSON ( url ) ;
2623 this . repos = repos . map ( repo => new Repository ( repo ) ) ;
27- // TODO: add your own code here
24+ this . repos . forEach ( repoDataObj => {
25+ repoDataObj . render ( document . getElementById ( 'dropdown-select' ) ) ;
26+ } ) ;
2827 } catch ( error ) {
2928 this . renderError ( error ) ;
3029 }
30+ document . getElementById ( 'dropdown-select' ) . addEventListener ( 'change' , event => {
31+ const selectedRepo = event . target . value ;
32+ const selectedData = this . repos . filter ( repoData => repoData . name === selectedRepo ) [ 0 ] ;
33+ this . fetchContributorsAndRender ( selectedRepo ) ;
34+ } ) ;
3135 }
3236
3337 /**
3438 * Removes all child elements from a container element
3539 * @param {* } container Container element to clear
3640 */
37- clearContainer ( ) {
38- while ( this . mainContainer . firstChild ) {
39- this . mainContainer . removeChild ( this . mainContainer . firstChild ) ;
41+ static clearContainer ( container ) {
42+ while ( container . firstChild ) {
43+ container . removeChild ( container . firstChild ) ;
4044 }
4145 }
4246
4347 /**
4448 * Fetch contributor information for the selected repository and render the
4549 * repo and its contributors as HTML elements in the DOM.
46- * @param {object } repo The selected repository object
50+ * @param {number } index The array index of the repository.
4751 */
48- async selectRepository ( repo ) {
52+ async fetchContributorsAndRender ( index ) {
4953 try {
50- this . clearContainer ( ) ;
54+ const repo = this . repos [ index ] ;
5155 const contributors = await repo . fetchContributors ( ) ;
5256
53- const repoContainer = Util . createAndAppend ( 'div' , this . mainContainer ) ;
54- const contributorContainer = Util . createAndAppend ( 'div' , this . mainContainer ) ;
57+ const container = document . getElementById ( 'container' ) ;
58+ App . clearContainer ( container ) ;
59+
60+ const leftDiv = Util . createAndAppend ( 'div' , container ) ;
61+ const rightDiv = Util . createAndAppend ( 'div' , container ) ;
5562
56- const contributorList = Util . createAndAppend ( 'ul' , contributorContainer ) ;
63+ const contributorList = Util . createAndAppend ( 'ul' , rightDiv ) ;
5764
58- repo . render ( repoContainer ) ;
65+ repo . render ( leftDiv ) ;
5966
6067 contributors
6168 . map ( contributor => new Contributor ( contributor ) )
0 commit comments