summaryrefslogtreecommitdiffstats
path: root/webapp/components/dot_menu/dot_menu_flag.jsx
blob: 11546ee79544d3fab0c268fef45c51ae9670737d (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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';
import {FormattedMessage} from 'react-intl';
import PropTypes from 'prop-types';

import * as Utils from 'utils/utils.jsx';
import Constants from 'utils/constants.jsx';

function formatMessage(isFlagged) {
    return (
        <FormattedMessage
            id={isFlagged ? 'rhs_root.mobile.unflag' : 'rhs_root.mobile.flag'}
            defaultMessage={isFlagged ? 'Unflag' : 'Flag'}
        />
    );
}

export default function DotMenuFlag(props) {
    function onFlagPost(e) {
        e.preventDefault();
        props.actions.flagPost(props.postId);
    }

    function onUnflagPost(e) {
        e.preventDefault();
        props.actions.unflagPost(props.postId);
    }

    const flagFunc = props.isFlagged ? onUnflagPost : onFlagPost;

    let flagId = null;
    if (props.idCount > -1) {
        flagId = Utils.createSafeId(props.idPrefix + props.idCount);
    }

    if (props.idPrefix.indexOf(Constants.RHS_ROOT) === 0) {
        flagId = props.idPrefix;
    }

    return (
        <li
            key={props.idPrefix}
            role='presentation'
        >
            <a
                id={flagId}
                href='#'
                onClick={flagFunc}
            >
                {formatMessage(props.isFlagged)}
            </a>
        </li>
    );
}

DotMenuFlag.propTypes = {
    idCount: PropTypes.number,
    idPrefix: PropTypes.string.isRequired,
    postId: PropTypes.string.isRequired,
    isFlagged: PropTypes.bool.isRequired,

    actions: PropTypes.shape({

        /*
         * Function flag the post
         */
        flagPost: PropTypes.func.isRequired,

        /*
         * Function to unflag the post
         */
        unflagPost: PropTypes.func.isRequired

    }).isRequired
};

DotMenuFlag.defaultProps = {
    idCount: -1,
    postId: '',
    isFlagged: false
};