summaryrefslogtreecommitdiffstats
path: root/webapp/components/post_view/components/commented_on_files_message_container.jsx
blob: d1fa455c7f9a183f8b28c91215c65200b809a77e (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) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import * as AsyncClient from 'utils/async_client.jsx';
import FileStore from 'stores/file_store.jsx';
import * as Utils from 'utils/utils.jsx';

export default class CommentedOnFilesMessageContainer extends React.Component {
    static propTypes = {
        parentPostChannelId: React.PropTypes.string.isRequired,
        parentPostId: React.PropTypes.string.isRequired
    }

    constructor(props) {
        super(props);

        this.handleFileChange = this.handleFileChange.bind(this);

        this.state = {
            fileInfos: FileStore.getInfosForPost(this.props.parentPostId)
        };
    }

    componentDidMount() {
        FileStore.addChangeListener(this.handleFileChange);

        if (!FileStore.hasInfosForPost(this.props.parentPostId)) {
            AsyncClient.getFileInfosForPost(this.props.parentPostChannelId, this.props.parentPostId);
        }
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.parentPostId !== this.props.parentPostId) {
            this.setState({
                fileInfos: FileStore.getInfosForPost(this.props.parentPostId)
            });

            if (!FileStore.hasInfosForPost(this.props.parentPostId)) {
                AsyncClient.getFileInfosForPost(this.props.parentPostChannelId, this.props.parentPostId);
            }
        }
    }

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

        if (nextProps.parentPostChannelId !== this.props.parentPostChannelId) {
            return true;
        }

        // fileInfos are treated as immutable by the FileStore
        if (nextState.fileInfos !== this.state.fileInfos) {
            return true;
        }

        return false;
    }

    handleFileChange() {
        this.setState({
            fileInfos: FileStore.getInfosForPost(this.props.parentPostId)
        });
    }

    componentWillUnmount() {
        FileStore.removeChangeListener(this.handleFileChange);
    }

    render() {
        let message = ' ';

        if (this.state.fileInfos && this.state.fileInfos.length > 0) {
            message = this.state.fileInfos[0].name;

            if (this.state.fileInfos.length === 2) {
                message += Utils.localizeMessage('post_body.plusOne', ' plus 1 other file');
            } else if (this.state.fileInfos.length > 2) {
                message += Utils.localizeMessage('post_body.plusMore', ' plus {count} other files').replace('{count}', (this.state.fileInfos.length - 1).toString());
            }
        }

        return <span>{message}</span>;
    }
}