1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from "rxjs";
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { UserOperation, Community as CommunityI, Post, GetPostsForm, SortType, ListingType, GetPostsResponse, CreatePostLikeResponse, CommunityUser} from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { PostListing } from './post-listing';
8 import { msgOp } from '../utils';
11 interface PostListingsProps {
15 interface PostListingsState {
16 community: CommunityI;
17 moderators: Array<CommunityUser>;
23 export class PostListings extends Component<PostListingsProps, PostListingsState> {
25 private subscription: Subscription;
26 private emptyState: PostListingsState = {
35 number_of_subscribers: null,
36 number_of_posts: null,
37 number_of_comments: null,
42 sortType: SortType.Hot,
43 type_: this.props.communityId
44 ? ListingType.Community
45 : UserService.Instance.loggedIn
46 ? ListingType.Subscribed
50 constructor(props: any, context: any) {
51 super(props, context);
54 this.state = this.emptyState;
56 this.subscription = WebSocketService.Instance.subject
57 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
59 (msg) => this.parseMessage(msg),
60 (err) => console.error(err),
61 () => console.log('complete')
64 let getPostsForm: GetPostsForm = {
65 type_: ListingType[this.state.type_],
66 community_id: this.props.communityId,
68 sort: SortType[SortType.Hot],
70 WebSocketService.Instance.getPosts(getPostsForm);
73 componentWillUnmount() {
74 this.subscription.unsubscribe();
80 <div>{this.selects()}</div>
81 {this.state.posts.length > 0
82 ? this.state.posts.map(post =>
83 <PostListing post={post} showCommunity={!this.props.communityId}/>)
84 : <div>No Listings. Subscribe to some <Link to="/communities">forums</Link>.</div>
92 <div className="mb-2">
93 <select value={this.state.sortType} onChange={linkEvent(this, this.handleSortChange)} class="custom-select w-auto">
94 <option disabled>Sort Type</option>
95 <option value={SortType.Hot}>Hot</option>
96 <option value={SortType.New}>New</option>
97 <option disabled>──────────</option>
98 <option value={SortType.TopDay}>Top Day</option>
99 <option value={SortType.TopWeek}>Week</option>
100 <option value={SortType.TopMonth}>Month</option>
101 <option value={SortType.TopYear}>Year</option>
102 <option value={SortType.TopAll}>All</option>
104 {!this.props.communityId &&
105 UserService.Instance.loggedIn &&
106 <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="ml-2 custom-select w-auto">
107 <option disabled>Type</option>
108 <option value={ListingType.All}>All</option>
109 <option value={ListingType.Subscribed}>Subscribed</option>
118 handleSortChange(i: PostListings, event: any) {
119 i.state.sortType = Number(event.target.value);
122 let getPostsForm: GetPostsForm = {
123 community_id: i.state.community.id,
125 sort: SortType[i.state.sortType],
126 type_: ListingType[ListingType.Community]
128 WebSocketService.Instance.getPosts(getPostsForm);
131 handleTypeChange(i: PostListings, event: any) {
132 i.state.type_ = Number(event.target.value);
135 let getPostsForm: GetPostsForm = {
137 sort: SortType[i.state.sortType],
138 type_: ListingType[i.state.type_]
140 WebSocketService.Instance.getPosts(getPostsForm);
143 parseMessage(msg: any) {
145 let op: UserOperation = msgOp(msg);
149 } else if (op == UserOperation.GetPosts) {
150 let res: GetPostsResponse = msg;
151 this.state.posts = res.posts;
152 this.setState(this.state);
153 } else if (op == UserOperation.CreatePostLike) {
154 let res: CreatePostLikeResponse = msg;
155 let found = this.state.posts.find(c => c.id == res.post.id);
156 found.my_vote = res.post.my_vote;
157 found.score = res.post.score;
158 found.upvotes = res.post.upvotes;
159 found.downvotes = res.post.downvotes;
160 this.setState(this.state);