aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/components/Avatar.js
blob: 6e56cfc294143b904380062b8ec21570cb563e20 (plain) (blame)
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
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';

import { UserType } from './Types';

import Icon from './Icon';

import { fetchUserUri } from '../api';

import './Avatar.css';

class Avatar extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            user: props.user
        };
    }
    componentDidMount() {
        let user = this.state.user;
        if (!user.uid && user.uri) {
            fetchUserUri(user.uri).then(response => {
                this.setState({
                    user: response.data
                });
            });
        }
    }
    shouldComponentUpdate(nextProps, nextState) {
        return this.state.user.avatar != nextState.user.avatar || this.state.user.uid != nextState.user.uid
        || this.props.user.avatar != nextProps.user.avatar || this.props.user.uid != nextProps.user.uid;
    }
    componentDidUpdate(prevProps, prevState, shouldReload) {
        this.setState({
            user: this.props.user
        });
    }
    render() {
        let user = this.state.user;
        return (
            <div className="Avatar" style={this.props.style}>
                <div className="msg-avatar">
                    {
                        user.uname ?
                            <Link to={{ pathname: this.props.link || `/${user.uname}/` }}>
                                {user.avatar ?
                                    <img src={user.avatar} alt={`${user.uname}`} />
                                    : <Icon name="ei-spinner" size="m" />}
                            </Link>
                            : <Icon name="ei-spinner" size="m" />
                    }
                </div>
                <div style={{ display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
                    <span>
                        <Link to={{ pathname: `/${user.uname}/` }}>
                            <span>{user.uname}</span>
                        </Link>
                    </span>
                    {this.props.children}
                </div>
            </div>
        );
    }
}

export default Avatar;

Avatar.propTypes = {
    user: UserType,
    link: PropTypes.string,
    style: PropTypes.object,
    children: PropTypes.node
};