-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfile.js
More file actions
69 lines (55 loc) · 1.56 KB
/
Profile.js
File metadata and controls
69 lines (55 loc) · 1.56 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
import React from 'react';
import { fetchUserData, cancelFetch } from './dataFetcher';
import { Userlist } from './Userlist';
export class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {userData: null}
}
loadUserData() {
this.setState({userData: null});
this.fetchID = fetchUserData(this.props.username, (userData) => {
this.setState({ userData });
});
}
componentDidMount() {
this.loadUserData();
}
componentWillUnmount() {
cancelFetch(this.fetchID);
}
componentDidUpdate(prevProps) {
if(this.props.username !== prevProps.username) {
cancelFetch(this.fetchID);
this.loadUserData();
}
}
render() {
const isLoading = this.state.userData === null ? true : false;
const name = isLoading ? 'Loading...' : this.state.userData.name;
const bio = isLoading ? 'Loading...' : this.state.userData.bio;
const friends = isLoading ? [] : this.state.userData.friends;
let className = 'Profile';
if (isLoading) {
className += ' loading';
}
return (
<div className={className}>
<div className="profile-picture">
{
isLoading === false
&&
<img src={this.state.userData.profilePictureUrl} alt="" />
}
</div>
<div className="profile-body">
<h2>{name}</h2>
<h3>@{this.props.username}</h3>
<p>{bio}</p>
<h3>My friends</h3>
<Userlist usernames={friends} onChoose={this.props.onChoose} />
</div>
</div>
);
}
}