summaryrefslogtreecommitdiffstats
path: root/webapp/components/reset_status_modal/reset_status_modal.jsx
blob: 2e9bb8382e2dc08d85c9ef6e09bfc9f7b3e00d30 (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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import ConfirmModal from 'components/confirm_modal.jsx';

import {toTitleCase} from 'utils/utils.jsx';

import React from 'react';
import PropTypes from 'prop-types';
import {FormattedMessage} from 'react-intl';
import {Preferences} from 'mattermost-redux/constants';

export default class ResetStatusModal extends React.PureComponent {
    static propTypes = {

        /*
         * The user's preference for whether their status is automatically reset
         */
        autoResetPref: PropTypes.string,
        actions: PropTypes.shape({

            /*
             * Function to get and then reset the user's status if needed
             */
            autoResetStatus: PropTypes.func.isRequired,

            /*
             * Function to set the status for a user
             */
            setStatus: PropTypes.func.isRequired,

            /*
             * Function to save user preferences
             */
            savePreferences: PropTypes.func.isRequired
        }).isRequired
    }

    constructor(props) {
        super(props);

        this.state = {
            show: false,
            currentUserStatus: {}
        };
    }

    componentDidMount() {
        this.props.actions.autoResetStatus().then(
            (status) => {
                const statusIsManual = status.manual;
                const autoResetPrefNotSet = this.props.autoResetPref === '';

                this.setState({
                    currentUserStatus: status, // Set in state until status refactor where we store 'manual' field in redux
                    show: Boolean(statusIsManual && autoResetPrefNotSet)
                });
            }
        );
    }

    onConfirm = (checked) => {
        this.setState({show: false});

        const newStatus = {...this.state.currentUserStatus};
        newStatus.status = 'online';
        this.props.actions.setStatus(newStatus);

        if (checked) {
            const pref = {category: Preferences.CATEGORY_AUTO_RESET_MANUAL_STATUS, user_id: newStatus.user_id, name: newStatus.user_id, value: 'true'};
            this.props.actions.savePreferences(pref.user_id, [pref]);
        }
    }

    onCancel = (checked) => {
        this.setState({show: false});

        if (checked) {
            const status = {...this.state.currentUserStatus};
            const pref = {category: Preferences.CATEGORY_AUTO_RESET_MANUAL_STATUS, user_id: status.user_id, name: status.user_id, value: 'false'};
            this.props.actions.savePreferences(pref.user_id, [pref]);
        }
    }

    render() {
        const userStatus = toTitleCase(this.state.currentUserStatus.status || '');
        const manualStatusTitle = (
            <FormattedMessage
                id='modal.manaul_status.title'
                defaultMessage='Your status is set to "{status}"'
                values={{
                    status: userStatus
                }}
            />
        );

        const manualStatusMessage = (
            <FormattedMessage
                id='modal.manaul_status.message'
                defaultMessage='Would you like to switch your status to "Online"?'
            />
        );

        const manualStatusButton = (
            <FormattedMessage
                id='modal.manaul_status.button'
                defaultMessage='Yes, set my status to "Online"'
            />
        );

        const manualStatusCancel = (
            <FormattedMessage
                id='modal.manaul_status.cancel'
                defaultMessage='No, keep it as "{status}"'
                values={{
                    status: userStatus
                }}
            />
        );

        const manualStatusCheckbox = (
            <FormattedMessage
                id='modal.manaul_status.ask'
                defaultMessage='Do not ask me again'
            />
        );

        return (
            <ConfirmModal
                show={this.state.show}
                title={manualStatusTitle}
                message={manualStatusMessage}
                confirmButtonText={manualStatusButton}
                onConfirm={this.onConfirm}
                cancelButtonText={manualStatusCancel}
                onCancel={this.onCancel}
                showCheckbox={true}
                checkboxText={manualStatusCheckbox}
            />
        );
    }
}