2019-04-08 05:19:02 +00:00
|
|
|
import { Component } from 'inferno';
|
2020-07-08 05:02:14 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-04-04 22:29:14 +00:00
|
|
|
import { CommunityForm } from './community-form';
|
2020-07-08 05:02:14 +00:00
|
|
|
import {
|
|
|
|
Community,
|
|
|
|
UserOperation,
|
|
|
|
WebSocketJsonResponse,
|
|
|
|
GetSiteResponse,
|
|
|
|
} from '../interfaces';
|
|
|
|
import { toast, wsJsonToRes } from '../utils';
|
2020-07-10 00:03:47 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
2019-03-23 01:42:57 +00:00
|
|
|
|
2020-07-08 05:02:14 +00:00
|
|
|
interface CreateCommunityState {
|
|
|
|
enableNsfw: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CreateCommunity extends Component<any, CreateCommunityState> {
|
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: CreateCommunityState = {
|
|
|
|
enableNsfw: null,
|
|
|
|
};
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-23 01:42:57 +00:00
|
|
|
super(props, context);
|
2019-04-04 22:29:14 +00:00
|
|
|
this.handleCommunityCreate = this.handleCommunityCreate.bind(this);
|
2020-07-08 05:02:14 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2020-07-10 00:03:47 +00:00
|
|
|
if (!UserService.Instance.user) {
|
|
|
|
toast(i18n.t('not_logged_in'), 'danger');
|
|
|
|
this.context.router.history.push(`/login`);
|
|
|
|
}
|
|
|
|
|
2020-07-08 05:02:14 +00:00
|
|
|
this.subscription = WebSocketService.Instance.subject
|
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
|
|
|
() => console.log('complete')
|
|
|
|
);
|
|
|
|
|
|
|
|
WebSocketService.Instance.getSite();
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 05:02:14 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
2019-04-22 16:24:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
2019-07-29 01:57:09 +00:00
|
|
|
<div class="col-12 col-lg-6 offset-lg-3 mb-4">
|
2020-02-02 18:50:44 +00:00
|
|
|
<h5>{i18n.t('create_community')}</h5>
|
2020-07-08 05:02:14 +00:00
|
|
|
<CommunityForm
|
|
|
|
onCreate={this.handleCommunityCreate}
|
|
|
|
enableNsfw={this.state.enableNsfw}
|
|
|
|
/>
|
2019-03-23 01:42:57 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 21:52:18 +00:00
|
|
|
handleCommunityCreate(community: Community) {
|
2019-04-29 02:05:11 +00:00
|
|
|
this.props.history.push(`/c/${community.name}`);
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2020-07-08 05:02:14 +00:00
|
|
|
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
|
|
console.log(msg);
|
|
|
|
let res = wsJsonToRes(msg);
|
|
|
|
if (msg.error) {
|
2020-07-15 13:55:38 +00:00
|
|
|
// Toast errors are already handled by community-form
|
2020-07-08 05:02:14 +00:00
|
|
|
return;
|
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
|
|
|
this.state.enableNsfw = data.site.enable_nsfw;
|
|
|
|
this.setState(this.state);
|
|
|
|
document.title = `${i18n.t('create_community')} - ${data.site.name}`;
|
|
|
|
}
|
|
|
|
}
|
2019-04-04 22:29:14 +00:00
|
|
|
}
|