import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { WebSocketService, UserService } from '../services'; import { Post, CreatePostLikeResponse, CreatePostLikeForm, PostForm as PostFormI } from '../interfaces'; import { MomentTime } from './moment-time'; import { PostForm } from './post-form'; import { mdToHtml } from '../utils'; interface PostListingState { showEdit: boolean; } interface PostListingProps { post: Post; editable?: boolean; showCommunity?: boolean; showBody?: boolean; } export class PostListing extends Component { private emptyState: PostListingState = { showEdit: false } constructor(props, context) { super(props, context); this.state = this.emptyState; this.handlePostLike = this.handlePostLike.bind(this); this.handlePostDisLike = this.handlePostDisLike.bind(this); this.handleEditPost = this.handleEditPost.bind(this); } render() { return (
{!this.state.showEdit ? this.listing() : }
) } listing() { let post = this.props.post; return (
{post.score}
{post.url ?
{post.name} {(new URL(post.url)).hostname}
:
{post.name}
}
  • by {post.creator_name} {this.props.showCommunity && to {post.community_name} }
  • ( +{post.upvotes} | -{post.downvotes} )
  • {post.number_of_comments} Comments
{this.myPost &&
  • edit
  • delete
} {this.props.showBody && this.props.post.body &&
}
) } private get myPost(): boolean { return this.props.editable && UserService.Instance.loggedIn && this.props.post.creator_id == UserService.Instance.user.id; } handlePostLike(i: PostListing, event) { let form: CreatePostLikeForm = { post_id: i.props.post.id, score: (i.props.post.my_vote == 1) ? 0 : 1 }; WebSocketService.Instance.likePost(form); } handlePostDisLike(i: PostListing, event) { let form: CreatePostLikeForm = { post_id: i.props.post.id, score: (i.props.post.my_vote == -1) ? 0 : -1 }; WebSocketService.Instance.likePost(form); } handleEditClick(i: PostListing, event) { i.state.showEdit = true; i.setState(i.state); } // The actual editing is done in the recieve for post handleEditPost(post: Post) { this.state.showEdit = false; this.setState(this.state); } handleDeleteClick(i: PostListing, event) { 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); } }