File size: 1,065 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';
import FriendIndexItem from './friend_index_item';

class FriendIndex extends React.Component {
  constructor() {
    super();
    this.state = {
      friends: null
    };
  }

  componentDidMount() {
    this.props
      .requestUserFriends(this.props.userId)
      .then(res => this.setState({ friends: Object.values(res.users) }));
  }

  componentDidUpdate(prevProps) {
    if (prevProps.match.params.userUrl !== this.props.match.params.userUrl) {
      this.props
        .requestUserFriends(this.props.userId)
        .then(res => this.setState({ friends: Object.values(res.users) }));
    }
  }

  render() {
    if (this.state.friends === null) return null;

    const friends = this.state.friends.map(friend => {
      return <FriendIndexItem user={friend} key={friend.id} />;
    });

    return (
      <div className="sidebar-friends">
        <div className="friends-header">Friends · <p>{friends.length}</p></div>
        <div className="friend-index">{friends}</div>
      </div>
    );
  }
}

export default FriendIndex;