]> Untitled Git - lemmy.git/blob - ui/src/components/post-listings.tsx
Adding post and user details paging.
[lemmy.git] / ui / src / components / post-listings.tsx
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';
9
10 interface PostListingsProps {
11   communityId?: number;
12 }
13
14 interface PostListingsState {
15   community: CommunityI;
16   moderators: Array<CommunityUser>;
17   posts: Array<Post>;
18   sortType: SortType;
19   type_: ListingType;
20   page: number;
21   loading: boolean;
22 }
23
24 export class PostListings extends Component<PostListingsProps, PostListingsState> {
25
26   private subscription: Subscription;
27   private emptyState: PostListingsState = {
28     community: {
29       id: null,
30       name: null,
31       title: null,
32       category_id: null,
33       category_name: null,
34       creator_id: null,
35       creator_name: null,
36       number_of_subscribers: null,
37       number_of_posts: null,
38       number_of_comments: null,
39       published: null
40     },
41     moderators: [],
42     posts: [],
43     sortType: SortType.Hot,
44     type_: this.props.communityId 
45     ? ListingType.Community 
46     : UserService.Instance.user
47     ? ListingType.Subscribed 
48     : ListingType.All,
49     page: 1,
50     loading: true
51   }
52
53   constructor(props: any, context: any) {
54     super(props, context);
55
56
57     this.state = this.emptyState;
58
59     this.subscription = WebSocketService.Instance.subject
60       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
61       .subscribe(
62         (msg) => this.parseMessage(msg),
63         (err) => console.error(err),
64         () => console.log('complete')
65       );
66
67       this.refetch();
68   }
69
70   componentWillUnmount() {
71     this.subscription.unsubscribe();
72   }
73
74   render() {
75     return (
76       <div>
77         {this.state.loading ? 
78         <h4><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> : 
79         <div>
80           {this.selects()}
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>
85           }
86           {this.paginator()}
87         </div>
88         }
89       </div>
90     )
91   }
92
93   selects() {
94     return (
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>
106         </select>
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>
113             </select>
114
115         }
116       </div>
117     )
118   }
119
120   paginator() {
121     return (
122       <div class="mt-2">
123         {this.state.page > 1 && 
124           <button class="btn btn-sm btn-secondary mr-1" onClick={linkEvent(this, this.prevPage)}>Prev</button>
125         }
126         <button class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.nextPage)}>Next</button>
127       </div>
128     );
129   }
130
131   nextPage(i: PostListings) { 
132     i.state.page++;
133     i.setState(i.state);
134     i.refetch();
135   }
136
137   prevPage(i: PostListings) { 
138     i.state.page--;
139     i.setState(i.state);
140     i.refetch();
141   }
142
143   handleSortChange(i: PostListings, event: any) {
144     i.state.sortType = Number(event.target.value);
145     i.state.page = 1;
146     i.setState(i.state);
147     i.refetch();
148   }
149
150   refetch() {
151     let getPostsForm: GetPostsForm = {
152       community_id: this.state.community.id,
153       page: this.state.page,
154       limit: fetchLimit,
155       sort: SortType[this.state.sortType],
156       type_: ListingType[ListingType.Community]
157     }
158     WebSocketService.Instance.getPosts(getPostsForm);
159   }
160
161   handleTypeChange(i: PostListings, event: any) {
162     i.state.type_ = Number(event.target.value);
163     i.state.page = 1;
164     i.setState(i.state);
165     i.refetch();
166   }
167
168   parseMessage(msg: any) {
169     console.log(msg);
170     let op: UserOperation = msgOp(msg);
171     if (msg.error) {
172       alert(msg.error);
173       return;
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);
187     }
188   }
189 }
190
191