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
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import * as Client from '../utils/client.jsx';
import PostStore from '../stores/post_store.jsx';
import ModalStore from '../stores/modal_store.jsx';
var Modal = ReactBootstrap.Modal;
import * as Utils from '../utils/utils.jsx';
import * as AsyncClient from '../utils/async_client.jsx';
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import Constants from '../utils/constants.jsx';
import {FormattedMessage} from 'mm-intl';
var ActionTypes = Constants.ActionTypes;
export default class DeletePostModal extends React.Component {
constructor(props) {
super(props);
this.handleDelete = this.handleDelete.bind(this);
this.handleToggle = this.handleToggle.bind(this);
this.handleHide = this.handleHide.bind(this);
this.onListenerChange = this.onListenerChange.bind(this);
this.selectedList = null;
this.state = {
show: false,
post: null,
commentCount: 0,
error: ''
};
}
componentDidMount() {
ModalStore.addModalListener(ActionTypes.TOGGLE_DELETE_POST_MODAL, this.handleToggle);
PostStore.addSelectedPostChangeListener(this.onListenerChange);
}
componentWillUnmount() {
PostStore.removeSelectedPostChangeListener(this.onListenerChange);
ModalStore.removeModalListener(ActionTypes.TOGGLE_DELETE_POST_MODAL, this.handleToggle);
}
componentDidUpdate(prevProps, prevState) {
if (this.state.show && !prevState.show) {
setTimeout(() => {
$(ReactDOM.findDOMNode(this.refs.deletePostBtn)).focus();
}, 0);
}
}
handleDelete() {
Client.deletePost(
this.state.post.channel_id,
this.state.post.id,
() => {
var selectedList = this.selectedList;
if (selectedList && selectedList.order && selectedList.order.length > 0) {
var selectedPost = selectedList.posts[selectedList.order[0]];
if ((selectedPost.id === this.state.post.id && !this.state.root_id) || selectedPost.root_id === this.state.post.id) {
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_SEARCH,
results: null
});
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_POST_SELECTED,
results: null
});
} else if (selectedPost.id === this.state.post.id && this.state.root_id) {
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.RECEIVED_POST_SELECTED,
post_list: selectedList
});
AppDispatcher.handleServerAction({
type: ActionTypes.RECEIVED_SEARCH,
results: null
});
}
}
}
PostStore.deletePost(this.state.post);
AsyncClient.getPosts(this.state.post.channel_id);
},
(err) => {
AsyncClient.dispatchError(err, 'deletePost');
}
);
this.handleHide();
}
handleToggle(value, args) {
this.setState({
show: value,
post: args.post,
commentCount: args.commentCount,
error: ''
});
}
handleHide() {
this.setState({show: false});
}
onListenerChange() {
var newList = PostStore.getSelectedPost();
if (!Utils.areObjectsEqual(this.selectedList, newList)) {
this.selectedList = newList;
}
}
render() {
if (!this.state.post) {
return null;
}
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.commentCount > 0) {
commentWarning = (
<FormattedMessage
id='delete_post.warning'
defaultMessage='This post has {count} comment(s) on it.'
values={{
count: this.state.commentCount
}}
/>
);
}
const postTerm = this.state.post.root_id ? (
<FormattedMessage
id='delete_post.comment'
defaultMessage='Comment'
/>
) : (
<FormattedMessage
id='delete_post.post'
defaultMessage='Post'
/>
);
return (
<Modal
show={this.state.show}
onHide={this.handleHide}
>
<Modal.Header closeButton={true}>
<Modal.Title>
<FormattedMessage
id='delete_post.confirm'
defaultMessage='Confirm {term} Delete'
values={{
term: (postTerm)
}}
/>
</Modal.Title>
</Modal.Header>
<Modal.Body>
<FormattedMessage
id='delete_post.question'
defaultMessage='Are you sure you want to delete this {term}?'
values={{
term: (postTerm)
}}
/>
<br/>
<br/>
{commentWarning}
{error}
</Modal.Body>
<Modal.Footer>
<button
type='button'
className='btn btn-default'
onClick={this.handleHide}
>
<FormattedMessage
id='delete_post.cancel'
defaultMessage='Cancel'
/>
</button>
<button
ref='deletePostBtn'
type='button'
className='btn btn-danger'
onClick={this.handleDelete}
>
<FormattedMessage
id='delete_post.del'
defaultMessage='Delete'
/>
</button>
</Modal.Footer>
</Modal>
);
}
}
|