From 88f29d8b6d643a7043a2fba3f341feca0a1b2425 Mon Sep 17 00:00:00 2001
From: hmhealey <harrisonmhealey@gmail.com>
Date: Wed, 9 Dec 2015 16:35:49 -0500
Subject: Fixed EditChannelHeaderModal's contents not always being updated when
 a change is made

---
 web/react/components/edit_channel_header_modal.jsx | 52 ++++++++++++++--------
 1 file changed, 34 insertions(+), 18 deletions(-)

(limited to 'web/react/components/edit_channel_header_modal.jsx')

diff --git a/web/react/components/edit_channel_header_modal.jsx b/web/react/components/edit_channel_header_modal.jsx
index 209e30fcc..e4817f6e4 100644
--- a/web/react/components/edit_channel_header_modal.jsx
+++ b/web/react/components/edit_channel_header_modal.jsx
@@ -1,8 +1,9 @@
 // Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
 // See License.txt for license information.
 
+import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
 import * as Client from '../utils/client.jsx';
-import * as AsyncClient from '../utils/async_client.jsx';
+import Constants from '../utils/constants.jsx';
 import * as Utils from '../utils/utils.jsx';
 
 const Modal = ReactBootstrap.Modal;
@@ -11,12 +12,14 @@ export default class EditChannelHeaderModal extends React.Component {
     constructor(props) {
         super(props);
 
-        this.handleEdit = this.handleEdit.bind(this);
+        this.handleChange = this.handleChange.bind(this);
+        this.handleSubmit = this.handleSubmit.bind(this);
 
         this.onShow = this.onShow.bind(this);
         this.onHide = this.onHide.bind(this);
 
         this.state = {
+            header: props.channel.header,
             serverError: ''
         };
     }
@@ -27,27 +30,38 @@ export default class EditChannelHeaderModal extends React.Component {
         }
     }
 
+    componentWillReceiveProps(nextProps) {
+        if (this.props.channel.header !== nextProps.channel.header) {
+            this.setState({
+                header: nextProps.channel.header
+            });
+        }
+    }
+
     componentDidUpdate(prevProps) {
         if (this.props.show && !prevProps.show) {
             this.onShow();
         }
     }
 
-    handleEdit() {
-        var data = {};
-        data.channel_id = this.props.channel.id;
-
-        if (data.channel_id.length !== 26) {
-            return;
-        }
-
-        data.channel_header = ReactDOM.findDOMNode(this.refs.textarea).value;
+    handleChange(e) {
+        this.setState({
+            header: e.target.value
+        });
+    }
 
-        Client.updateChannelHeader(data,
-            () => {
+    handleSubmit() {
+        Client.updateChannelHeader(
+            this.props.channel.id,
+            this.state.header,
+            (channel) => {
                 this.setState({serverError: ''});
-                AsyncClient.getChannel(this.props.channel.id);
                 this.onHide();
+
+                AppDispatcher.handleServerAction({
+                    type: Constants.ActionTypes.RECIEVED_CHANNEL,
+                    channel
+                });
             },
             (err) => {
                 if (err.message === 'Invalid channel_header parameter') {
@@ -66,7 +80,8 @@ export default class EditChannelHeaderModal extends React.Component {
 
     onHide() {
         this.setState({
-            serverError: ''
+            serverError: '',
+            header: this.props.channel.header
         });
 
         this.props.onHide();
@@ -94,7 +109,8 @@ export default class EditChannelHeaderModal extends React.Component {
                         rows='6'
                         id='edit_header'
                         maxLength='1024'
-                        defaultValue={this.props.channel.header}
+                        value={this.state.header}
+                        onChange={this.handleChange}
                     />
                     {serverError}
                 </Modal.Body>
@@ -102,14 +118,14 @@ export default class EditChannelHeaderModal extends React.Component {
                     <button
                         type='button'
                         className='btn btn-default'
-                        onClick={this.props.onHide}
+                        onClick={this.onHide}
                     >
                         {'Cancel'}
                     </button>
                     <button
                         type='button'
                         className='btn btn-primary'
-                        onClick={this.handleEdit}
+                        onClick={this.handleSubmit}
                     >
                         {'Save'}
                     </button>
-- 
cgit v1.2.3-1-g7c22

