Skip to content

Commit 4a94107

Browse files
committed
feat: added enter to search
1 parent 8016d58 commit 4a94107

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

src/Components/SearchBar.js

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,26 @@ import React, { Component } from 'react';
22
import axios from 'axios';
33

44
import { Button, Input, Fa } from 'mdbreact';
5-
import { Card, CardBody, CardTitle, CardText } from 'mdbreact';
5+
import { Card, CardBody, CardTitle, CardText, ToastContainer, toast } from 'mdbreact';
66

77
export default class SearchBar extends Component {
88
constructor(props){
99
super(props)
1010

1111
this.state = {
1212
userName: '',
13-
reposData: []
13+
reposData: [],
14+
userData: [],
15+
searchButton: 'Search',
1416
}
17+
1518
}
16-
1719
handleChange = (e) =>{
1820
this.setState({
1921
userName: e.target.value
2022
})
2123
}
2224

23-
handleSubmit = (e) => {
24-
console.log("Submitted");
25-
axios.get('https://api.github.com/users/'+this.state.userName+'/repos')
26-
.then((response) =>{
27-
console.log(response.data);
28-
29-
this.setState({
30-
reposData: response.data
31-
})
32-
})
33-
}
3425

3526
onKeyDown = (e) => {
3627
if (e.keyCode === 13 && e.shiftKey === false) {
@@ -39,16 +30,47 @@ export default class SearchBar extends Component {
3930
}
4031
}
4132

33+
handleSubmit = (e) => {
34+
console.log("Submitted");
35+
if(this.state.userName === ''){
36+
toast.warn('Please enter a username');
37+
return;
38+
}
39+
this.setState({
40+
searchButton: 'Searching...'
41+
});
42+
let getData = [];
43+
getData.push(axios.get('https://api.github.com/users/'+this.state.userName+'/repos'));
44+
getData.push(axios.get('https://api.github.com/users/'+this.state.userName));
45+
46+
Promise.all(getData)
47+
.then(result=>{
48+
this.setState({
49+
reposData: result[0].data,
50+
userData: result[1].data,
51+
searchButton: 'Search'
52+
});
53+
console.log(this.state)
54+
});
55+
}
56+
4257
render(){
4358
return(
4459
<div>
45-
<Input label="Type any Github Username" onKeyDown={this.onKeyDown} onChange={this.handleChange} group type="text" />
60+
<Input label="Type any Github Username" onKeyDown={this.onKeyDown} onChange={this.handleChange} group type="text" />
4661
{/* <Input label="Type your password" icon="lock" group type="password" /> */}
47-
<Button color="danger" onClick={this.handleSubmit} >Search <Fa icon="search" /></Button>
62+
<Avatar
63+
data={this.state.userData}
64+
/>
65+
<Button color="danger" onClick={this.handleSubmit} >{this.state.searchButton} <Fa icon="search" /></Button>
4866
<PanelBoard
4967
data={this.state.reposData}
5068
/>
51-
69+
<ToastContainer
70+
hideProgressBar={true}
71+
newestOnTop={true}
72+
autoClose={2000}
73+
/>
5274
</div>
5375

5476
);
@@ -71,6 +93,7 @@ class PanelBoard extends Component {
7193
branch={repo.default_branch}
7294
dLink={repo.html_url}
7395
/>
96+
// console.log(repo.name)
7497
));
7598

7699
return(
@@ -115,4 +138,18 @@ class ResultPanel extends Component {
115138
);
116139
}
117140
}
141+
142+
class Avatar extends Component {
143+
render() {
144+
const avatar= this.props.data ? this.props.data : null;
145+
146+
return (
147+
avatar &&
148+
<div>
149+
<img className="gitAvatar" src={avatar.avatar_url} />
150+
<div><strong>{avatar.name}</strong></div>
151+
</div>
152+
);
153+
}
154+
}
118155
// export default Timer;

0 commit comments

Comments
 (0)