1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
6 Community as CommunityI,
18 AddModToCommunityResponse,
19 BanFromCommunityResponse,
20 WebSocketJsonResponse,
21 } from '../interfaces';
22 import { WebSocketService, UserService } from '../services';
23 import { PostListings } from './post-listings';
24 import { SortSelect } from './sort-select';
25 import { Sidebar } from './sidebar';
26 import { wsJsonToRes, routeSortTypeToEnum, fetchLimit, toast } from '../utils';
27 import { i18n } from '../i18next';
30 community: CommunityI;
32 communityName: string;
33 moderators: Array<CommunityUser>;
34 admins: Array<UserView>;
42 export class Community extends Component<any, State> {
43 private subscription: Subscription;
44 private emptyState: State = {
53 number_of_subscribers: null,
54 number_of_posts: null,
55 number_of_comments: null,
63 communityId: Number(this.props.match.params.id),
64 communityName: this.props.match.params.name,
68 sort: this.getSortTypeFromProps(this.props),
69 page: this.getPageFromProps(this.props),
72 getSortTypeFromProps(props: any): SortType {
73 return props.match.params.sort
74 ? routeSortTypeToEnum(props.match.params.sort)
75 : UserService.Instance.user
76 ? UserService.Instance.user.default_sort_type
80 getPageFromProps(props: any): number {
81 return props.match.params.page ? Number(props.match.params.page) : 1;
84 constructor(props: any, context: any) {
85 super(props, context);
87 this.state = this.emptyState;
88 this.handleSortChange = this.handleSortChange.bind(this);
90 this.subscription = WebSocketService.Instance.subject
91 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
93 msg => this.parseMessage(msg),
94 err => console.error(err),
95 () => console.log('complete')
98 let form: GetCommunityForm = {
99 id: this.state.communityId ? this.state.communityId : null,
100 name: this.state.communityName ? this.state.communityName : null,
102 WebSocketService.Instance.getCommunity(form);
105 componentWillUnmount() {
106 this.subscription.unsubscribe();
109 // Necessary for back button for some reason
110 componentWillReceiveProps(nextProps: any) {
112 nextProps.history.action == 'POP' ||
113 nextProps.history.action == 'PUSH'
115 this.state.sort = this.getSortTypeFromProps(nextProps);
116 this.state.page = this.getPageFromProps(nextProps);
117 this.setState(this.state);
124 <div class="container">
125 {this.state.loading ? (
127 <svg class="icon icon-spinner spin">
128 <use xlinkHref="#icon-spinner"></use>
133 <div class="col-12 col-md-8">
135 {this.state.community.title}
136 {this.state.community.removed && (
137 <small className="ml-2 text-muted font-italic">
141 {this.state.community.nsfw && (
142 <small className="ml-2 text-muted font-italic">
148 <PostListings posts={this.state.posts} removeDuplicates />
151 <div class="col-12 col-md-4">
153 community={this.state.community}
154 moderators={this.state.moderators}
155 admins={this.state.admins}
156 online={this.state.online}
168 <SortSelect sort={this.state.sort} onChange={this.handleSortChange} />
170 href={`/feeds/c/${this.state.communityName}.xml?sort=${
171 SortType[this.state.sort]
175 <svg class="icon mx-2 text-muted small">
176 <use xlinkHref="#icon-rss">#</use>
186 {this.state.page > 1 && (
188 class="btn btn-sm btn-secondary mr-1"
189 onClick={linkEvent(this, this.prevPage)}
194 {this.state.posts.length == fetchLimit && (
196 class="btn btn-sm btn-secondary"
197 onClick={linkEvent(this, this.nextPage)}
206 nextPage(i: Community) {
211 window.scrollTo(0, 0);
214 prevPage(i: Community) {
219 window.scrollTo(0, 0);
222 handleSortChange(val: SortType) {
223 this.state.sort = val;
225 this.state.loading = true;
226 this.setState(this.state);
229 window.scrollTo(0, 0);
233 let sortStr = SortType[this.state.sort].toLowerCase();
234 this.props.history.push(
235 `/c/${this.state.community.name}/sort/${sortStr}/page/${this.state.page}`
240 let getPostsForm: GetPostsForm = {
241 page: this.state.page,
243 sort: SortType[this.state.sort],
244 type_: ListingType[ListingType.Community],
245 community_id: this.state.community.id,
247 WebSocketService.Instance.getPosts(getPostsForm);
250 parseMessage(msg: WebSocketJsonResponse) {
252 let res = wsJsonToRes(msg);
254 toast(i18n.t(msg.error), 'danger');
255 this.context.router.history.push('/');
257 } else if (msg.reconnect) {
259 } else if (res.op == UserOperation.GetCommunity) {
260 let data = res.data as GetCommunityResponse;
261 this.state.community = data.community;
262 this.state.moderators = data.moderators;
263 this.state.admins = data.admins;
264 this.state.online = data.online;
265 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
266 this.setState(this.state);
268 } else if (res.op == UserOperation.EditCommunity) {
269 let data = res.data as CommunityResponse;
270 this.state.community = data.community;
271 this.setState(this.state);
272 } else if (res.op == UserOperation.FollowCommunity) {
273 let data = res.data as CommunityResponse;
274 this.state.community.subscribed = data.community.subscribed;
275 this.state.community.number_of_subscribers =
276 data.community.number_of_subscribers;
277 this.setState(this.state);
278 } else if (res.op == UserOperation.GetPosts) {
279 let data = res.data as GetPostsResponse;
280 this.state.posts = data.posts;
281 this.state.loading = false;
282 this.setState(this.state);
283 } else if (res.op == UserOperation.EditPost) {
284 let data = res.data as PostResponse;
285 let found = this.state.posts.find(c => c.id == data.post.id);
287 found.url = data.post.url;
288 found.name = data.post.name;
289 found.nsfw = data.post.nsfw;
291 this.setState(this.state);
292 } else if (res.op == UserOperation.CreatePost) {
293 let data = res.data as PostResponse;
294 this.state.posts.unshift(data.post);
295 this.setState(this.state);
296 } else if (res.op == UserOperation.CreatePostLike) {
297 let data = res.data as PostResponse;
298 let found = this.state.posts.find(c => c.id == data.post.id);
300 found.score = data.post.score;
301 found.upvotes = data.post.upvotes;
302 found.downvotes = data.post.downvotes;
303 if (data.post.my_vote !== null) {
304 found.my_vote = data.post.my_vote;
305 found.upvoteLoading = false;
306 found.downvoteLoading = false;
309 this.setState(this.state);
310 } else if (res.op == UserOperation.AddModToCommunity) {
311 let data = res.data as AddModToCommunityResponse;
312 this.state.moderators = data.moderators;
313 this.setState(this.state);
314 } else if (res.op == UserOperation.BanFromCommunity) {
315 let data = res.data as BanFromCommunityResponse;
318 .filter(p => p.creator_id == data.user.id)
319 .forEach(p => (p.banned = data.banned));
321 this.setState(this.state);