2019-04-08 05:19:02 +00:00
|
|
|
import { Component } from 'inferno';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { UserOperation, Community as CommunityI, GetCommunityResponse, CommunityResponse, CommunityUser} from '../interfaces';
|
|
|
|
import { WebSocketService } from '../services';
|
2019-04-05 19:14:54 +00:00
|
|
|
import { PostListings } from './post-listings';
|
2019-04-03 23:01:20 +00:00
|
|
|
import { Sidebar } from './sidebar';
|
2019-04-08 05:19:02 +00:00
|
|
|
import { msgOp } from '../utils';
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
interface State {
|
|
|
|
community: CommunityI;
|
2019-04-05 19:14:54 +00:00
|
|
|
communityId: number;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-08 21:46:09 +00:00
|
|
|
loading: boolean;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Community extends Component<any, State> {
|
|
|
|
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: State = {
|
|
|
|
community: {
|
|
|
|
id: null,
|
|
|
|
name: null,
|
2019-04-03 23:01:20 +00:00
|
|
|
title: null,
|
|
|
|
category_id: null,
|
|
|
|
category_name: null,
|
|
|
|
creator_id: null,
|
|
|
|
creator_name: null,
|
|
|
|
number_of_subscribers: null,
|
|
|
|
number_of_posts: null,
|
2019-04-04 20:53:32 +00:00
|
|
|
number_of_comments: null,
|
2019-03-26 18:00:18 +00:00
|
|
|
published: null
|
|
|
|
},
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: [],
|
2019-04-08 21:46:09 +00:00
|
|
|
communityId: Number(this.props.match.params.id),
|
|
|
|
loading: true
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-26 18:00:18 +00:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2019-04-08 21:46:09 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
(msg) => this.parseMessage(msg),
|
2019-03-26 18:00:18 +00:00
|
|
|
(err) => console.error(err),
|
|
|
|
() => console.log('complete')
|
2019-04-08 21:46:09 +00:00
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-04-05 19:14:54 +00:00
|
|
|
WebSocketService.Instance.getCommunity(this.state.communityId);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-04-08 21:46:09 +00:00
|
|
|
{this.state.loading ?
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> :
|
2019-03-26 18:00:18 +00:00
|
|
|
<div class="row">
|
2019-04-09 00:10:24 +00:00
|
|
|
<div class="col-12 col-md-9">
|
2019-04-20 04:06:25 +00:00
|
|
|
<h5>{this.state.community.title}
|
2019-04-15 23:12:06 +00:00
|
|
|
{this.state.community.removed &&
|
|
|
|
<small className="ml-2 text-muted font-italic">removed</small>
|
|
|
|
}
|
2019-04-20 04:06:25 +00:00
|
|
|
</h5>
|
2019-04-05 19:14:54 +00:00
|
|
|
<PostListings communityId={this.state.communityId} />
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-04-09 00:10:24 +00:00
|
|
|
<div class="col-12 col-md-3">
|
2019-04-04 20:53:32 +00:00
|
|
|
<Sidebar community={this.state.community} moderators={this.state.moderators} />
|
2019-04-03 06:49:32 +00:00
|
|
|
</div>
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
2019-04-08 21:46:09 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
console.log(msg);
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
alert(msg.error);
|
|
|
|
return;
|
|
|
|
} else if (op == UserOperation.GetCommunity) {
|
2019-04-04 22:29:14 +00:00
|
|
|
let res: GetCommunityResponse = msg;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.state.community = res.community;
|
2019-04-04 20:53:32 +00:00
|
|
|
this.state.moderators = res.moderators;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
2019-04-04 22:29:14 +00:00
|
|
|
} else if (op == UserOperation.EditCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community = res.community;
|
|
|
|
this.setState(this.state);
|
2019-04-05 06:26:38 +00:00
|
|
|
} else if (op == UserOperation.FollowCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community.subscribed = res.community.subscribed;
|
|
|
|
this.state.community.number_of_subscribers = res.community.number_of_subscribers;
|
|
|
|
this.setState(this.state);
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-03 06:49:32 +00:00
|
|
|
|