Files
mattermost/web/react/components/post.jsx

195 lines
6.2 KiB
React
Raw Normal View History

2015-06-14 23:53:32 -08:00
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var PostHeader = require('./post_header.jsx');
var PostBody = require('./post_body.jsx');
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
var Constants = require('../utils/constants.jsx');
var UserStore = require('../stores/user_store.jsx');
2015-07-23 09:39:29 -04:00
var PostStore = require('../stores/post_store.jsx');
var ChannelStore = require('../stores/channel_store.jsx');
var client = require('../utils/client.jsx');
var AsyncClient = require('../utils/async_client.jsx');
2015-06-14 23:53:32 -08:00
var ActionTypes = Constants.ActionTypes;
var utils = require('../utils/utils.jsx');
2015-06-14 23:53:32 -08:00
var PostInfo = require('./post_info.jsx');
2015-08-31 10:10:22 -07:00
export default class Post extends React.Component {
constructor(props) {
super(props);
this.handleCommentClick = this.handleCommentClick.bind(this);
this.forceUpdateInfo = this.forceUpdateInfo.bind(this);
this.retryPost = this.retryPost.bind(this);
this.state = {};
}
handleCommentClick(e) {
2015-06-14 23:53:32 -08:00
e.preventDefault();
2015-07-11 08:32:02 -07:00
var data = {};
2015-06-14 23:53:32 -08:00
data.order = [this.props.post.id];
data.posts = this.props.posts;
AppDispatcher.handleServerAction({
type: ActionTypes.RECIEVED_POST_SELECTED,
post_list: data
});
AppDispatcher.handleServerAction({
type: ActionTypes.RECIEVED_SEARCH,
results: null
});
2015-08-31 10:10:22 -07:00
}
forceUpdateInfo() {
this.refs.info.forceUpdate();
this.refs.header.forceUpdate();
2015-08-31 10:10:22 -07:00
}
retryPost(e) {
2015-07-23 09:39:29 -04:00
e.preventDefault();
var post = this.props.post;
client.createPost(post, post.channel_id,
2015-08-31 10:10:22 -07:00
function success(data) {
AsyncClient.getPosts();
2015-07-23 09:39:29 -04:00
var channel = ChannelStore.get(post.channel_id);
2015-07-23 09:39:29 -04:00
var member = ChannelStore.getMember(post.channel_id);
member.msg_count = channel.total_msg_count;
2015-08-31 10:10:22 -07:00
member.last_viewed_at = utils.getTimestamp();
2015-07-23 09:39:29 -04:00
ChannelStore.setChannelMember(member);
AppDispatcher.handleServerAction({
type: ActionTypes.RECIEVED_POST,
post: data
});
2015-08-31 10:10:22 -07:00
},
function error() {
post.state = Constants.POST_FAILED;
2015-07-23 09:39:29 -04:00
PostStore.updatePendingPost(post);
this.forceUpdate();
}.bind(this)
);
post.state = Constants.POST_LOADING;
2015-07-23 09:39:29 -04:00
PostStore.updatePendingPost(post);
this.forceUpdate();
2015-08-31 10:10:22 -07:00
}
shouldComponentUpdate(nextProps) {
if (!utils.areStatesEqual(nextProps.post, this.props.post)) {
return true;
}
return false;
2015-08-31 10:10:22 -07:00
}
render() {
2015-06-14 23:53:32 -08:00
var post = this.props.post;
var parentPost = this.props.parentPost;
var posts = this.props.posts;
var type = 'Post';
if (post.root_id && post.root_id.length > 0) {
type = 'Comment';
2015-06-14 23:53:32 -08:00
}
var commentCount = 0;
2015-08-31 10:10:22 -07:00
var commentRootId;
if (parentPost) {
commentRootId = post.root_id;
} else {
commentRootId = post.id;
}
2015-06-14 23:53:32 -08:00
for (var postId in posts) {
2015-08-31 10:10:22 -07:00
if (posts[postId].root_id === commentRootId) {
2015-06-14 23:53:32 -08:00
commentCount += 1;
}
}
2015-08-31 10:10:22 -07:00
var rootUser;
if (this.props.sameRoot) {
rootUser = 'same--root';
} else {
rootUser = 'other--root';
}
2015-06-14 23:53:32 -08:00
var postType = '';
2015-08-31 10:10:22 -07:00
if (type !== 'Post') {
postType = 'post--comment';
2015-06-14 23:53:32 -08:00
}
var currentUserCss = '';
if (UserStore.getCurrentId() === post.user_id) {
currentUserCss = 'current--user';
}
var userProfile = UserStore.getProfile(post.user_id);
var timestamp = UserStore.getCurrentUser().update_at;
if (userProfile) {
timestamp = userProfile.update_at;
}
var sameUserClass = '';
if (this.props.sameUser) {
sameUserClass = 'same--user';
}
2015-08-31 10:10:22 -07:00
var profilePic = null;
if (this.props.hideProfilePic) {
profilePic = (
<div className='post-profile-img__container'>
<img
className='post-profile-img'
src={'/api/v1/users/' + post.user_id + '/image?time=' + timestamp}
height='36'
width='36' />
</div>
);
}
2015-06-14 23:53:32 -08:00
return (
<div>
2015-08-31 10:10:22 -07:00
<div
id={post.id}
className={'post ' + sameUserClass + ' ' + rootUser + ' ' + postType + ' ' + currentUserCss} >
{profilePic}
<div className='post__content'>
2015-08-31 10:10:22 -07:00
<PostHeader
ref='header'
post={post}
sameRoot={this.props.sameRoot}
commentCount={commentCount}
handleCommentClick={this.handleCommentClick}
isLastComment={this.props.isLastComment} />
<PostBody
post={post}
sameRoot={this.props.sameRoot}
parentPost={parentPost}
posts={posts}
handleCommentClick={this.handleCommentClick}
retryPost={this.retryPost} />
<PostInfo
ref='info'
post={post}
sameRoot={this.props.sameRoot}
commentCount={commentCount}
handleCommentClick={this.handleCommentClick}
allowReply='true' />
2015-06-14 23:53:32 -08:00
</div>
</div>
</div>
);
}
2015-08-31 10:10:22 -07:00
}
Post.propTypes = {
post: React.PropTypes.object,
posts: React.PropTypes.object,
parentPost: React.PropTypes.object,
sameUser: React.PropTypes.bool,
sameRoot: React.PropTypes.bool,
hideProfilePic: React.PropTypes.bool,
isLastComment: React.PropTypes.bool
};