2019-04-03 06:49:32 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { Link } from 'inferno-router';
|
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { Post, CreatePostLikeForm, PostForm as PostFormI } from '../interfaces';
|
2019-04-03 06:49:32 +00:00
|
|
|
import { MomentTime } from './moment-time';
|
2019-04-03 20:59:37 +00:00
|
|
|
import { PostForm } from './post-form';
|
2019-04-03 06:49:32 +00:00
|
|
|
import { mdToHtml } from '../utils';
|
|
|
|
|
|
|
|
interface PostListingState {
|
2019-04-03 20:59:37 +00:00
|
|
|
showEdit: boolean;
|
2019-04-05 00:01:10 +00:00
|
|
|
iframeExpanded: boolean;
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PostListingProps {
|
|
|
|
post: Post;
|
2019-04-03 20:59:37 +00:00
|
|
|
editable?: boolean;
|
2019-04-03 06:49:32 +00:00
|
|
|
showCommunity?: boolean;
|
|
|
|
showBody?: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
viewOnly?: boolean;
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PostListing extends Component<PostListingProps, PostListingState> {
|
|
|
|
|
|
|
|
private emptyState: PostListingState = {
|
2019-04-05 00:01:10 +00:00
|
|
|
showEdit: false,
|
|
|
|
iframeExpanded: false
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-03 06:49:32 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
this.handlePostLike = this.handlePostLike.bind(this);
|
|
|
|
this.handlePostDisLike = this.handlePostDisLike.bind(this);
|
2019-04-03 20:59:37 +00:00
|
|
|
this.handleEditPost = this.handleEditPost.bind(this);
|
2019-04-05 02:08:21 +00:00
|
|
|
this.handleEditCancel = this.handleEditCancel.bind(this);
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{!this.state.showEdit
|
|
|
|
? this.listing()
|
2019-04-05 02:08:21 +00:00
|
|
|
: <PostForm post={this.props.post} onEdit={this.handleEditPost} onCancel={this.handleEditCancel}/>
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
)
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
listing() {
|
2019-04-03 06:49:32 +00:00
|
|
|
let post = this.props.post;
|
|
|
|
return (
|
|
|
|
<div class="listing">
|
2019-04-08 05:19:02 +00:00
|
|
|
<div className={`float-left small text-center ${this.props.viewOnly && 'no-click'}`}>
|
2019-04-03 06:49:32 +00:00
|
|
|
<div className={`pointer upvote ${post.my_vote == 1 ? 'text-info' : 'text-muted'}`} onClick={linkEvent(this, this.handlePostLike)}>▲</div>
|
|
|
|
<div>{post.score}</div>
|
|
|
|
<div className={`pointer downvote ${post.my_vote == -1 && 'text-danger'}`} onClick={linkEvent(this, this.handlePostDisLike)}>▼</div>
|
|
|
|
</div>
|
|
|
|
<div className="ml-4">
|
|
|
|
{post.url
|
2019-04-05 19:14:54 +00:00
|
|
|
? <div className="mb-0">
|
|
|
|
<h4 className="d-inline"><a className="text-white" href={post.url}>{post.name}</a></h4>
|
2019-04-03 06:49:32 +00:00
|
|
|
<small><a className="ml-2 text-muted font-italic" href={post.url}>{(new URL(post.url)).hostname}</a></small>
|
2019-04-05 00:01:10 +00:00
|
|
|
{ !this.state.iframeExpanded
|
|
|
|
? <span class="pointer ml-2 text-muted small" title="Expand here" onClick={linkEvent(this, this.handleIframeExpandClick)}>+</span>
|
|
|
|
:
|
|
|
|
<span>
|
|
|
|
<span class="pointer ml-2 text-muted small" onClick={linkEvent(this, this.handleIframeExpandClick)}>-</span>
|
|
|
|
<div class="embed-responsive embed-responsive-1by1">
|
|
|
|
<iframe scrolling="yes" class="embed-responsive-item" src={post.url}></iframe>
|
|
|
|
</div>
|
|
|
|
</span>
|
|
|
|
}
|
2019-04-05 19:14:54 +00:00
|
|
|
</div>
|
2019-04-05 00:01:10 +00:00
|
|
|
: <h4 className="mb-0"><Link className="text-white" to={`/post/${post.id}`}>{post.name}</Link></h4>
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
<div className="details ml-4 mb-1">
|
|
|
|
<ul class="list-inline mb-0 text-muted small">
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span>by </span>
|
2019-04-05 19:14:54 +00:00
|
|
|
<Link to={`/user/${post.creator_id}`}>{post.creator_name}</Link>
|
2019-04-03 06:49:32 +00:00
|
|
|
{this.props.showCommunity &&
|
|
|
|
<span>
|
|
|
|
<span> to </span>
|
|
|
|
<Link to={`/community/${post.community_id}`}>{post.community_name}</Link>
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span><MomentTime data={post} /></span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span>(
|
|
|
|
<span className="text-info">+{post.upvotes}</span>
|
|
|
|
<span> | </span>
|
|
|
|
<span className="text-danger">-{post.downvotes}</span>
|
|
|
|
<span>) </span>
|
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<Link to={`/post/${post.id}`}>{post.number_of_comments} Comments</Link>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2019-04-04 22:29:14 +00:00
|
|
|
{this.myPost &&
|
2019-04-03 20:59:37 +00:00
|
|
|
<ul class="list-inline mb-1 text-muted small font-weight-bold">
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
{this.props.showBody && this.props.post.body && <div className="md-div" dangerouslySetInnerHTML={mdToHtml(post.body)} />}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
private get myPost(): boolean {
|
|
|
|
return this.props.editable && UserService.Instance.loggedIn && this.props.post.creator_id == UserService.Instance.user.id;
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostLike(i: PostListing) {
|
2019-04-03 06:49:32 +00:00
|
|
|
|
|
|
|
let form: CreatePostLikeForm = {
|
|
|
|
post_id: i.props.post.id,
|
|
|
|
score: (i.props.post.my_vote == 1) ? 0 : 1
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.likePost(form);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostDisLike(i: PostListing) {
|
2019-04-03 06:49:32 +00:00
|
|
|
let form: CreatePostLikeForm = {
|
|
|
|
post_id: i.props.post.id,
|
|
|
|
score: (i.props.post.my_vote == -1) ? 0 : -1
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.likePost(form);
|
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleEditClick(i: PostListing) {
|
2019-04-03 20:59:37 +00:00
|
|
|
i.state.showEdit = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-05 02:08:21 +00:00
|
|
|
handleEditCancel() {
|
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-04 22:29:14 +00:00
|
|
|
// The actual editing is done in the recieve for post
|
2019-04-08 05:19:02 +00:00
|
|
|
handleEditPost() {
|
2019-04-03 20:59:37 +00:00
|
|
|
this.state.showEdit = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleDeleteClick(i: PostListing) {
|
2019-04-03 20:59:37 +00:00
|
|
|
let deleteForm: PostFormI = {
|
|
|
|
body: '',
|
|
|
|
community_id: i.props.post.community_id,
|
|
|
|
name: "deleted",
|
|
|
|
url: '',
|
|
|
|
edit_id: i.props.post.id,
|
|
|
|
auth: null
|
|
|
|
};
|
|
|
|
WebSocketService.Instance.editPost(deleteForm);
|
|
|
|
}
|
2019-04-05 00:01:10 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleIframeExpandClick(i: PostListing) {
|
2019-04-05 00:01:10 +00:00
|
|
|
i.state.iframeExpanded = !i.state.iframeExpanded;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
|
|
|
|