1+ 'use strict' ;
2+
3+ /* global Util */
4+
5+ // eslint-disable-next-line no-unused-vars
6+ class Repository {
7+ constructor ( data ) {
8+ this . data = data ;
9+ } //end constructor
10+
11+ /**
12+ * Render the repository info to the DOM.
13+ * @param {HTMLElement } parent The parent element in which to render the repository.
14+ */
15+ renderContainer ( data ) {
16+
17+ }
18+
19+ /**
20+ * Returns an array of contributors as a promise
21+ */
22+ fetchContributors ( ) {
23+ return Util . fetchJSON ( this . data . contributors_url ) ;
24+ }
25+
26+ /**
27+ * Returns the name of the repository
28+ */
29+ name ( ) {
30+ return this . data . name ;
31+ } //end name
32+ } //end Repository
33+ function renderContainer ( data ) {
34+ async function get0API ( url ) {
35+ try {
36+ let data = await Util . fetchJSON ( index0API )
37+ await renderIndex0 ( data )
38+ } catch ( error ) {
39+ this . renderError ( error ) ;
40+ }
41+ } //end asyn function
42+
43+ get0API ( index0API )
44+ let newArray = [ ] ;
45+ let forkArray = [ ] ;
46+ let languageArray = [ ] ;
47+ let descriptionArray = [ ] ;
48+ let updatedAt = [ ] ;
49+ let htmlArray = [ ] ;
50+ const root = document . getElementById ( 'root' ) ;
51+ data . sort ( ( a , b ) => ( a . name ) . localeCompare ( b . name ) ) ;
52+
53+ for ( let i = 0 ; i < data . length ; i ++ ) {
54+ newArray . push ( data [ i ] . name ) ;
55+ descriptionArray . push ( data [ i ] . description ) ;
56+ forkArray . push ( data [ i ] . forks ) ;
57+ languageArray . push ( data [ i ] . language ) ;
58+ updatedAt . push ( data [ i ] . updated_at ) ;
59+ htmlArray . push ( data [ i ] . html_url ) ;
60+ var date = new Date ( ( data [ i ] . updated_at ) ) ;
61+ date = date . toUTCString ( ) ;
62+ }
63+
64+ while ( root . firstChild ) {
65+ root . removeChild ( root . firstChild ) ;
66+ }
67+
68+ Util . createAndAppend ( 'h1' , root , { text : "Hack Your Future Repositories" , class : 'title' } ) ;
69+ Util . createAndAppend ( 'h3' , root , { text : "Select a repository: " , class : 'subtitle' } ) ;
70+ const selectList = Util . createAndAppend ( 'select' , root , { id : "mySelect" } ) ;
71+ const headerDiv = Util . createAndAppend ( 'div' , root , { class : 'headerdiv' } ) ;
72+ Util . createAndAppend ( 'h3' , headerDiv , { text : "Repository Information" , class : 'subtitle' , id : 'repoHeader' } ) ;
73+ Util . createAndAppend ( 'h3' , headerDiv , { text : "Contributors" , class : 'subtitle' , id :'contributorHeader' } ) ;
74+ const container = Util . createAndAppend ( 'div' , root , { class : 'container' } ) ;
75+ const card = Util . createAndAppend ( 'div' , container , { class : 'card' } ) ;
76+ const ul = Util . createAndAppend ( 'ul' , card , { id : "myUl" , } ) ;
77+ const contributorsCard = Util . createAndAppend ( 'div' , container , { class : 'card' } ) ;
78+ const contributorsUl = Util . createAndAppend ( 'ul' , contributorsCard , { id : 'contributorsUl' } ) ;
79+ const Index0Name = Util . createAndAppend ( 'li' , ul , { text : "Repository: " , class : 'nameInContainer' } ) ;
80+ const Index0Link = Util . createAndAppend ( 'a' , Index0Name , { text : newArray [ 0 ] , target : "_blank" , href : htmlArray [ 0 ] } ) ;
81+ const Index0Description = Util . createAndAppend ( 'li' , ul , { text : "Description: " + descriptionArray [ 0 ] , class :"descriptionInContainer" } ) ;
82+ const Index0Fork = Util . createAndAppend ( 'li' , ul , { text : "Number of Forks: " + forkArray [ 0 ] , class : 'forksInContainer' } ) ;
83+ const Index0Language = Util . createAndAppend ( 'li' , ul , { text : "Language: " + languageArray [ 0 ] , class : 'updatedAtInContainer' } ) ;
84+ const Index0UpdatedAt = Util . createAndAppend ( 'li' , ul , { text : "Updated at: " + date , class : 'updatedAtInContainer' } )
85+
86+ function renderIndex0 ( data ) {
87+ for ( let i = 0 ; i < data . length ; i ++ ) {
88+ let Image0Link = Util . createAndAppend ( 'li' , contributorsUl , { } )
89+ let contributor0Name = Util . createAndAppend ( 'img' , Image0Link , { src : data [ i ] . avatar_url , class : 'imageSrc' } ) ;
90+ let contributor0Link = Util . createAndAppend ( 'a' , Image0Link , { text : data [ i ] . login , target : "_blank" , href : data [ i ] . html_url , id : 'link' } ) ;
91+ let contributor0Badge = Util . createAndAppend ( 'li' , Image0Link , { text :"Contributions: " + data [ i ] . contributions , class : 'badge' } ) ;
92+ } //end for
93+
94+ data . forEach ( ( repo ) => {
95+ for ( let i = 0 ; i < newArray . length ; i ++ ) {
96+ Util . createAndAppend ( 'option' , selectList , { id : "myOption" , value : i , text : newArray [ i ] } ) ;
97+ }
98+ } ) ;
99+
100+ function removeNodes ( container ) {
101+ while ( ul . hasChildNodes ( ) ) {
102+ ul . removeChild ( ul . firstChild ) ;
103+ }
104+ while ( contributorsUl . hasChildNodes ( ) ) {
105+ contributorsUl . removeChild ( contributorsUl . firstChild ) ;
106+ }
107+ } //end removeNodes
108+
109+ selectList . onchange = function ( selectedIndex ) {
110+ let contributorAPI = 'https://api.github.com/repos/HackYourFuture/' + newArray [ this . selectedIndex ] + '/contributors'
111+ async function getAPI ( url ) {
112+ try {
113+ let data = await Util . fetchJSON ( contributorAPI )
114+ await renderContributors ( data )
115+ } catch ( error ) {
116+ this . renderError ( error ) ;
117+ }
118+ }
119+
120+ getAPI ( contributorAPI ) ;
121+ let repoName = Util . createAndAppend ( 'li' , ul , { text : "Repository: " , class : 'nameInContainer' , function : removeNodes ( ) } ) ;
122+ Util . createAndAppend ( 'a' , repoName , { text : newArray [ this . selectedIndex ] , id : 'linkInContainer' , target : "_blank" , href : htmlArray [ this . selectedIndex ] } ) ;
123+ Util . createAndAppend ( 'li' , ul , { text : "Description: " + descriptionArray [ this . selectedIndex ] , class : 'descriptionInContainer' } ) ;
124+ Util . createAndAppend ( 'li' , ul , { text : "Number of Forks: " + forkArray [ this . selectedIndex ] , class : 'forksInContainer' } ) ;
125+ Util . createAndAppend ( 'li' , ul , { text : "Language: " + languageArray [ this . selectedIndex ] , class : 'languageInContainer' } ) ;
126+ let dates = new Date ( updatedAt [ this . selectedIndex ] ) ;
127+ dates = dates . toUTCString ( ) ;
128+ Util . createAndAppend ( 'li' , ul , { text : "Updated at: " + dates , class : 'updatedAtInContainer' } ) ;
129+
130+
131+
132+ } // end of onchange
133+ } // end renderIndex0
134+ } //end renderContainer
135+
136+ function renderContributors ( data ) {
137+
138+ for ( let i = 0 ; i < data . length ; i ++ ) {
139+ let ImageLink = Util . createAndAppend ( 'li' , contributorsUl , { } )
140+ let contributorName = Util . createAndAppend ( 'img' , ImageLink , { src : data [ i ] . avatar_url , class : 'imageSrc' } ) ;
141+ let contributorLink = Util . createAndAppend ( 'a' , ImageLink , { text : data [ i ] . login , target : "_blank" , href : data [ i ] . html_url , id : 'link' } ) ;
142+ let contributorBadge = Util . createAndAppend ( 'li' , ImageLink , { text :"Contributions: " + data [ i ] . contributions , class : 'badge' } ) ;
143+ } //end for
144+ } //end createStuff3
0 commit comments