1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from 'rxjs';
3 import { retryWhen, delay, take } from 'rxjs/operators';
5 CommunityForm as CommunityFormI,
8 ListCategoriesResponse,
11 WebSocketJsonResponse,
12 } from '../interfaces';
13 import { WebSocketService } from '../services';
16 capitalizeFirstLetter,
21 import Tribute from 'tributejs/src/Tribute.js';
22 import autosize from 'autosize';
23 import { i18n } from '../i18next';
25 import { Community } from '../interfaces';
27 interface CommunityFormProps {
28 community?: Community; // If a community is given, that means this is an edit
30 onCreate?(community: Community): any;
31 onEdit?(community: Community): any;
34 interface CommunityFormState {
35 communityForm: CommunityFormI;
36 categories: Array<Category>;
41 export class CommunityForm extends Component<
45 private id = `community-form-${randomStr()}`;
46 private tribute: Tribute;
47 private subscription: Subscription;
49 private emptyState: CommunityFormState = {
61 constructor(props: any, context: any) {
62 super(props, context);
64 this.tribute = setupTribute();
65 this.state = this.emptyState;
67 if (this.props.community) {
68 this.state.communityForm = {
69 name: this.props.community.name,
70 title: this.props.community.title,
71 category_id: this.props.community.category_id,
72 description: this.props.community.description,
73 edit_id: this.props.community.id,
74 nsfw: this.props.community.nsfw,
79 this.subscription = WebSocketService.Instance.subject
80 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
82 msg => this.parseMessage(msg),
83 err => console.error(err),
84 () => console.log('complete')
87 WebSocketService.Instance.listCategories();
88 WebSocketService.Instance.getSite();
92 var textarea: any = document.getElementById(this.id);
94 this.tribute.attach(textarea);
95 textarea.addEventListener('tribute-replaced', () => {
96 this.state.communityForm.description = textarea.value;
97 this.setState(this.state);
98 autosize.update(textarea);
102 componentWillUnmount() {
103 this.subscription.unsubscribe();
108 <form onSubmit={linkEvent(this, this.handleCreateCommunitySubmit)}>
109 <div class="form-group row">
110 <label class="col-12 col-form-label" htmlFor="community-name">
118 value={this.state.communityForm.name}
119 onInput={linkEvent(this, this.handleCommunityNameChange)}
124 title={i18n.t('community_reqs')}
129 <div class="form-group row">
130 <label class="col-12 col-form-label" htmlFor="community-title">
137 value={this.state.communityForm.title}
138 onInput={linkEvent(this, this.handleCommunityTitleChange)}
146 <div class="form-group row">
147 <label class="col-12 col-form-label" htmlFor={this.id}>
153 value={this.state.communityForm.description}
154 onInput={linkEvent(this, this.handleCommunityDescriptionChange)}
161 <div class="form-group row">
162 <label class="col-12 col-form-label" htmlFor="community-category">
168 id="community-category"
169 value={this.state.communityForm.category_id}
170 onInput={linkEvent(this, this.handleCommunityCategoryChange)}
172 {this.state.categories.map(category => (
173 <option value={category.id}>{category.name}</option>
179 {this.state.enable_nsfw && (
180 <div class="form-group row">
182 <div class="form-check">
184 class="form-check-input"
187 checked={this.state.communityForm.nsfw}
188 onChange={linkEvent(this, this.handleCommunityNsfwChange)}
190 <label class="form-check-label" htmlFor="community-nsfw">
197 <div class="form-group row">
199 <button type="submit" class="btn btn-secondary mr-2">
200 {this.state.loading ? (
201 <svg class="icon icon-spinner spin">
202 <use xlinkHref="#icon-spinner"></use>
204 ) : this.props.community ? (
205 capitalizeFirstLetter(i18n.t('save'))
207 capitalizeFirstLetter(i18n.t('create'))
210 {this.props.community && (
213 class="btn btn-secondary"
214 onClick={linkEvent(this, this.handleCancel)}
225 handleCreateCommunitySubmit(i: CommunityForm, event: any) {
226 event.preventDefault();
227 i.state.loading = true;
228 if (i.props.community) {
229 WebSocketService.Instance.editCommunity(i.state.communityForm);
231 WebSocketService.Instance.createCommunity(i.state.communityForm);
236 handleCommunityNameChange(i: CommunityForm, event: any) {
237 i.state.communityForm.name = event.target.value;
241 handleCommunityTitleChange(i: CommunityForm, event: any) {
242 i.state.communityForm.title = event.target.value;
246 handleCommunityDescriptionChange(i: CommunityForm, event: any) {
247 i.state.communityForm.description = event.target.value;
251 handleCommunityCategoryChange(i: CommunityForm, event: any) {
252 i.state.communityForm.category_id = Number(event.target.value);
256 handleCommunityNsfwChange(i: CommunityForm, event: any) {
257 i.state.communityForm.nsfw = event.target.checked;
261 handleCancel(i: CommunityForm) {
265 parseMessage(msg: WebSocketJsonResponse) {
266 let res = wsJsonToRes(msg);
269 toast(i18n.t(msg.error), 'danger');
270 this.state.loading = false;
271 this.setState(this.state);
273 } else if (res.op == UserOperation.ListCategories) {
274 let data = res.data as ListCategoriesResponse;
275 this.state.categories = data.categories;
276 if (!this.props.community) {
277 this.state.communityForm.category_id = data.categories[0].id;
279 this.setState(this.state);
280 } else if (res.op == UserOperation.CreateCommunity) {
281 let data = res.data as CommunityResponse;
282 this.state.loading = false;
283 this.props.onCreate(data.community);
285 // TODO is this necessary
286 else if (res.op == UserOperation.EditCommunity) {
287 let data = res.data as CommunityResponse;
288 this.state.loading = false;
289 this.props.onEdit(data.community);
290 } else if (res.op == UserOperation.GetSite) {
291 let data = res.data as GetSiteResponse;
292 this.state.enable_nsfw = data.site.enable_nsfw;
293 this.setState(this.state);