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} />
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 (res.op == UserOperation.GetCommunity) {
258 let data = res.data as GetCommunityResponse;
259 this.state.community = data.community;
260 this.state.moderators = data.moderators;
261 this.state.admins = data.admins;
262 this.state.online = data.online;
263 document.title = `/c/${this.state.community.name} - ${WebSocketService.Instance.site.name}`;
264 this.setState(this.state);
266 } else if (res.op == UserOperation.EditCommunity) {
267 let data = res.data as CommunityResponse;
268 this.state.community = data.community;
269 this.setState(this.state);
270 } else if (res.op == UserOperation.FollowCommunity) {
271 let data = res.data as CommunityResponse;
272 this.state.community.subscribed = data.community.subscribed;
273 this.state.community.number_of_subscribers =
274 data.community.number_of_subscribers;
275 this.setState(this.state);
276 } else if (res.op == UserOperation.GetPosts) {
277 let data = res.data as GetPostsResponse;
278 this.state.posts = data.posts;
279 this.state.loading = false;
280 this.setState(this.state);
281 } else if (res.op == UserOperation.EditPost) {
282 let data = res.data as PostResponse;
283 let found = this.state.posts.find(c => c.id == data.post.id);
285 found.url = data.post.url;
286 found.name = data.post.name;
287 found.nsfw = data.post.nsfw;
289 this.setState(this.state);
290 } else if (res.op == UserOperation.CreatePost) {
291 let data = res.data as PostResponse;
292 this.state.posts.unshift(data.post);
293 this.setState(this.state);
294 } else if (res.op == UserOperation.CreatePostLike) {
295 let data = res.data as PostResponse;
296 let found = this.state.posts.find(c => c.id == data.post.id);
298 found.score = data.post.score;
299 found.upvotes = data.post.upvotes;
300 found.downvotes = data.post.downvotes;
301 if (data.post.my_vote !== null) {
302 found.my_vote = data.post.my_vote;
303 found.upvoteLoading = false;
304 found.downvoteLoading = false;
307 this.setState(this.state);
308 } else if (res.op == UserOperation.AddModToCommunity) {
309 let data = res.data as AddModToCommunityResponse;
310 this.state.moderators = data.moderators;
311 this.setState(this.state);
312 } else if (res.op == UserOperation.BanFromCommunity) {
313 let data = res.data as BanFromCommunityResponse;
316 .filter(p => p.creator_id == data.user.id)
317 .forEach(p => (p.banned = data.banned));
319 this.setState(this.state);