@@ -16,14 +16,78 @@ class App {
1616 // 1. Create the fixed HTML elements of your page
1717 // 2. Make an initial XMLHttpRequest using Util.fetchJSON() to populate your <select> element
1818
19+ const BodyEl = document . body ;
20+
1921 const root = document . getElementById ( 'root' ) ;
2022
21- Util . createAndAppend ( 'h1' , root , { text : 'It works!' } ) ; // TODO: replace with your own code
23+ // Create div element 'select' in document body to hold the label element and list box.
24+
25+ Util . createAndAppend ( 'div' , BodyEl , { id : 'select-container' } ) ;
26+ const select = document . getElementById ( 'select-container' ) ;
27+ Util . createAndAppend ( 'LABEL' , select , {
28+ text : 'HYF Repositories: ' ,
29+ id : 'label' ,
30+ for : 'repo' ,
31+ } ) ;
32+ Util . createAndAppend ( 'select' , select , { id : 'select-repo' } ) ;
33+
34+ // Create two div elements section1 and section2 under 'Root' div to have
35+ // section1 - Repository Information.
36+ // section2 - Contributions.
37+
38+ Util . createAndAppend ( 'div' , root , { id : 'repo-details' } ) ;
39+ Util . createAndAppend ( 'div' , root , { id : 'contributor-information' } ) ;
40+
41+ // Insert section1 before section2 div element under 'root' div.
42+ const newNode = document . getElementById ( 'contributor-information' ) ;
43+ const referenceNode = document . querySelector ( 'repo-details' ) ;
44+ root . insertBefore ( newNode , referenceNode ) ;
45+
46+ // Insert Select div first in the body before root div.
47+ BodyEl . insertBefore ( select , document . getElementById ( 'root' ) ) ;
2248
2349 try {
2450 const repos = await Util . fetchJSON ( url ) ;
51+
2552 this . repos = repos . map ( repo => new Repository ( repo ) ) ;
26- // TODO: add your own code here
53+
54+ const sortRepoName = [ ] ;
55+ // push all the HYP repo names to sort array.
56+
57+ Object . keys ( repos ) . forEach ( key => {
58+ sortRepoName . push ( repos [ key ] . name ) ;
59+ } ) ;
60+ // sort the repo name using sort function and localeComapare for uppercase and lowercase sorting.
61+ sortRepoName . sort ( ( a , b ) => a . localeCompare ( b ) ) ;
62+
63+ const selectBox = document . getElementById ( 'select-repo' ) ;
64+
65+ // Create Option under Select element and attach the same with SELECT element.
66+
67+ sortRepoName . forEach ( loadrepo => {
68+ const option = document . createElement ( 'option' ) ;
69+ option . value = loadrepo ;
70+ option . text = loadrepo ;
71+ selectBox . appendChild ( option ) ;
72+ } ) ;
73+
74+ let keyIndex ;
75+ console . log ( selectBox . value ) ;
76+ Object . keys ( repos ) . forEach ( key => {
77+ if ( selectBox . value === repos [ key ] . name ) {
78+ keyIndex = key ;
79+ }
80+ } ) ;
81+ this . fetchContributorsAndRender ( keyIndex ) ;
82+
83+ selectBox . addEventListener ( 'change' , ( ) => {
84+ Object . keys ( repos ) . forEach ( key => {
85+ if ( selectBox . value === repos [ key ] . name ) {
86+ keyIndex = key ;
87+ }
88+ } ) ;
89+ this . fetchContributorsAndRender ( keyIndex ) ;
90+ } ) ;
2791 } catch ( error ) {
2892 this . renderError ( error ) ;
2993 }
@@ -46,14 +110,18 @@ class App {
46110 */
47111 async fetchContributorsAndRender ( index ) {
48112 try {
113+ console . log ( index ) ;
49114 const repo = this . repos [ index ] ;
50115 const contributors = await repo . fetchContributors ( ) ;
51116
52- const container = document . getElementById ( 'container ' ) ;
117+ const container = document . getElementById ( 'repo-details ' ) ;
53118 App . clearContainer ( container ) ;
54119
55- const leftDiv = Util . createAndAppend ( 'div' , container ) ;
56- const rightDiv = Util . createAndAppend ( 'div' , container ) ;
120+ const contributorContainer = document . getElementById ( 'contributor-information' ) ;
121+ App . clearContainer ( contributorContainer ) ;
122+
123+ const leftDiv = 'repo-details' ;
124+ const rightDiv = document . getElementById ( 'contributor-information' ) ;
57125
58126 const contributorList = Util . createAndAppend ( 'ul' , rightDiv ) ;
59127
@@ -69,13 +137,12 @@ class App {
69137
70138 /**
71139 * Render an error to the DOM.
72- * @param {Error } error An Error object describing the error.
140+ * @param {Error } error An Error object describing the1 2 error.
73141 */
74142 renderError ( error ) {
75143 console . log ( error ) ; // TODO: replace with your own code
76144 }
77145}
78-
79146const HYF_REPOS_URL = 'https://api.github.com/orgs/HackYourFuture/repos?per_page=100' ;
80147
81148window . onload = ( ) => new App ( HYF_REPOS_URL ) ;
0 commit comments