summaryrefslogtreecommitdiffstats
path: root/webapp/components/post_view/post_flag_icon.jsx
blob: 02f8feb53560e5db8eaef0f9f257705c1395f4ad (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
93
94
95
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';
import PropTypes from 'prop-types';
import {FormattedMessage} from 'react-intl';
import {Tooltip, OverlayTrigger} from 'react-bootstrap';

import {flagPost, unflagPost} from 'actions/post_actions.jsx';
import Constants from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';

function flagToolTip(isFlagged) {
    return (
        <Tooltip id='flagTooltip'>
            <FormattedMessage
                id={isFlagged ? 'flag_post.unflag' : 'flag_post.flag'}
                defaultMessage={isFlagged ? 'Unflag' : 'Flag for follow up'}
            />
        </Tooltip>
    );
}

function flagIcon(isFlagged) {
    let flagIconSvg = Constants.FLAG_ICON_SVG;

    if (isFlagged) {
        flagIconSvg = Constants.FLAG_FILLED_ICON_SVG;
    }

    return (
        <span
            className='icon'
            dangerouslySetInnerHTML={{__html: flagIconSvg}}
        />
    );
}

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

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

    const flagFunc = props.isFlagged ? onUnflagPost : onFlagPost;
    const flagVisible = props.isFlagged ? 'visible' : '';

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

    if (!props.isEphemeral) {
        return (
            <OverlayTrigger
                trigger={['hover', 'focus']}
                key={'flagtooltipkey' + flagVisible}
                delayShow={Constants.OVERLAY_TIME_DELAY}
                placement='top'
                overlay={flagToolTip(props.isFlagged)}
            >
                <a
                    id={flagIconId}
                    href='#'
                    className={'flag-icon__container ' + flagVisible}
                    onClick={flagFunc}
                >
                    {flagIcon(props.isFlagged)}
                </a>
            </OverlayTrigger>
        );
    }

    return null;
}

PostFlagIcon.propTypes = {
    idPrefix: PropTypes.string.isRequired,
    idCount: PropTypes.number,
    postId: PropTypes.string.isRequired,
    isFlagged: PropTypes.bool.isRequired,
    isEphemeral: PropTypes.bool
};

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