summaryrefslogtreecommitdiffstats
path: root/web/react/components/change_url_modal.jsx
blob: f8db13392cfeacb276f0c13fbf12615a5684b040 (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
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

var Modal = ReactBootstrap.Modal;
var Utils = require('../utils/utils.jsx');

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) {
            React.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'>{'Must be longer than two characters'}<br/></span>);
        }
        if (url.charAt(0) === '-' || url.charAt(0) === '_') {
            error.push(<span key='error2'>{'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'>{'Must end with a letter or number'}<br/></span>);
        }
        if (url.indexOf('__') > -1) {
            error.push(<span key='error4'>{'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'>{'Invalid URL'}<br/></span>);
        }
        return error;
    }
    doSubmit(e) {
        e.preventDefault();

        const url = React.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();

        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}>
                                    <span
                                        data-toggle='tooltip'
                                        title={fullTeamUrl}
                                        className='input-group-addon'
                                    >
                                        {teamURL}
                                    </span>
                                    <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}
                        >
                            {'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
};