summaryrefslogtreecommitdiffstats
path: root/web/react/components/rename_channel_modal.jsx
blob: 80f0956f2a25e78cd9b5931d9bfb8b74cedad21f (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

const Utils = require('../utils/utils.jsx');
const Client = require('../utils/client.jsx');
const AsyncClient = require('../utils/async_client.jsx');
const ChannelStore = require('../stores/channel_store.jsx');

export default class RenameChannelModal extends React.Component {
    constructor(props) {
        super(props);

        this.handleSubmit = this.handleSubmit.bind(this);
        this.onNameChange = this.onNameChange.bind(this);
        this.onDisplayNameChange = this.onDisplayNameChange.bind(this);
        this.displayNameKeyUp = this.displayNameKeyUp.bind(this);
        this.handleClose = this.handleClose.bind(this);
        this.handleShow = this.handleShow.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);

        this.state = {
            displayName: '',
            channelName: '',
            channelId: '',
            serverError: '',
            nameError: '',
            displayNameError: '',
            invalid: false
        };
    }
    handleSubmit(e) {
        e.preventDefault();

        if (this.state.channelId.length !== 26) {
            return;
        }

        let channel = ChannelStore.get(this.state.channelId);
        const oldName = channel.name;
        const oldDisplayName = channel.displayName;
        let state = {serverError: ''};

        channel.display_name = this.state.displayName.trim();
        if (!channel.display_name) {
            state.displayNameError = 'This field is required';
            state.invalid = true;
        } else if (channel.display_name.length > 22) {
            state.displayNameError = 'This field must be less than 22 characters';
            state.invalid = true;
        } else {
            state.displayNameError = '';
        }

        channel.name = this.state.channelName.trim();
        if (!channel.name) {
            state.nameError = 'This field is required';
            state.invalid = true;
        } else if (channel.name.length > 22) {
            state.nameError = 'This field must be less than 22 characters';
            state.invalid = true;
        } else {
            let cleanedName = Utils.cleanUpUrlable(channel.name);
            if (cleanedName === channel.name) {
                state.nameError = '';
            } else {
                state.nameError = 'Must be lowercase alphanumeric characters';
                state.invalid = true;
            }
        }

        this.setState(state);

        if (state.invalid || (oldName === channel.name && oldDisplayName === channel.display_name)) {
            return;
        }

        Client.updateChannel(channel,
            function handleUpdateSuccess() {
                $(ReactDOM.findDOMNode(this.refs.modal)).modal('hide');

                AsyncClient.getChannel(channel.id);
                Utils.updateAddressBar(channel.name);

                ReactDOM.findDOMNode(this.refs.displayName).value = '';
                ReactDOM.findDOMNode(this.refs.channelName).value = '';
            }.bind(this),
            function handleUpdateError(err) {
                state.serverError = err.message;
                state.invalid = true;
                this.setState(state);
            }.bind(this)
        );
    }
    onNameChange() {
        this.setState({channelName: ReactDOM.findDOMNode(this.refs.channelName).value});
    }
    onDisplayNameChange() {
        this.setState({displayName: ReactDOM.findDOMNode(this.refs.displayName).value});
    }
    displayNameKeyUp() {
        const displayName = ReactDOM.findDOMNode(this.refs.displayName).value.trim();
        const channelName = Utils.cleanUpUrlable(displayName);
        ReactDOM.findDOMNode(this.refs.channelName).value = channelName;
        this.setState({channelName: channelName});
    }
    handleClose() {
        this.setState({
            displayName: '',
            channelName: '',
            channelId: '',
            serverError: '',
            nameError: '',
            displayNameError: '',
            invalid: false
        });
    }
    handleShow(e) {
        const button = $(e.relatedTarget);
        this.setState({displayName: button.attr('data-display'), channelName: button.attr('data-name'), channelId: button.attr('data-channelid')});
    }
    componentDidMount() {
        $(ReactDOM.findDOMNode(this.refs.modal)).on('show.bs.modal', this.handleShow);
        $(ReactDOM.findDOMNode(this.refs.modal)).on('hidden.bs.modal', this.handleClose);
    }
    componentWillUnmount() {
        $(ReactDOM.findDOMNode(this.refs.modal)).off('hidden.bs.modal', this.handleClose);
    }
    render() {
        let displayNameError = null;
        let displayNameClass = 'form-group';
        if (this.state.displayNameError) {
            displayNameError = <label className='control-label'>{this.state.displayNameError}</label>;
            displayNameClass += ' has-error';
        }

        let nameError = null;
        let nameClass = 'form-group';
        if (this.state.nameError) {
            nameError = <label className='control-label'>{this.state.nameError}</label>;
            nameClass += ' has-error';
        }

        let serverError = null;
        if (this.state.serverError) {
            serverError = <div className='form-group has-error'><label className='control-label'>{this.state.serverError}</label></div>;
        }

        return (
            <div
                className='modal fade'
                ref='modal'
                id='rename_channel'
                tabIndex='-1'
                role='dialog'
                aria-hidden='true'
            >
                <div className='modal-dialog'>
                    <div className='modal-content'>
                        <div className='modal-header'>
                            <button
                                type='button'
                                className='close'
                                data-dismiss='modal'
                            >
                                <span aria-hidden='true'>&times;</span>
                                <span className='sr-only'>Close</span>
                            </button>
                        <h4 className='modal-title'>Rename Channel</h4>
                        </div>
                        <form role='form'>
                            <div className='modal-body'>
                                <div className={displayNameClass}>
                                    <label className='control-label'>Display Name</label>
                                    <input
                                        onKeyUp={this.displayNameKeyUp}
                                        onChange={this.onDisplayNameChange}
                                        type='text'
                                        ref='displayName'
                                        className='form-control'
                                        placeholder='Enter display name'
                                        value={this.state.displayName}
                                        maxLength='64'
                                    />
                                    {displayNameError}
                                </div>
                                <div className={nameClass}>
                                    <label className='control-label'>Handle</label>
                                    <input
                                        onChange={this.onNameChange}
                                        type='text'
                                        className='form-control'
                                        ref='channelName'
                                        placeholder='lowercase alphanumeric&#39;s only'
                                        value={this.state.channelName}
                                        maxLength='64'
                                    />
                                    {nameError}
                                </div>
                                {serverError}
                            </div>
                            <div className='modal-footer'>
                                <button
                                    type='button'
                                    className='btn btn-default'
                                    data-dismiss='modal'
                                >
                                    Cancel
                                </button>
                                <button
                                    onClick={this.handleSubmit}
                                    type='submit'
                                    className='btn btn-primary'
                                >
                                    Save
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        );
    }
}