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

const Client = require('../utils/client.jsx');
const AsyncClient = require('../utils/async_client.jsx');

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

        this.handleEdit = this.handleEdit.bind(this);
        this.handleUserInput = this.handleUserInput.bind(this);
        this.handleClose = this.handleClose.bind(this);

        this.state = {
            description: '',
            title: '',
            channelId: '',
            serverError: ''
        };
    }
    handleEdit() {
        var data = {};
        data.channel_id = this.state.channelId;

        if (data.channel_id.length !== 26) {
            return;
        }

        data.channel_description = this.state.description.trim();

        Client.updateChannelDesc(data,
            function handleUpdateSuccess() {
                this.setState({serverError: ''});
                AsyncClient.getChannel(this.state.channelId);
                $(React.findDOMNode(this.refs.modal)).modal('hide');
            }.bind(this),
            function handleUpdateError(err) {
                if (err.message === 'Invalid channel_description parameter') {
                    this.setState({serverError: 'This description is too long, please enter a shorter one'});
                } else {
                    this.setState({serverError: err.message});
                }
            }.bind(this)
        );
    }
    handleUserInput(e) {
        this.setState({description: e.target.value});
    }
    handleClose() {
        this.setState({description: '', serverError: ''});
    }
    componentDidMount() {
        $(React.findDOMNode(this.refs.modal)).on('show.bs.modal', function handleShow(e) {
            const button = e.relatedTarget;
            this.setState({description: $(button).attr('data-desc'), title: $(button).attr('data-title'), channelId: $(button).attr('data-channelid'), serverError: ''});
        }.bind(this));
        $(React.findDOMNode(this.refs.modal)).on('hidden.bs.modal', this.handleClose);
    }
    componentWillUnmount() {
        $(React.findDOMNode(this.refs.modal)).off('hidden.bs.modal', this.handleClose);
    }
    render() {
        var serverError = null;
        if (this.state.serverError) {
            serverError = <div className='form-group has-error'><br/><label className='control-label'>{this.state.serverError}</label></div>;
        }

        var editTitle = (
            <h4
                className='modal-title'
                ref='title'
            >
                Edit Description
            </h4>
        );
        if (this.state.title) {
            editTitle = (
                <h4
                    className='modal-title'
                    ref='title'
                >
                    Edit Description for <span className='name'>{this.state.title}</span>
                </h4>
            );
        }

        return (
            <div
                className='modal fade'
                ref='modal'
                id='edit_channel'
                role='dialog'
                tabIndex='-1'
                aria-hidden='true'
            >
                <div className='modal-dialog'>
                    <div className='modal-content'>
                        <div className='modal-header'>
                            <button
                                type='button'
                                className='close'
                                data-dismiss='modal'
                                aria-label='Close'
                            >
                                <span aria-hidden='true'>&times;</span>
                            </button>
                            {editTitle}
                        </div>
                        <div className='modal-body'>
                            <textarea
                                className='form-control no-resize'
                                rows='6'
                                ref='channelDesc'
                                maxLength='1024'
                                value={this.state.description}
                                onChange={this.handleUserInput}
                            />
                            {serverError}
                        </div>
                        <div className='modal-footer'>
                            <button
                                type='button'
                                className='btn btn-default'
                                data-dismiss='modal'
                            >
                                Cancel
                            </button>
                            <button
                                type='button'
                                className='btn btn-primary'
                                onClick={this.handleEdit}
                            >
                                Save
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}