1
+ const form = document . querySelector ( "form" ) ;
2
+ const input = document . querySelector ( "input" ) ;
3
+ const reposContainer = document . querySelector ( ".repos" ) ;
4
+ const mainContainer = document . querySelector ( ".main-container" ) ;
5
+
6
+ const API = "https://api.github.com/users/" ;
7
+
8
+ async function fetchData ( username ) {
9
+ try {
10
+ const response = await fetch ( `${ API } ${ username } ` ) ;
11
+ if ( ! response . ok ) throw new Error ( response . statusText ) ;
12
+
13
+ const {
14
+ avatar_url,
15
+ bio,
16
+ blog,
17
+ company,
18
+ followers,
19
+ following,
20
+ location,
21
+ login,
22
+ twitter_username,
23
+ } = await response . json ( ) ;
24
+
25
+ const html = `
26
+
27
+ class="user-avatar"
28
+ style="background: url(${ avatar_url } ) no-repeat center/cover"
29
+ >
30
+
31
+ Follow
32
+
33
+
34
+
35
+
36
+ ${ followers } follower
37
+
38
+
39
+
40
+ ${ following } following
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+ ` ;
61
+
62
+ const section = document . createElement ( "section" ) ;
63
+ section . classList . add ( "about-user" ) ;
64
+ section . innerHTML = html ;
65
+ mainContainer . insertAdjacentElement ( "afterbegin" , section ) ;
66
+ } catch ( error ) {
67
+ console . error ( error ) ;
68
+ }
69
+ }
70
+
71
+ async function fetchRepos ( username ) {
72
+ try {
73
+ const response = await fetch ( `${ API } ${ username } /subscriptions` ) ;
74
+ if ( ! response . ok ) throw new Error ( response . statusText ) ;
75
+ const data = await response . json ( ) ;
76
+
77
+ data . forEach (
78
+ ( {
79
+ name,
80
+ description,
81
+ forks_count,
82
+ language,
83
+ watchers_count,
84
+ git_url,
85
+ } ) => {
86
+ const modifiedUrl = git_url
87
+ . replace ( / ^ g i t : / , "http:" )
88
+ . replace ( / \. g i t $ / , "" ) ;
89
+
90
+ const singleElement = document . createElement ( "div" ) ;
91
+ singleElement . classList . add ( "repo-card" ) ;
92
+ const html = `
93
+ ${ modifiedUrl } class="repo-title">
${ name }
94
+
95
+
96
+
97
+
98
+
99
+ ${ forks_count }
100
+
101
+
102
+
103
+ ` ;
104
+ singleElement . innerHTML = html ;
105
+ reposContainer . append ( singleElement ) ;
106
+ }
107
+ ) ;
108
+ } catch ( error ) {
109
+ console . error ( error ) ;
110
+ }
111
+ }
112
+
113
+ form . addEventListener ( "submit" , async ( e ) => {
114
+ e . preventDefault ( ) ;
115
+ const val = input . value ;
116
+
117
+ if ( val ) {
118
+ try {
119
+ await fetchData ( val ) ;
120
+ await fetchRepos ( val ) ;
121
+ } catch ( error ) {
122
+ console . log ( error ) ;
123
+ } finally {
124
+ input . value = "" ;
125
+ }
126
+ }
127
+
128
+ document
129
+ . querySelector ( "input" )
130
+ . addEventListener ( "click" , ( ) => location . reload ( ) ) ;
131
+ } ) ;
0 commit comments