-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-cards.js
More file actions
81 lines (73 loc) · 1.86 KB
/
github-cards.js
File metadata and controls
81 lines (73 loc) · 1.86 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
// run this code at https://jscomplete.com/playground/rgs2.4
const CardList = (props) => (
<div>
{props.profiles.map(profile => <Card key={profile.id}{...profile}/>)}
</div>
);
class Card extends React.Component {
render() {
const profile = this.props;
return (
<div className="github-profile">
<img src={profile.avatar_url} />
<div className="info">
<div className="name">{profile.name}</div>
<div className="company">{profile.company}</div>
</div>
</div>
);
}
}
class Form extends React.Component {
state = {
userName: '',
}
handleSubmit = async (event) => {
event.preventDefault();
console.log(this.state.userName);
const resp = await axios.get(`https://api.github.com/users/${this.state.userName}`)
this.props.profileHandler(resp.data);
this.setState({
userName: ''
});
};
render () {
return (
<form onSubmit={this.handleSubmit}>
<input
type="text"
value={this.state.userName}
onChange={event => this.setState({ userName: event.target.value })}
placeholder="Github Account name"
required />
<button>Add Card</button>
</form>
);
}
}
class App extends React.Component {
state = {
profiles: []
};
addNewProfile = (profileData) => {
console.log(profileData);
const duplicate = this.state.profiles.find(each => each.id === profileData.id);
if (duplicate) return;
this.setState(prevState => ({
profiles: [...prevState.profiles, profileData]
}));
};
render() {
return (
<div>
<div className="header">{this.props.title}</div>
<Form profileHandler={this.addNewProfile}/>
<CardList profiles={this.state.profiles}/>
</div>
);
}
}
ReactDOM.render(
<App title="The GitHub Cards App" />,
mountNode,
);