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

var Client = require('../utils/client.jsx');
var PostStore = require('../stores/post_store.jsx');
var BrowserStore = require('../stores/browser_store.jsx');
var Utils = require('../utils/utils.jsx');
var AsyncClient = require('../utils/async_client.jsx');
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;

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

        this.handleDelete = this.handleDelete.bind(this);
        this.onListenerChange = this.onListenerChange.bind(this);
        this.onShow = this.onShow.bind(this);

        this.state = {title: '', postId: '', channelId: '', selectedList: PostStore.getSelectedPost(), comments: 0};
    }
    handleDelete() {
        Client.deletePost(this.state.channelId, this.state.postId,
            function deleteSuccess() {
                var selectedList = this.state.selectedList;
                if (selectedList && selectedList.order && selectedList.order.length > 0) {
                    var selectedPost = selectedList.posts[selectedList.order[0]];
                    if ((selectedPost.id === this.state.postId && this.state.title === 'Post') || selectedPost.root_id === this.state.postId) {
                        AppDispatcher.handleServerAction({
                            type: ActionTypes.RECIEVED_SEARCH,
                            results: null
                        });

                        AppDispatcher.handleServerAction({
                            type: ActionTypes.RECIEVED_POST_SELECTED,
                            results: null
                        });
                    } else if (selectedPost.id === this.state.postId && this.state.title === 'Comment') {
                        if (selectedPost.root_id && selectedPost.root_id.length > 0 && selectedList.posts[selectedPost.root_id]) {
                            selectedList.order = [selectedPost.root_id];
                            delete selectedList.posts[selectedPost.id];

                            AppDispatcher.handleServerAction({
                                type: ActionTypes.RECIEVED_POST_SELECTED,
                                post_list: selectedList
                            });

                            AppDispatcher.handleServerAction({
                                type: ActionTypes.RECIEVED_SEARCH,
                                results: null
                            });
                        }
                    }
                }
                PostStore.removePost(this.state.postId, this.state.channelId);
                AsyncClient.getPosts(this.state.channelId);
            }.bind(this),
            function deleteFailed(err) {
                AsyncClient.dispatchError(err, 'deletePost');
            }
        );
    }
    onShow(e) {
        var newState = {};
        if (BrowserStore.getItem('edit_state_transfer')) {
            newState = BrowserStore.getItem('edit_state_transfer');
            BrowserStore.removeItem('edit_state_transfer');
        } else {
            var button = e.relatedTarget;
            newState = {title: $(button).attr('data-title'), channelId: $(button).attr('data-channelid'), postId: $(button).attr('data-postid'), comments: $(button).attr('data-comments')};
        }
        this.setState(newState);
    }
    componentDidMount() {
        $(ReactDOM.findDOMNode(this.refs.modal)).on('show.bs.modal', this.onShow);
        PostStore.addSelectedPostChangeListener(this.onListenerChange);
    }
    componentWillUnmount() {
        PostStore.removeSelectedPostChangeListener(this.onListenerChange);
    }
    onListenerChange() {
        var newList = PostStore.getSelectedPost();
        if (!Utils.areObjectsEqual(this.state.selectedList, newList)) {
            this.setState({selectedList: newList});
        }
    }
    render() {
        var error = null;
        if (this.state.error) {
            error = <div className='form-group has-error'><label className='control-label'>{this.state.error}</label></div>;
        }

        var commentWarning = '';
        if (this.state.comments > 0) {
            commentWarning = 'This post has ' + this.state.comments + ' comment(s) on it.';
        }

        return (
            <div
                className='modal fade'
                id='delete_post'
                ref='modal'
                role='dialog'
                tabIndex='-1'
                aria-hidden='true'
            >
              <div className='modal-dialog modal-push-down'>
                <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>
                    <h4 className='modal-title'>Confirm {this.state.title} Delete</h4>
                  </div>
                  <div className='modal-body'>
                    Are you sure you want to delete the {this.state.title.toLowerCase()}?
                    <br/>
                    <br/>
                    {commentWarning}
                  </div>
                  {error}
                  <div className='modal-footer'>
                    <button
                        type='button'
                        className='btn btn-default'
                        data-dismiss='modal'
                    >
                        Cancel
                    </button>
                    <button
                        type='button'
                        className='btn btn-danger'
                        data-dismiss='modal'
                        onClick={this.handleDelete}
                    >
                        Delete
                    </button>
                  </div>
                </div>
              </div>
            </div>
        );
    }
}