This repository was archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.jsx
172 lines (144 loc) · 4.11 KB
/
app.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* global fetch */
import L from 'leaflet'
import geocoding from 'esri-leaflet-geocoder'
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
const markers = new L.FeatureGroup()
let map = null
L.Icon.Default.imagePath = 'https://cdn.jsdelivr.net/leaflet/1.0.0-rc.1/images/'
class App extends Component {
constructor (props) {
super(props)
this.state = {
username: '',
realName: '',
avatar: '',
location: '',
repos: '',
followers: '',
url: '',
notFound: ''
}
this.fetchUser = this.fetchUser.bind(this)
}
render () {
return (<div>
<div id='app'>
<SearchBox fetchUser={this.fetchUser}/>
<Card data={this.state}/>
</div>
<div id='map'/>
</div>)
}
// the api request function
fetchApi (url) {
fetch(url)
.then((res) => res.json())
.then((data) => {
// update state with API data
this.setState({
username: data.login,
realName: data.name,
avatar: data.avatar_url,
location: data.location,
repos: data.public_repos,
followers: data.followers,
url: data.html_url,
notFound: data.message
})
geocoding.Tasks.geocode()
.text(data.location)
.run(function (err, body, response) {
if (err) console.log(err)
markers.clearLayers()
if (body.results[0]) {
let latlng = body.results[0].latlng
markers.addLayer(L.marker(latlng))
map.setView(latlng, 10)
} else {
map.setView([0, 0], 2)
}
})
})
.catch((err) => console.log(err))
}
fetchUser (username) {
let url = `https://api.github.com/users/${username}`
this.fetchApi(url)
}
componentDidMount () {
map = L.map('map').setView([0, 0], 2)
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a>',
subdomains: 'abcd'
}).addTo(map)
map.addLayer(markers)
if (!this.state.username) return
let url = `https://api.github.com/users/${this.state.username}`
this.fetchApi(url)
}
}
class SearchBox extends Component {
constructor (props) {
super(props)
this.handleClick = this.handleClick.bind(this)
}
render () {
return (<form
className='searchbox'
onSubmit={this.handleClick}>
<input
ref='search'
className='searchbox__input'
type='text'
placeholder='type username...'/>
<input
type='submit'
className='searchbox__button'
value='Search GitHub User' />
</form>)
}
handleClick (e) {
e.preventDefault()
let username = this.refs.search.value
// sending the username value to parent component to fetch new data from API
this.props.fetchUser(username)
this.refs.search.value = ''
}
}
SearchBox.propTypes = {
fetchUser: React.PropTypes.func
}
class Card extends Component {
render () {
let data = this.props.data
if (!data.username) {
return <h3 className='card__notfound'>Enter a username to start</h3>
}
if (data.notFound === 'Not Found') {
// when username is not found...
return <h3 className='card__notfound'>User not found. Try again!</h3>
}
return (<div className='card'>
<a href={data.url} target='_blank'>
<img className='card__avatar' src={data.avatar} />
</a>
<h2 className='card__username'>
<a className='card__link' href={data.url} target='_blank'>{data.username}</a></h2>
<dl>
<dt>Real name</dt>
<dd>{data.realName}</dd>
<dt>Location</dt>
<dd>{data.location}</dd>
<dt>Number of public repos</dt>
<dd>{data.repos}</dd>
<dt>Number of followers</dt>
<dd>{data.followers}</dd>
</dl>
</div>)
}
}
Card.propTypes = {
data: React.PropTypes.object
}
ReactDOM.render(<App />, document.getElementById('react-app'))