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, 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 {
15 interface PostListingsState {
16 moderators: Array<CommunityUser>;
24 export class PostListings extends Component<PostListingsProps, PostListingsState> {
26 private subscription: Subscription;
27 private emptyState: PostListingsState = {
30 sortType: SortType.Hot,
31 type_: (this.props.type !== undefined && UserService.Instance.user) ? this.props.type :
32 this.props.communityId
33 ? ListingType.Community
34 : UserService.Instance.user
35 ? ListingType.Subscribed
41 constructor(props: any, context: any) {
42 super(props, context);
44 this.state = this.emptyState;
46 this.subscription = WebSocketService.Instance.subject
47 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
49 (msg) => this.parseMessage(msg),
50 (err) => console.error(err),
51 () => console.log('complete')
57 componentWillUnmount() {
58 this.subscription.unsubscribe();
65 <h5><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
68 {this.state.posts.length > 0
69 ? this.state.posts.map(post =>
70 <PostListing post={post} showCommunity={!this.props.communityId}/>)
71 : <div>No Listings. {!this.props.communityId && <span>Subscribe to some <Link to="/communities">forums</Link>.</span>}</div>
82 <div className="mb-2">
83 <select value={this.state.sortType} onChange={linkEvent(this, this.handleSortChange)} class="custom-select w-auto">
84 <option disabled>Sort Type</option>
85 <option value={SortType.Hot}>Hot</option>
86 <option value={SortType.New}>New</option>
87 <option disabled>──────────</option>
88 <option value={SortType.TopDay}>Top Day</option>
89 <option value={SortType.TopWeek}>Week</option>
90 <option value={SortType.TopMonth}>Month</option>
91 <option value={SortType.TopYear}>Year</option>
92 <option value={SortType.TopAll}>All</option>
94 {!this.props.communityId &&
95 UserService.Instance.user &&
96 <select value={this.state.type_} onChange={linkEvent(this, this.handleTypeChange)} class="ml-2 custom-select w-auto">
97 <option disabled>Type</option>
98 <option value={ListingType.All}>All</option>
99 <option value={ListingType.Subscribed}>Subscribed</option>
110 {this.state.page > 1 &&
111 <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
113 <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
118 nextPage(i: PostListings) {
124 prevPage(i: PostListings) {
130 handleSortChange(i: PostListings, event: any) {
131 i.state.sortType = Number(event.target.value);
138 let getPostsForm: GetPostsForm = {
139 community_id: this.props.communityId,
140 page: this.state.page,
142 sort: SortType[this.state.sortType],
143 type_: ListingType[this.state.type_]
145 WebSocketService.Instance.getPosts(getPostsForm);
148 handleTypeChange(i: PostListings, event: any) {
149 i.state.type_ = Number(event.target.value);
151 if (i.state.type_ == ListingType.All) {
152 i.context.router.history.push('/all');
154 i.context.router.history.push('/');
160 parseMessage(msg: any) {
162 let op: UserOperation = msgOp(msg);
166 } else if (op == UserOperation.GetPosts) {
167 let res: GetPostsResponse = msg;
168 this.state.posts = res.posts;
169 this.state.loading = false;
170 this.setState(this.state);
171 } else if (op == UserOperation.CreatePostLike) {
172 let res: CreatePostLikeResponse = msg;
173 let found = this.state.posts.find(c => c.id == res.post.id);
174 found.my_vote = res.post.my_vote;
175 found.score = res.post.score;
176 found.upvotes = res.post.upvotes;
177 found.downvotes = res.post.downvotes;
178 this.setState(this.state);