2015-06-14 23:53:32 -08:00
|
|
|
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
|
|
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
const AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
|
|
|
|
|
const Client = require('../utils/client.jsx');
|
|
|
|
|
const AsyncClient = require('../utils/async_client.jsx');
|
|
|
|
|
const ChannelStore = require('../stores/channel_store.jsx');
|
|
|
|
|
const PostStore = require('../stores/post_store.jsx');
|
|
|
|
|
const UserStore = require('../stores/user_store.jsx');
|
|
|
|
|
const SocketStore = require('../stores/socket_store.jsx');
|
|
|
|
|
const MsgTyping = require('./msg_typing.jsx');
|
|
|
|
|
const Textbox = require('./textbox.jsx');
|
|
|
|
|
const FileUpload = require('./file_upload.jsx');
|
|
|
|
|
const FilePreview = require('./file_preview.jsx');
|
|
|
|
|
const Utils = require('../utils/utils.jsx');
|
|
|
|
|
|
|
|
|
|
const Constants = require('../utils/constants.jsx');
|
|
|
|
|
const ActionTypes = Constants.ActionTypes;
|
|
|
|
|
|
|
|
|
|
export default class CreatePost extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.lastTime = 0;
|
|
|
|
|
|
|
|
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
|
|
|
this.postMsgKeyPress = this.postMsgKeyPress.bind(this);
|
|
|
|
|
this.handleUserInput = this.handleUserInput.bind(this);
|
|
|
|
|
this.resizePostHolder = this.resizePostHolder.bind(this);
|
|
|
|
|
this.handleUploadStart = this.handleUploadStart.bind(this);
|
|
|
|
|
this.handleFileUploadComplete = this.handleFileUploadComplete.bind(this);
|
|
|
|
|
this.handleUploadError = this.handleUploadError.bind(this);
|
|
|
|
|
this.removePreview = this.removePreview.bind(this);
|
|
|
|
|
this.onChange = this.onChange.bind(this);
|
|
|
|
|
this.getFileCount = this.getFileCount.bind(this);
|
|
|
|
|
|
|
|
|
|
PostStore.clearDraftUploads();
|
|
|
|
|
|
|
|
|
|
const draft = PostStore.getCurrentDraft();
|
|
|
|
|
let previews = [];
|
|
|
|
|
let messageText = '';
|
|
|
|
|
let uploadsInProgress = [];
|
2015-09-22 15:23:50 -07:00
|
|
|
if (draft && (draft.message || (draft.previews && draft.previews.length))) {
|
2015-08-31 11:31:55 -04:00
|
|
|
previews = draft.previews;
|
|
|
|
|
messageText = draft.message;
|
|
|
|
|
uploadsInProgress = draft.uploadsInProgress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
channelId: ChannelStore.getCurrentId(),
|
|
|
|
|
messageText: messageText,
|
|
|
|
|
uploadsInProgress: uploadsInProgress,
|
|
|
|
|
previews: previews,
|
|
|
|
|
submitting: false,
|
|
|
|
|
initialText: messageText
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-09-08 11:54:01 -04:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
|
if (prevState.previews.length !== this.state.previews.length) {
|
|
|
|
|
this.resizePostHolder();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
handleSubmit(e) {
|
2015-06-14 23:53:32 -08:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
if (this.state.uploadsInProgress.length > 0 || this.state.submitting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let post = {};
|
2015-06-14 23:53:32 -08:00
|
|
|
post.filenames = [];
|
|
|
|
|
post.message = this.state.messageText;
|
|
|
|
|
|
2015-06-18 17:39:12 -04:00
|
|
|
if (post.message.trim().length === 0 && this.state.previews.length === 0) {
|
2015-06-14 23:53:32 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (post.message.length > Constants.CHARACTER_LIMIT) {
|
2015-08-31 11:31:55 -04:00
|
|
|
this.setState({postError: `Post length must be less than ${Constants.CHARACTER_LIMIT} characters.`});
|
2015-06-14 23:53:32 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({submitting: true, serverError: null});
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
if (post.message.indexOf('/') === 0) {
|
2015-08-31 11:31:55 -04:00
|
|
|
Client.executeCommand(
|
2015-08-10 14:01:11 -04:00
|
|
|
this.state.channelId,
|
2015-06-14 23:53:32 -08:00
|
|
|
post.message,
|
|
|
|
|
false,
|
2015-08-31 11:31:55 -04:00
|
|
|
function handleCommandSuccess(data) {
|
2015-07-20 12:54:13 -07:00
|
|
|
PostStore.storeDraft(data.channel_id, null);
|
2015-08-10 14:01:11 -04:00
|
|
|
this.setState({messageText: '', submitting: false, postError: null, previews: [], serverError: null});
|
2015-06-14 23:53:32 -08:00
|
|
|
|
|
|
|
|
if (data.goto_location.length > 0) {
|
|
|
|
|
window.location.href = data.goto_location;
|
|
|
|
|
}
|
|
|
|
|
}.bind(this),
|
2015-08-31 11:31:55 -04:00
|
|
|
function handleCommandError(err) {
|
|
|
|
|
let state = {};
|
2015-08-10 14:01:11 -04:00
|
|
|
state.serverError = err.message;
|
2015-06-14 23:53:32 -08:00
|
|
|
state.submitting = false;
|
|
|
|
|
this.setState(state);
|
|
|
|
|
}.bind(this)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2015-08-10 14:01:11 -04:00
|
|
|
post.channel_id = this.state.channelId;
|
2015-06-14 23:53:32 -08:00
|
|
|
post.filenames = this.state.previews;
|
|
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
const time = Utils.getTimestamp();
|
|
|
|
|
const userId = UserStore.getCurrentId();
|
|
|
|
|
post.pending_post_id = `${userId}:${time}`;
|
2015-08-12 12:45:15 -04:00
|
|
|
post.user_id = userId;
|
2015-07-23 09:39:29 -04:00
|
|
|
post.create_at = time;
|
|
|
|
|
post.root_id = this.state.rootId;
|
|
|
|
|
post.parent_id = this.state.parentId;
|
|
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
const channel = ChannelStore.get(this.state.channelId);
|
2015-08-12 12:45:15 -04:00
|
|
|
|
|
|
|
|
PostStore.storePendingPost(post);
|
2015-08-12 14:52:43 -04:00
|
|
|
PostStore.storeDraft(channel.id, null);
|
2015-08-12 12:45:15 -04:00
|
|
|
this.setState({messageText: '', submitting: false, postError: null, previews: [], serverError: null});
|
2015-07-23 09:39:29 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
Client.createPost(post, channel,
|
|
|
|
|
function handlePostSuccess(data) {
|
2015-08-26 12:09:01 -04:00
|
|
|
AsyncClient.getPosts();
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let member = ChannelStore.getMember(channel.id);
|
2015-06-14 23:53:32 -08:00
|
|
|
member.msg_count = channel.total_msg_count;
|
2015-08-10 14:01:11 -04:00
|
|
|
member.last_viewed_at = Date.now();
|
2015-06-14 23:53:32 -08:00
|
|
|
ChannelStore.setChannelMember(member);
|
2015-08-12 12:45:15 -04:00
|
|
|
|
|
|
|
|
AppDispatcher.handleServerAction({
|
|
|
|
|
type: ActionTypes.RECIEVED_POST,
|
|
|
|
|
post: data
|
|
|
|
|
});
|
2015-09-03 12:28:19 -04:00
|
|
|
},
|
2015-08-31 11:31:55 -04:00
|
|
|
function handlePostError(err) {
|
|
|
|
|
let state = {};
|
2015-07-23 09:39:29 -04:00
|
|
|
|
2015-08-12 12:45:15 -04:00
|
|
|
if (err.message === 'Invalid RootId parameter') {
|
|
|
|
|
if ($('#post_deleted').length > 0) {
|
|
|
|
|
$('#post_deleted').modal('show');
|
|
|
|
|
}
|
2015-07-23 09:39:29 -04:00
|
|
|
PostStore.removePendingPost(post.pending_post_id);
|
|
|
|
|
} else {
|
2015-08-12 12:45:15 -04:00
|
|
|
post.state = Constants.POST_FAILED;
|
2015-07-23 09:39:29 -04:00
|
|
|
PostStore.updatePendingPost(post);
|
|
|
|
|
}
|
2015-07-21 15:55:34 -04:00
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
state.submitting = false;
|
|
|
|
|
this.setState(state);
|
|
|
|
|
}.bind(this)
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
postMsgKeyPress(e) {
|
2015-08-10 14:01:11 -04:00
|
|
|
if (e.which === 13 && !e.shiftKey && !e.altKey) {
|
2015-06-14 23:53:32 -08:00
|
|
|
e.preventDefault();
|
2015-08-31 11:31:55 -04:00
|
|
|
React.findDOMNode(this.refs.textbox).blur();
|
2015-06-14 23:53:32 -08:00
|
|
|
this.handleSubmit(e);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
const t = Date.now();
|
2015-06-14 23:53:32 -08:00
|
|
|
if ((t - this.lastTime) > 5000) {
|
2015-08-31 11:31:55 -04:00
|
|
|
SocketStore.sendMessage({channel_id: this.state.channelId, action: 'typing', props: {parent_id: ''}, state: {}});
|
2015-06-14 23:53:32 -08:00
|
|
|
this.lastTime = t;
|
|
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
handleUserInput(messageText) {
|
2015-08-28 14:43:48 -07:00
|
|
|
this.setState({messageText: messageText});
|
2015-07-16 15:25:28 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let draft = PostStore.getCurrentDraft();
|
|
|
|
|
draft.message = messageText;
|
2015-06-14 23:53:32 -08:00
|
|
|
PostStore.storeCurrentDraft(draft);
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
resizePostHolder() {
|
|
|
|
|
const height = $(window).height() - $(React.findDOMNode(this.refs.topDiv)).height() - $('#error_bar').outerHeight() - 50;
|
|
|
|
|
$('.post-list-holder-by-time').css('height', `${height}px`);
|
2015-06-14 23:53:32 -08:00
|
|
|
$(window).trigger('resize');
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
handleUploadStart(clientIds, channelId) {
|
|
|
|
|
let draft = PostStore.getDraft(channelId);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
draft.uploadsInProgress = draft.uploadsInProgress.concat(clientIds);
|
2015-08-10 14:01:11 -04:00
|
|
|
PostStore.storeDraft(channelId, draft);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
this.setState({uploadsInProgress: draft.uploadsInProgress});
|
|
|
|
|
}
|
|
|
|
|
handleFileUploadComplete(filenames, clientIds, channelId) {
|
|
|
|
|
let draft = PostStore.getDraft(channelId);
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-07 16:24:13 -04:00
|
|
|
// remove each finished file from uploads
|
2015-08-31 11:31:55 -04:00
|
|
|
for (let i = 0; i < clientIds.length; i++) {
|
|
|
|
|
const index = draft.uploadsInProgress.indexOf(clientIds[i]);
|
2015-08-07 16:24:13 -04:00
|
|
|
|
2015-08-10 14:01:11 -04:00
|
|
|
if (index !== -1) {
|
2015-08-31 11:31:55 -04:00
|
|
|
draft.uploadsInProgress.splice(index, 1);
|
2015-08-07 16:24:13 -04:00
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-07 16:24:13 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
draft.previews = draft.previews.concat(filenames);
|
2015-08-10 14:01:11 -04:00
|
|
|
PostStore.storeDraft(channelId, draft);
|
2015-08-07 16:24:13 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
this.setState({uploadsInProgress: draft.uploadsInProgress, previews: draft.previews});
|
|
|
|
|
}
|
|
|
|
|
handleUploadError(err, clientId) {
|
2015-08-13 15:44:38 -07:00
|
|
|
if (clientId !== -1) {
|
2015-08-31 11:31:55 -04:00
|
|
|
let draft = PostStore.getDraft(this.state.channelId);
|
2015-08-10 13:15:01 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
const index = draft.uploadsInProgress.indexOf(clientId);
|
2015-08-13 15:44:38 -07:00
|
|
|
if (index !== -1) {
|
2015-08-31 11:31:55 -04:00
|
|
|
draft.uploadsInProgress.splice(index, 1);
|
2015-08-13 15:44:38 -07:00
|
|
|
}
|
2015-08-10 13:15:01 -04:00
|
|
|
|
2015-08-13 15:44:38 -07:00
|
|
|
PostStore.storeDraft(this.state.channelId, draft);
|
2015-08-10 13:15:01 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
this.setState({uploadsInProgress: draft.uploadsInProgress, serverError: err});
|
2015-08-13 15:44:38 -07:00
|
|
|
} else {
|
|
|
|
|
this.setState({serverError: err});
|
|
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
removePreview(id) {
|
|
|
|
|
let previews = this.state.previews;
|
|
|
|
|
let uploadsInProgress = this.state.uploadsInProgress;
|
2015-08-10 08:41:18 -04:00
|
|
|
|
2015-08-10 12:05:45 -04:00
|
|
|
// id can either be the path of an uploaded file or the client id of an in progress upload
|
2015-08-31 11:31:55 -04:00
|
|
|
let index = previews.indexOf(id);
|
2015-08-10 08:41:18 -04:00
|
|
|
if (index !== -1) {
|
|
|
|
|
previews.splice(index, 1);
|
|
|
|
|
} else {
|
2015-08-10 12:05:45 -04:00
|
|
|
index = uploadsInProgress.indexOf(id);
|
2015-08-10 08:41:18 -04:00
|
|
|
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
uploadsInProgress.splice(index, 1);
|
2015-08-10 12:05:45 -04:00
|
|
|
this.refs.fileUpload.cancelUpload(id);
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-10 08:41:18 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let draft = PostStore.getCurrentDraft();
|
|
|
|
|
draft.previews = previews;
|
|
|
|
|
draft.uploadsInProgress = uploadsInProgress;
|
2015-06-14 23:53:32 -08:00
|
|
|
PostStore.storeCurrentDraft(draft);
|
2015-08-10 08:41:18 -04:00
|
|
|
|
|
|
|
|
this.setState({previews: previews, uploadsInProgress: uploadsInProgress});
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
ChannelStore.addChangeListener(this.onChange);
|
2015-06-14 23:53:32 -08:00
|
|
|
this.resizePostHolder();
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
ChannelStore.removeChangeListener(this.onChange);
|
|
|
|
|
}
|
|
|
|
|
onChange() {
|
|
|
|
|
const channelId = ChannelStore.getCurrentId();
|
2015-08-10 14:01:11 -04:00
|
|
|
if (this.state.channelId !== channelId) {
|
2015-08-31 11:31:55 -04:00
|
|
|
let draft = PostStore.getCurrentDraft();
|
2015-07-23 12:45:08 -04:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let previews = [];
|
|
|
|
|
let messageText = '';
|
|
|
|
|
let uploadsInProgress = [];
|
2015-09-22 15:23:50 -07:00
|
|
|
if (draft && (draft.message || (draft.previews && draft.previews.length))) {
|
2015-08-12 12:45:15 -04:00
|
|
|
previews = draft.previews;
|
|
|
|
|
messageText = draft.message;
|
|
|
|
|
uploadsInProgress = draft.uploadsInProgress;
|
2015-07-23 12:45:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setState({channelId: channelId, messageText: messageText, initialText: messageText, submitting: false, serverError: null, postError: null, previews: previews, uploadsInProgress: uploadsInProgress});
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|
|
|
|
|
getFileCount(channelId) {
|
2015-08-10 14:01:11 -04:00
|
|
|
if (channelId === this.state.channelId) {
|
2015-08-07 16:24:13 -04:00
|
|
|
return this.state.previews.length + this.state.uploadsInProgress.length;
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
|
|
|
|
|
const draft = PostStore.getDraft(channelId);
|
|
|
|
|
return draft.previews.length + draft.uploadsInProgress.length;
|
|
|
|
|
}
|
|
|
|
|
render() {
|
|
|
|
|
let serverError = null;
|
2015-08-10 14:01:11 -04:00
|
|
|
if (this.state.serverError) {
|
|
|
|
|
serverError = (
|
|
|
|
|
<div className='has-error'>
|
|
|
|
|
<label className='control-label'>{this.state.serverError}</label>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-06-14 23:53:32 -08:00
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let postError = null;
|
2015-08-10 14:01:11 -04:00
|
|
|
if (this.state.postError) {
|
|
|
|
|
postError = <label className='control-label'>{this.state.postError}</label>;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let preview = null;
|
2015-08-07 16:24:13 -04:00
|
|
|
if (this.state.previews.length > 0 || this.state.uploadsInProgress.length > 0) {
|
2015-06-14 23:53:32 -08:00
|
|
|
preview = (
|
|
|
|
|
<FilePreview
|
|
|
|
|
files={this.state.previews}
|
|
|
|
|
onRemove={this.removePreview}
|
2015-09-03 10:49:36 -04:00
|
|
|
uploadsInProgress={this.state.uploadsInProgress}
|
|
|
|
|
/>
|
2015-06-14 23:53:32 -08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-31 11:31:55 -04:00
|
|
|
let postFooterClassName = 'post-create-footer';
|
2015-08-10 14:01:11 -04:00
|
|
|
if (postError) {
|
|
|
|
|
postFooterClassName += ' has-error';
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-14 23:53:32 -08:00
|
|
|
return (
|
2015-08-31 11:31:55 -04:00
|
|
|
<form
|
|
|
|
|
id='create_post'
|
|
|
|
|
ref='topDiv'
|
|
|
|
|
role='form'
|
|
|
|
|
onSubmit={this.handleSubmit}
|
|
|
|
|
>
|
2015-08-10 14:01:11 -04:00
|
|
|
<div className='post-create'>
|
|
|
|
|
<div className='post-create-body'>
|
2015-09-08 15:47:15 -04:00
|
|
|
<div className='post-body__cell'>
|
|
|
|
|
<Textbox
|
|
|
|
|
onUserInput={this.handleUserInput}
|
|
|
|
|
onKeyPress={this.postMsgKeyPress}
|
|
|
|
|
onHeightChange={this.resizePostHolder}
|
|
|
|
|
messageText={this.state.messageText}
|
|
|
|
|
createMessage='Write a message...'
|
|
|
|
|
channelId={this.state.channelId}
|
|
|
|
|
id='post_textbox'
|
|
|
|
|
ref='textbox'
|
|
|
|
|
/>
|
|
|
|
|
<FileUpload
|
|
|
|
|
ref='fileUpload'
|
|
|
|
|
getFileCount={this.getFileCount}
|
|
|
|
|
onUploadStart={this.handleUploadStart}
|
|
|
|
|
onFileUpload={this.handleFileUploadComplete}
|
|
|
|
|
onUploadError={this.handleUploadError}
|
|
|
|
|
postType='post'
|
|
|
|
|
channelId=''
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<a
|
|
|
|
|
className='send-button theme'
|
|
|
|
|
onClick={this.handleSubmit}
|
|
|
|
|
>
|
|
|
|
|
<i className='fa fa-paper-plane' />
|
|
|
|
|
</a>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
2015-08-10 14:01:11 -04:00
|
|
|
<div className={postFooterClassName}>
|
|
|
|
|
{postError}
|
|
|
|
|
{serverError}
|
|
|
|
|
{preview}
|
2015-08-31 11:31:55 -04:00
|
|
|
<MsgTyping
|
|
|
|
|
channelId={this.state.channelId}
|
|
|
|
|
parentId=''
|
|
|
|
|
/>
|
2015-06-14 23:53:32 -08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-08-31 11:31:55 -04:00
|
|
|
}
|