summaryrefslogtreecommitdiffstats
path: root/webapp/components/post_view/components/reaction_list_container.jsx
blob: cdc58003b01fdc5f5a3cd6d7d43dce2e9939b6d6 (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
89
90
91
92
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import * as AsyncClient from 'utils/async_client.jsx';
import EmojiStore from 'stores/emoji_store.jsx';
import ReactionStore from 'stores/reaction_store.jsx';

import ReactionListView from './reaction_list_view.jsx';

export default class ReactionListContainer extends React.Component {
    static propTypes = {
        post: React.PropTypes.object.isRequired
    }

    constructor(props) {
        super(props);

        this.handleReactionsChanged = this.handleReactionsChanged.bind(this);
        this.handleEmojisChanged = this.handleEmojisChanged.bind(this);

        this.state = {
            reactions: ReactionStore.getReactions(this.props.post.id),
            emojis: EmojiStore.getEmojis()
        };
    }

    componentDidMount() {
        ReactionStore.addChangeListener(this.props.post.id, this.handleReactionsChanged);
        EmojiStore.addChangeListener(this.handleEmojisChanged);

        if (this.props.post.has_reactions) {
            AsyncClient.listReactions(this.props.post.channel_id, this.props.post.id);
        }
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.post.id !== this.props.post.id) {
            ReactionStore.removeChangeListener(this.props.post.id, this.handleReactionsChanged);
            ReactionStore.addChangeListener(nextProps.post.id, this.handleReactionsChanged);

            this.setState({
                reactions: ReactionStore.getReactions(nextProps.post.id)
            });
        }
    }

    shouldComponentUpdate(nextProps, nextState) {
        if (nextProps.post.has_reactions !== this.props.post.has_reactions) {
            return true;
        }

        if (nextState.reactions !== this.state.reactions) {
            // this will only work so long as the entries in the ReactionStore are never mutated
            return true;
        }

        if (nextState.emojis !== this.state.emojis) {
            return true;
        }

        return false;
    }

    componentWillUnmount() {
        ReactionStore.removeChangeListener(this.props.post.id, this.handleReactionsChanged);
        EmojiStore.removeChangeListener(this.handleEmojisChanged);
    }

    handleReactionsChanged() {
        this.setState({
            reactions: ReactionStore.getReactions(this.props.post.id)
        });
    }

    handleEmojisChanged() {
        this.setState({
            emojis: EmojiStore.getEmojis()
        });
    }

    render() {
        return (
            <ReactionListView
                post={this.props.post}
                reactions={this.state.reactions}
                emojis={this.state.emojis}
            />
        );
    }
}