summaryrefslogtreecommitdiffstats
path: root/webapp/components/status_dropdown/status_dropdown.jsx
blob: 4b173a0eaf5df90d54db8eb890fc30c912409637 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';
import {Dropdown} from 'react-bootstrap';
import StatusIcon from 'components/status_icon.jsx';
import PropTypes from 'prop-types';
import {FormattedMessage} from 'react-intl';
import {UserStatuses} from 'utils/constants.jsx';
import BootstrapSpan from 'components/bootstrap_span.jsx';

export default class StatusDropdown extends React.Component {

    static propTypes = {
        style: PropTypes.object,
        status: PropTypes.string,
        userId: PropTypes.string.isRequired,
        profilePicture: PropTypes.string,
        actions: PropTypes.shape({
            setStatus: PropTypes.func.isRequired
        }).isRequired
    }

    state = {
        showDropdown: false,
        mouseOver: false
    }

    onMouseEnter = () => {
        this.setState({mouseOver: true});
    }

    onMouseLeave = () => {
        this.setState({mouseOver: false});
    }

    onToggle = (showDropdown) => {
        this.setState({showDropdown});
    }

    closeDropdown = () => {
        this.setState({showDropdown: false});
    }

    setStatus = (status) => {
        this.props.actions.setStatus({
            user_id: this.props.userId,
            status
        });
        this.closeDropdown();
    }

    setOnline = (event) => {
        event.preventDefault();
        this.setStatus(UserStatuses.ONLINE);
    }

    setOffline = (event) => {
        event.preventDefault();
        this.setStatus(UserStatuses.OFFLINE);
    }

    setAway = (event) => {
        event.preventDefault();
        this.setStatus(UserStatuses.AWAY);
    }

    renderStatusOnlineAction = () => {
        return this.renderStatusAction(UserStatuses.ONLINE, this.setOnline);
    }

    renderStatusAwayAction = () => {
        return this.renderStatusAction(UserStatuses.AWAY, this.setAway);
    }

    renderStatusOfflineAction = () => {
        return this.renderStatusAction(UserStatuses.OFFLINE, this.setOffline);
    }

    renderProfilePicture = () => {
        if (!this.props.profilePicture) {
            return null;
        }
        return (
            <img
                className='user__picture'
                src={this.props.profilePicture}
            />
        );
    }

    renderStatusAction = (status, onClick) => {
        return (
            <li key={status}>
                <a
                    href={'#'}
                    onClick={onClick}
                >
                    <FormattedMessage
                        id={`status_dropdown.set_${status}`}
                        defaultMessage={status}
                    />
                </a>
            </li>
        );
    }

    renderStatusIcon = () => {
        if (this.state.mouseOver) {
            return (
                <span className={'status status-edit'}>
                    <i
                        className={'fa fa-caret-down'}
                    />
                </span>
            );
        }
        return (
            <StatusIcon
                status={this.props.status}
            />
        );
    }

    render() {
        const statusIcon = this.renderStatusIcon();
        const profilePicture = this.renderProfilePicture();
        const actions = [
            this.renderStatusOnlineAction(),
            this.renderStatusAwayAction(),
            this.renderStatusOfflineAction()
        ];
        return (
            <Dropdown
                id={'status-dropdown'}
                open={this.state.showDropdown}
                onToggle={this.onToggle}
                style={this.props.style}
            >
                <BootstrapSpan
                    bsRole={'toggle'}
                    onMouseEnter={this.onMouseEnter}
                    onMouseLeave={this.onMouseLeave}
                >
                    <div className='status-wrapper'>
                        {profilePicture}
                        <div className='status_dropdown__toggle'>
                            {statusIcon}
                        </div>
                    </div>
                </BootstrapSpan>
                <Dropdown.Menu>
                    {actions}
                </Dropdown.Menu>
            </Dropdown>
        );
    }
}