-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (77 loc) · 2.05 KB
/
app.js
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
'use strict';
import React from 'react';
import axios from 'axios';
import AppContent from 'components/app-content';
class App extends React.Component {
constructor () {
super();
this.state = {
userinfo: null,
repos: [],
starred: [],
isFetching: false
};
this.handleSearch = this.handleSearch.bind(this);
}
getGitHubApiUrl (username, type) {
const internalUser = username ? `/${username}` : '';
const internalType = type ? `/${type}` : '';
return `https://api.github.com/users${internalUser}${internalType}`;
}
handleSearch (e) {
const keyCode = e.witch || e.keyCode;
const ENTER = 13;
if (keyCode === ENTER) {
const value = e.target.value;
this.setState({
isFetching: true
});
return axios.get(this.getGitHubApiUrl(value))
.then(result => {
result = result.data;
this.setState({
userinfo: {
username: result.name,
photo: result.avatar_url,
login: result.login,
repos: result.public_repos,
followers: result.followers,
following: result.following
},
repos: [],
starred: []
});
this.setState({
isFetching: false
});
});
}
}
/* Utilizando High Order Functions */
getRepos (type) {
return (e) => {
const username = this.state.userinfo.login;
axios.get(this.getGitHubApiUrl(username, type))
.then((result) => {
var data = result.data;
this.setState({
[type]: data.map((repo) => ({
name: repo.name,
link: repo.html_url
}))
});
});
};
}
render () {
return (
<AppContent
{...this.state}
handleSearch={this.handleSearch}
getRepos={this.getRepos('repos')}
getStarred={this.getRepos('starred')}
/>
);
}
}
export default App;