summaryrefslogtreecommitdiffstats
path: root/ui/src/components/post-listing.tsx
blob: c6c07d8b08427a14e16f1eb39dcc53495260527f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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<PostListingProps, PostListingState> {

  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 (
     <div>
     {!this.state.showEdit 
       ? this.listing()
       : <PostForm post={this.props.post} onEdit={this.handleEditPost} />
     }
   </div>
   )
  }

  listing() {
    let post = this.props.post;
    return (
      <div class="listing">
        <div className="float-left small text-center">
          <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 
            ? <h5 className="mb-0">
            <a className="text-white" href={post.url}>{post.name}</a>
            <small><a className="ml-2 text-muted font-italic" href={post.url}>{(new URL(post.url)).hostname}</a></small>
          </h5> 
            : <h5 className="mb-0"><Link className="text-white" to={`/post/${post.id}`}>{post.name}</Link></h5>
          }
        </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>
              <a href={post.creator_id.toString()}>{post.creator_name}</a>
              {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>
            {this.myPost && 
            <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>
          }
          {this.props.showBody && this.props.post.body && <div className="md-div" dangerouslySetInnerHTML={mdToHtml(post.body)} />}
        </div>
      </div>
    )
  }

  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);
  }

  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);
  }
}