forked from shashankKeshava/Mindtree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
118 lines (112 loc) · 3.06 KB
/
Copy pathApp.js
File metadata and controls
118 lines (112 loc) · 3.06 KB
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
import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";
import request from "superagent";
// Add Credentials of Client ID and Client Secret
const clientID = "XXXXXXX";
const clientSecret = "XXXXX";
const url = `https://api.github.com/users?client_id=${clientID}&client_secret=${clientSecret}`;
class App extends Component {
constructor(props) {
super(props); // Super allows to use constructor methods i.e this.props of parent class
this.state = {
userList: null,
userDetails: null,
apiResponse: null
};
}
handleCloseBio = () => {
this.setState({
userDetails: null
});
};
handleClick = user => {
let userBio = null;
request.get(user.url).end((err, res) => {
if (err) console.log("Fetch User Details Failed", err);
else {
userBio = (
<div className={"basicDetails"}>
<div className={"basicDetails-header"}>
<h3>
{res.body.login} User Bio
</h3>
<a className={"basicDetails-close"} onClick={this.handleCloseBio}>
X
</a>
</div>
<ol>
<li className={"align-left"}>
Id:{res.body.id}
</li>
<li className={"align-left"}>
Name:{res.body.name}
</li>
<li className={"align-left"}>
Company:{res.body.company}
</li>
<li className={"align-left"}>
Location:{res.body.location}
</li>
<li className={"align-left"}>
Public Repos:{res.body.public_repos}
</li>
<li className={"align-left"}>
Followers:{res.body.followers}
</li>
</ol>
</div>
);
this.setState({
userDetails: userBio
});
}
});
};
_userListing = () => {
let users = [];
if (!!this.state.apiResponse) {
users = this.state.apiResponse.map(user => {
// Bind helps to bind this and multiple parameters to the event handler, arrow frunctions (=>) can also be used to bind
return (
<div
key={user.id}
className={"userTiles"}
onClick={this.handleClick.bind(this, user)}
>
<img className={"avatar"} src={user.avatar_url} />
<b className={"login"}>
{user.login}
</b>
</div>
);
});
}
return (
<div>
{users}
</div>
);
};
componentDidMount = () => {
request.get(url).end((err, res) => {
if (err) console.log(err);
else
this.setState({
apiResponse: res.body
});
});
};
render() {
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">Mindtree Interview Coding Assignment</h1>
</header>
{this._userListing()}
{this.state.userDetails}
</div>
);
}
}
export default App;