Files
mattermost/webapp/components/post.jsx

256 lines
7.9 KiB
React
Raw Normal View History

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
import PostHeader from './post_header.jsx';
import PostBody from './post_body.jsx';
2016-03-14 08:50:46 -04:00
import PostStore from 'stores/post_store.jsx';
import ChannelStore from 'stores/channel_store.jsx';
2016-03-14 08:50:46 -04:00
import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
2016-03-14 08:50:46 -04:00
import * as Client from 'utils/client.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
import * as Utils from 'utils/utils.jsx';
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
2015-06-14 23:53:32 -08:00
2016-03-14 08:50:46 -04:00
import React from 'react';
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();
AppDispatcher.handleServerAction({
2016-02-08 10:33:59 -05:00
type: ActionTypes.RECEIVED_POST_SELECTED,
postId: Utils.getRootId(this.props.post)
2015-06-14 23:53:32 -08:00
});
AppDispatcher.handleServerAction({
2016-02-08 10:33:59 -05:00
type: ActionTypes.RECEIVED_SEARCH,
2015-06-14 23:53:32 -08:00
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,
(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;
member.last_viewed_at = Utils.getTimestamp();
2015-07-23 09:39:29 -04:00
ChannelStore.setChannelMember(member);
AppDispatcher.handleServerAction({
2016-02-08 10:33:59 -05:00
type: ActionTypes.RECEIVED_POST,
post: data
});
2015-08-31 10:10:22 -07:00
},
() => {
post.state = Constants.POST_FAILED;
2015-07-23 09:39:29 -04:00
PostStore.updatePendingPost(post);
this.forceUpdate();
}
2015-07-23 09:39:29 -04:00
);
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.areObjectsEqual(nextProps.post, this.props.post)) {
return true;
}
if (nextProps.sameRoot !== this.props.sameRoot) {
return true;
}
2015-06-14 23:53:32 -08:00
if (nextProps.sameUser !== this.props.sameUser) {
return true;
}
if (nextProps.displayNameType !== this.props.displayNameType) {
return true;
}
if (this.getCommentCount(nextProps) !== this.getCommentCount(this.props)) {
return true;
2015-06-14 23:53:32 -08:00
}
if (nextProps.shouldHighlight !== this.props.shouldHighlight) {
return true;
}
if (!Utils.areObjectsEqual(nextProps.user, this.props.user)) {
return true;
}
return false;
}
getCommentCount(props) {
const post = props.post;
const parentPost = props.parentPost;
const posts = props.posts;
let commentCount = 0;
let commentRootId;
2015-08-31 10:10:22 -07:00
if (parentPost) {
commentRootId = post.root_id;
} else {
commentRootId = post.id;
}
for (const 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;
}
}
return commentCount;
}
render() {
const post = this.props.post;
const parentPost = this.props.parentPost;
const posts = this.props.posts;
2016-04-01 23:17:56 +05:00
const mattermostLogo = Constants.MATTERMOST_ICON_SVG;
2015-10-22 08:45:28 -04:00
if (!post.props) {
post.props = {};
}
let type = 'Post';
if (post.root_id && post.root_id.length > 0) {
type = 'Comment';
}
const commentCount = this.getCommentCount(this.props);
let rootUser;
2015-08-31 10:10:22 -07:00
if (this.props.sameRoot) {
rootUser = 'same--root';
} else {
rootUser = 'other--root';
}
2015-06-14 23:53:32 -08:00
let postType = '';
2015-08-31 10:10:22 -07:00
if (type !== 'Post') {
postType = 'post--comment';
2015-11-19 01:11:06 +05:00
} else if (commentCount > 0) {
postType = 'post--root';
2015-06-14 23:53:32 -08:00
}
let currentUserCss = '';
2016-02-08 07:26:10 -05:00
if (this.props.currentUser.id === post.user_id && !post.props.from_webhook && !Utils.isSystemMessage(post)) {
currentUserCss = 'current--user';
}
2016-02-08 07:26:10 -05:00
let timestamp = 0;
if (!this.props.user || this.props.user.update_at == null) {
timestamp = this.props.currentUser.update_at;
} else {
timestamp = this.props.user.update_at;
}
let sameUserClass = '';
if (this.props.sameUser) {
sameUserClass = 'same--user';
}
let shouldHighlightClass = '';
if (this.props.shouldHighlight) {
shouldHighlightClass = 'post--highlight';
}
let systemMessageClass = '';
if (Utils.isSystemMessage(post)) {
systemMessageClass = 'post--system';
}
let profilePic = null;
if (!this.props.hideProfilePic) {
2015-08-31 10:10:22 -07:00
profilePic = (
2015-11-19 01:11:06 +05:00
<img
src={Utils.getProfilePicSrcForPost(post, timestamp)}
2015-11-19 01:11:06 +05:00
height='36'
width='36'
/>
2015-08-31 10:10:22 -07:00
);
2016-04-01 23:17:56 +05:00
if (Utils.isSystemMessage(post)) {
profilePic = (
<span
className='icon'
dangerouslySetInnerHTML={{__html: mattermostLogo}}
/>
);
}
2015-08-31 10:10:22 -07:00
}
2015-06-14 23:53:32 -08:00
return (
<div>
2015-08-31 10:10:22 -07:00
<div
2015-09-11 08:38:18 -04:00
id={'post_' + post.id}
className={'post ' + sameUserClass + ' ' + rootUser + ' ' + postType + ' ' + currentUserCss + ' ' + shouldHighlightClass + ' ' + systemMessageClass}
>
<div className='post__content'>
2015-11-19 01:11:06 +05:00
<div className='post__img'>{profilePic}</div>
<div>
<PostHeader
ref='header'
post={post}
sameRoot={this.props.sameRoot}
commentCount={commentCount}
handleCommentClick={this.handleCommentClick}
isLastComment={this.props.isLastComment}
sameUser={this.props.sameUser}
user={this.props.user}
2016-02-08 07:26:10 -05:00
currentUser={this.props.currentUser}
2015-11-19 01:11:06 +05:00
/>
<PostBody
post={post}
sameRoot={this.props.sameRoot}
parentPost={parentPost}
posts={posts}
handleCommentClick={this.handleCommentClick}
retryPost={this.retryPost}
/>
</div>
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.isRequired,
2015-08-31 10:10:22 -07:00
posts: React.PropTypes.object,
parentPost: React.PropTypes.object,
user: React.PropTypes.object,
2015-08-31 10:10:22 -07:00
sameUser: React.PropTypes.bool,
sameRoot: React.PropTypes.bool,
hideProfilePic: React.PropTypes.bool,
isLastComment: React.PropTypes.bool,
shouldHighlight: React.PropTypes.bool,
2016-02-08 07:26:10 -05:00
displayNameType: React.PropTypes.string,
hasProfiles: React.PropTypes.bool,
currentUser: React.PropTypes.object.isRequired
2015-08-31 10:10:22 -07:00
};