1+ 'use strict' ;
2+
3+ const HYF_REPOS_URL =
4+ "https://api.github.com/orgs/HackYourFuture/repos?per_page=10" ;
5+
6+ const root = document . querySelector ( "#root" ) ;
7+ const select = document . querySelector ( "#repo" ) ;
8+ const repoContaine = document . querySelector ( '.repo-containe' ) ;
9+ const repoDiv = document . querySelector ( '#repoDiv' ) ;
10+ const op = document . querySelector ( 'option' ) ;
11+ const contri = document . querySelector ( '#contri' ) ;
12+
13+
14+ function main ( url ) {
15+
16+ // to creat ul and li element
17+ function creatLi ( txt ) {
18+ const liRepo = document . createElement ( "li" ) ;
19+ liRepo . innerHTML = txt ;
20+ const ulRepo = document . createElement ( "ul" ) ;
21+ ulRepo . appendChild ( liRepo ) ;
22+ contri . appendChild ( ulRepo ) ;
23+ }
24+
25+ // to creat option element and append it to to select
26+ function creatOption ( txt , num ) {
27+ let opt = document . createElement ( 'option' ) ;
28+
29+ opt . textContent = txt ;
30+ opt . setAttribute ( 'value' , num ) ;
31+ select . appendChild ( opt ) ;
32+ }
33+
34+ // load document and add it to ul
35+ function loadDoc ( url ) {
36+ var xhttp = new XMLHttpRequest ( ) ;
37+ xhttp . onreadystatechange = function ( ) {
38+ if ( this . readyState == 4 && this . status == 200 ) {
39+ const data = JSON . parse ( xhttp . response ) ;
40+ data . forEach ( ele => {
41+ const txt = `<img src="${ ele . avatar_url } "> ${ ele . login } ${ ele . contributions } ` ;
42+ creatLi ( txt ) ;
43+ } ) ;
44+
45+ }
46+ } ;
47+ xhttp . open ( "GET" , url , true ) ;
48+ xhttp . send ( ) ;
49+ }
50+
51+ function fetchJSON ( url ) {
52+ const hyfRepo = new XMLHttpRequest ( ) ;
53+
54+ function reqListener ( ) {
55+ const facts = JSON . parse ( this . response ) ;
56+ let arr = [ ] ;
57+ for ( let i in facts ) {
58+ arr . push ( [ { "name" : facts [ i ] . name } , { "description" : facts [ i ] . description } , { "forks_count" : facts [ i ] . forks_count } , { "updated_at" : facts [ i ] . updated_at } ] ) ;
59+ }
60+
61+ arr . forEach ( ( ele , i ) => {
62+ creatOption ( ele [ 0 ] . name , i ) ;
63+ } ) ;
64+
65+ console . log ( facts ) ;
66+
67+ select . addEventListener ( 'change' , ( ) => {
68+ contri . innerHTML = "" ;
69+ let s = select . value ;
70+
71+ let txt = "<strong>Repository : </strong>" +
72+ facts [ s ] . name +
73+ "<br>" +
74+ "<strong>Description : </strong>" +
75+ facts [ s ] . description +
76+ "<br>" +
77+ "<strong>Forks : </strong>" +
78+ facts [ s ] . forks_count +
79+ "<br>" +
80+ "<strong>Updated : </strong>" +
81+ facts [ s ] . updated_at ;
82+
83+ repoDiv . innerHTML = txt + s ;
84+ let link = facts [ s ] . contributors_url ;
85+
86+ loadDoc ( link ) ;
87+ } ) ;
88+
89+
90+ }
91+
92+ hyfRepo . open ( "GET" , url , true ) ;
93+ hyfRepo . send ( ) ;
94+ hyfRepo . addEventListener ( "load" , reqListener ) ;
95+ }
96+
97+
98+ return fetchJSON ( url ) ;
99+ }
100+
101+ main ( HYF_REPOS_URL ) ;
0 commit comments