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, fetchLimit } from '../utils';
10 interface PostListingsProps {
14 interface PostListingsState {
15 community: CommunityI;
16 moderators: Array<CommunityUser>;
24 export class PostListings extends Component<PostListingsProps, PostListingsState> {
26 private subscription: Subscription;
27 private emptyState: PostListingsState = {
36 number_of_subscribers: null,
37 number_of_posts: null,
38 number_of_comments: null,
43 sortType: SortType.Hot,
44 type_: this.props.communityId
45 ? ListingType.Community
46 : UserService.Instance.user
47 ? ListingType.Subscribed
53 constructor(props: any, context: any) {
54 super(props, context);
57 this.state = this.emptyState;
59 this.subscription = WebSocketService.Instance.subject
60 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
62 (msg) => this.parseMessage(msg),
63 (err) => console.error(err),
64 () => console.log('complete')
70 componentWillUnmount() {
71 this.subscription.unsubscribe();
78 <h4><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> :
81 {this.state.posts.length > 0
82 ? this.state.posts.map(post =>
83 <PostListing post={post} showCommunity={!this.props.communityId}/>)
84 : <div>No Listings. {!this.props.communityId && <span>Subscribe to some <Link to="/communities">forums</Link>.</span>}</div>
95 <div className="mb-2">
96 <select value={this.state.sortType} onChange={linkEvent(this, this.handleSortChange)} class="custom-select w-auto">
97 <option disabled>Sort Type</option>
98 <option value={SortType.Hot}>Hot</option>
99 <option value={SortType.New}>New</option>
100 <option disabled>──────────</option>
101 <option value={SortType.TopDay}>Top Day</option>
102 <option value={SortType.TopWeek}>Week</option>
103 <option value={SortType.TopMonth}>Month</option>
104 <option value={SortType.TopYear}>Year</option>
105 <option value={SortType.TopAll}>All</option>
107 {!this.props.communityId &&
108 UserService.Instance.user &&
109 <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="ml-2 custom-select w-auto">
110 <option disabled>Type</option>
111 <option value={ListingType.All}>All</option>
112 <option value={ListingType.Subscribed}>Subscribed</option>
123 {this.state.page > 1 &&
124 <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
126 <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
131 nextPage(i: PostListings) {
137 prevPage(i: PostListings) {
143 handleSortChange(i: PostListings, event: any) {
144 i.state.sortType = Number(event.target.value);
151 let getPostsForm: GetPostsForm = {
152 community_id: this.state.community.id,
153 page: this.state.page,
155 sort: SortType[this.state.sortType],
156 type_: ListingType[ListingType.Community]
158 WebSocketService.Instance.getPosts(getPostsForm);
161 handleTypeChange(i: PostListings, event: any) {
162 i.state.type_ = Number(event.target.value);
168 parseMessage(msg: any) {
170 let op: UserOperation = msgOp(msg);
174 } else if (op == UserOperation.GetPosts) {
175 let res: GetPostsResponse = msg;
176 this.state.posts = res.posts;
177 this.state.loading = false;
178 this.setState(this.state);
179 } else if (op == UserOperation.CreatePostLike) {
180 let res: CreatePostLikeResponse = msg;
181 let found = this.state.posts.find(c => c.id == res.post.id);
182 found.my_vote = res.post.my_vote;
183 found.score = res.post.score;
184 found.upvotes = res.post.upvotes;
185 found.downvotes = res.post.downvotes;
186 this.setState(this.state);