summaryrefslogtreecommitdiffstats
path: root/webapp/components/post_view/components/reaction_container.jsx
blob: 09e6ce5ea0fbb4200f2cbf1a40a8f30f65ea795f (plain)
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
82
83
84
85
86
87
88
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import {addReaction, removeReaction} from 'actions/post_actions.jsx';
import * as UserActions from 'actions/user_actions.jsx';

import UserStore from 'stores/user_store.jsx';

import Reaction from './reaction.jsx';

export default class ReactionContainer extends React.Component {
    static propTypes = {
        post: React.PropTypes.object.isRequired,
        emojiName: React.PropTypes.string.isRequired,
        reactions: React.PropTypes.arrayOf(React.PropTypes.object),
        emojis: React.PropTypes.object.isRequired
    }

    constructor(props) {
        super(props);

        this.handleUsersChanged = this.handleUsersChanged.bind(this);

        this.getStateFromStore = this.getStateFromStore.bind(this);

        this.getProfilesForReactions = this.getProfilesForReactions.bind(this);
        this.getMissingProfiles = this.getMissingProfiles.bind(this);

        this.state = this.getStateFromStore(props);
    }

    componentDidMount() {
        UserStore.addChangeListener(this.handleUsersChanged);
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.reactions !== this.props.reactions) {
            this.setState(this.getStateFromStore(nextProps));
        }
    }

    componentWillUnmount() {
        UserStore.removeChangeListener(this.handleUsersChanged);
    }

    handleUsersChanged() {
        this.setState(this.getStateFromStore());
    }

    getStateFromStore(props = this.props) {
        const profiles = this.getProfilesForReactions(props.reactions);
        const otherUsers = props.reactions.length - profiles.length;

        return {
            profiles,
            otherUsers,
            currentUserId: UserStore.getCurrentId()
        };
    }

    getProfilesForReactions(reactions) {
        return reactions.map((reaction) => {
            return UserStore.getProfile(reaction.user_id);
        }).filter((profile) => Boolean(profile));
    }

    getMissingProfiles() {
        const ids = this.props.reactions.map((reaction) => reaction.user_id);

        UserActions.getMissingProfiles(ids);
    }

    render() {
        return (
            <Reaction
                {...this.props}
                {...this.state}
                actions={{
                    addReaction,
                    getMissingProfiles: this.getMissingProfiles,
                    removeReaction
                }}
            />
        );
    }
}