1+ /* eslint-disable no-console */
2+ /* eslint-disable no-use-before-define */
3+ /* eslint-disable no-unused-vars */
4+ const createAndAppend = ( name , parent , options = { } ) => {
5+ const elem = document . createElement ( name ) ;
6+ parent . appendChild ( elem ) ;
7+ Object . entries ( options ) . forEach ( ( [ key , value ] ) => {
8+ if ( key === 'innerHTML' ) {
9+ elem . innerHTML = value ;
10+ } else if ( key === 'text' ) {
11+ elem . textContent = value ;
12+ } else {
13+ elem . setAttribute ( key , value ) ;
14+ }
15+ } ) ;
16+ return elem ;
17+ } ;
18+ const errorHandler = err => {
19+ const root = document . getElementById ( 'root' ) ;
20+ createAndAppend ( 'div' , root , {
21+ text : err . message ,
22+ class : 'alert-error' ,
23+ } ) ;
24+ } ;
25+
26+ const optionLoading = items => {
27+ const selectEl = document . querySelector ( '#reposName' ) ;
28+ items
29+ . sort ( ( current , next ) => current . name . localeCompare ( next . name ) )
30+ . forEach ( repo => {
31+ createAndAppend ( 'option' , selectEl , {
32+ text : repo . name ,
33+ value : repo . name ,
34+ } ) ;
35+ } ) ;
36+ } ;
37+
38+ const renderRepoContainer = repo => {
39+ const { selectedIndex } = document . querySelector ( 'select' ) ;
40+ const convertedDate = new Date (
41+ repo [ selectedIndex ] . updated_at ,
42+ ) . toLocaleString ( ) ;
43+ const repoContainer = document . querySelector ( '.repo-container' ) ;
44+
45+ createAndAppend ( 'ul' , repoContainer , { class : 'repoDetails' } ) ;
46+ const cardUl = document . querySelector ( '.repoDetails' ) ;
47+
48+ createAndAppend ( 'li' , cardUl , {
49+ innerHTML : `<span class='firstText'>Repository</span> <span>:</span><a href="${ repo [ selectedIndex ] . html_url } " target='_blank'>${ repo [ selectedIndex ] . name } </a>` ,
50+ } ) ;
51+ createAndAppend ( 'li' , cardUl , {
52+ innerHTML : `<span class='firstText'>Description</span><span>:</span> <span >${ repo [ selectedIndex ] . description } </span>` ,
53+ } ) ;
54+ createAndAppend ( 'li' , cardUl , {
55+ innerHTML : `<span class='firstText'>Forks</span><span>:</span> <span >${ repo [ selectedIndex ] . forks_count } </span>` ,
56+ } ) ;
57+ createAndAppend ( 'li' , cardUl , {
58+ innerHTML : `<span class='firstText'>Updated</span><span>:</span> <span >${ convertedDate } </span>` ,
59+ } ) ;
60+
61+ return repo ;
62+ } ;
63+
64+ const renderRepoContributors = repo => {
65+ const { selectedIndex } = document . querySelector ( 'select' ) ;
66+ const selectedRepo = repo [ selectedIndex ] ;
67+
68+ const repoContributors = document . querySelector ( '.contributors-container' ) ;
69+ createAndAppend ( 'ul' , repoContributors , { class : 'repoContributors' } ) ;
70+ const ulContributors = document . querySelector ( '.repoContributors' ) ;
71+
72+ createAndAppend ( 'h5' , ulContributors , {
73+ text : 'Contributions' ,
74+ style : 'margin:0 0 5px 0; font-size: 15px; ' ,
75+ } ) ;
76+
77+ fetchJSON ( selectedRepo . contributors_url )
78+ . then ( data =>
79+ data . forEach ( dataSelected => {
80+ const li = createAndAppend ( 'li' , ulContributors , {
81+ style :
82+ 'display: flex;text-align: center; justify-content: space-between; border-bottom: 2px solid #687466;' ,
83+ } ) ;
84+ const imgAvatar = createAndAppend ( 'img' , li , {
85+ src : dataSelected . avatar_url ,
86+ class : 'avatar' ,
87+ } ) ;
88+ const contInfo = createAndAppend ( 'a' , li , {
89+ text : dataSelected . login ,
90+ href : dataSelected . html_url ,
91+ target : '_blank' ,
92+ style : 'margin: 20px auto 0 5px; text-decoration: none;' ,
93+ } ) ;
94+ const contCount = createAndAppend ( 'span' , li , {
95+ text : dataSelected . contributions ,
96+ class : 'contributions-count' ,
97+ } ) ;
98+ } ) ,
99+ )
100+ . catch ( err => console . error ( err ) ) ;
101+ } ;
102+
103+ const renderSelectedOptionChange = repo => {
104+ const selectRepo = document . querySelector ( 'select' ) ;
105+ selectRepo . addEventListener ( 'change' , ( ) => {
106+ document . querySelector ( 'section.repo-container ul' ) . remove ( ) ;
107+ document . querySelector ( '.repoContributors' ) . remove ( ) ;
108+
109+ renderRepoContainer ( repo ) ;
110+ renderRepoContributors ( repo ) ;
111+ } ) ;
112+ } ;
113+
114+ const fetchJSON = url => {
115+ const repos = fetch ( url )
116+ . then ( res => {
117+ if ( ! res . ok ) {
118+ throw new Error ( 'Network response was not ok' ) ;
119+ }
120+ return res . json ( ) ;
121+ } )
122+ . then ( data => data )
123+ . catch ( err => errorHandler ( err ) ) ;
124+ return repos ;
125+ } ;
126+
127+ const main = url => {
128+ fetchJSON ( url )
129+ . then ( data => {
130+ optionLoading ( data ) ;
131+ renderRepoContainer ( data ) ;
132+ renderRepoContributors ( data ) ;
133+ renderSelectedOptionChange ( data ) ;
134+ } )
135+ . catch ( err => errorHandler ( err ) ) ;
136+ } ;
137+
138+ const HYF_REPOS_URL =
139+ 'https://api.github.com/orgs/HackYourFuture/repos?per_page=30' ;
140+
141+ main ( HYF_REPOS_URL ) ;
0 commit comments