summaryrefslogtreecommitdiffstats
path: root/webapp/components/change_url_modal.jsx
blob: 9a526a9eef003b10be1b007cb5a52c62349058c6 (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
224
225
226
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import ReactDOM from 'react-dom';
import Constants from 'utils/constants.jsx';
import {Modal, Tooltip, OverlayTrigger} from 'react-bootstrap';
import * as Utils from 'utils/utils.jsx';

import {FormattedMessage} from 'react-intl';

import React from 'react';

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

        this.onURLChanged = this.onURLChanged.bind(this);
        this.doSubmit = this.doSubmit.bind(this);
        this.doCancel = this.doCancel.bind(this);

        this.state = {
            currentURL: props.currentURL,
            urlError: '',
            userEdit: false
        };
    }
    componentWillReceiveProps(nextProps) {
        // This check prevents the url being deleted when we re-render
        // because of user status check
        if (!this.state.userEdit) {
            this.setState({
                currentURL: nextProps.currentURL
            });
        }
    }
    componentDidUpdate(prevProps) {
        if (this.props.show === true && prevProps.show === false) {
            ReactDOM.findDOMNode(this.refs.urlinput).select();
        }
    }
    onURLChanged(e) {
        const url = e.target.value.trim();
        this.setState({currentURL: url.replace(/[^A-Za-z0-9-_]/g, '').toLowerCase(), userEdit: true});
    }
    getURLError(url) {
        let error = []; //eslint-disable-line prefer-const
        if (url.length < 2) {
            error.push(
                <span key='error1'>
                    <FormattedMessage
                        id='change_url.longer'
                        defaultMessage='Must be longer than two characters'
                    />
                    <br/>
                </span>
            );
        }
        if (url.charAt(0) === '-' || url.charAt(0) === '_') {
            error.push(
                <span key='error2'>
                    <FormattedMessage
                        id='change_url.startWithLetter'
                        defaultMessage='Must start with a letter or number'
                    />
                    <br/>
                </span>
            );
        }
        if (url.length > 1 && (url.charAt(url.length - 1) === '-' || url.charAt(url.length - 1) === '_')) {
            error.push(
                <span key='error3'>
                    <FormattedMessage
                        id='change_url.endWithLetter'
                        defaultMessage='Must end with a letter or number'
                    />
                    <br/>
                </span>);
        }
        if (url.indexOf('__') > -1) {
            error.push(
                <span key='error4'>
                    <FormattedMessage
                        id='change_url.noUnderscore'
                        defaultMessage='Can not contain two underscores in a row.'
                    />
                    <br/>
                </span>);
        }

        // In case of error we don't detect
        if (error.length === 0) {
            error.push(
                <span key='errorlast'>
                    <FormattedMessage
                        id='change_url.invalidUrl'
                        defaultMessage='Invalid URL'
                    />
                    <br/>
                </span>);
        }
        return error;
    }
    doSubmit(e) {
        e.preventDefault();

        const url = ReactDOM.findDOMNode(this.refs.urlinput).value;
        const cleanedURL = Utils.cleanUpUrlable(url);
        if (cleanedURL !== url || url.length < 2 || url.indexOf('__') > -1) {
            this.setState({urlError: this.getURLError(url)});
            return;
        }
        this.setState({urlError: '', userEdit: false});
        this.props.onModalSubmit(url);
    }
    doCancel() {
        this.setState({urlError: '', userEdit: false});
        this.props.onModalDismissed();
    }
    render() {
        let urlClass = 'input-group input-group--limit';
        let urlError = null;
        let serverError = null;

        if (this.state.urlError) {
            urlClass += ' has-error';
            urlError = (<p className='input__help error'>{this.state.urlError}</p>);
        }

        if (this.props.serverError) {
            serverError = <div className='form-group has-error'><p className='input__help error'>{this.props.serverError}</p></div>;
        }

        const fullTeamUrl = Utils.getTeamURLFromAddressBar();
        const teamURL = Utils.getShortenedTeamURL();
        const urlTooltip = (
            <Tooltip id='urlTooltip'>{fullTeamUrl}</Tooltip>
        );

        return (
            <Modal
                show={this.props.show}
                onHide={this.doCancel}
            >
                <Modal.Header closeButton={true}>
                    <Modal.Title>{this.props.title}</Modal.Title>
                </Modal.Header>
                <form
                    role='form'
                    className='form-horizontal'
                >
                    <Modal.Body>
                        <div className='modal-intro'>{this.props.description}</div>
                        <div className='form-group'>
                            <label className='col-sm-2 form__label control-label'>{this.props.urlLabel}</label>
                            <div className='col-sm-10'>
                                <div className={urlClass}>
                                    <OverlayTrigger
                                        delayShow={Constants.OVERLAY_TIME_DELAY}
                                        placement='top'
                                        overlay={urlTooltip}
                                    >
                                        <span className='input-group-addon'>{teamURL}</span>
                                    </OverlayTrigger>
                                    <input
                                        type='text'
                                        ref='urlinput'
                                        className='form-control'
                                        maxLength='22'
                                        onChange={this.onURLChanged}
                                        value={this.state.currentURL}
                                        autoFocus={true}
                                        tabIndex='1'
                                    />
                                </div>
                                {urlError}
                                {serverError}
                            </div>
                        </div>
                    </Modal.Body>
                    <Modal.Footer>
                        <button
                            type='button'
                            className='btn btn-default'
                            onClick={this.doCancel}
                        >
                            <FormattedMessage
                                id='change_url.close'
                                defaultMessage='Close'
                            />
                        </button>
                        <button
                            onClick={this.doSubmit}
                            type='submit'
                            className='btn btn-primary'
                            tabIndex='2'
                        >
                            {this.props.submitButtonText}
                        </button>
                    </Modal.Footer>
                </form>
            </Modal>
        );
    }
}

ChangeUrlModal.defaultProps = {
    show: false,
    title: 'Change URL',
    desciption: '',
    urlLabel: 'URL',
    submitButtonText: 'Save',
    currentURL: '',
    serverError: ''
};

ChangeUrlModal.propTypes = {
    show: React.PropTypes.bool.isRequired,
    title: React.PropTypes.string,
    description: React.PropTypes.string,
    urlLabel: React.PropTypes.string,
    submitButtonText: React.PropTypes.string,
    currentURL: React.PropTypes.string,
    serverError: React.PropTypes.string,
    onModalSubmit: React.PropTypes.func.isRequired,
    onModalDismissed: React.PropTypes.func.isRequired
};