1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { CommunityForm as CommunityFormI, UserOperation, Category, ListCategoriesResponse, CommunityResponse } from '../interfaces';
5 import { WebSocketService } from '../services';
6 import { msgOp } from '../utils';
7 import * as autosize from 'autosize';
9 import { Community } from '../interfaces';
11 interface CommunityFormProps {
12 community?: Community; // If a community is given, that means this is an edit
14 onCreate?(community: Community): any;
15 onEdit?(community: Community): any;
18 interface CommunityFormState {
19 communityForm: CommunityFormI;
20 categories: Array<Category>;
24 export class CommunityForm extends Component<CommunityFormProps, CommunityFormState> {
25 private subscription: Subscription;
27 private emptyState: CommunityFormState = {
37 constructor(props: any, context: any) {
38 super(props, context);
40 this.state = this.emptyState;
42 if (this.props.community) {
43 this.state.communityForm = {
44 name: this.props.community.name,
45 title: this.props.community.title,
46 category_id: this.props.community.category_id,
47 description: this.props.community.description,
48 edit_id: this.props.community.id,
53 this.subscription = WebSocketService.Instance.subject
54 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
56 (msg) => this.parseMessage(msg),
57 (err) => console.error(err),
58 () => console.log("complete")
61 WebSocketService.Instance.listCategories();
65 autosize(document.querySelectorAll('textarea'));
68 componentWillUnmount() {
69 this.subscription.unsubscribe();
75 <form onSubmit={linkEvent(this, this.handleCreateCommunitySubmit)}>
76 <div class="form-group row">
77 <label class="col-12 col-form-label">Name</label>
79 <input type="text" class="form-control" value={this.state.communityForm.name} onInput={linkEvent(this, this.handleCommunityNameChange)} required minLength={3} maxLength={20} pattern="[a-z0-9_]+" title="lowercase, underscores, and no spaces."/>
82 <div class="form-group row">
83 <label class="col-12 col-form-label">Title</label>
85 <input type="text" value={this.state.communityForm.title} onInput={linkEvent(this, this.handleCommunityTitleChange)} class="form-control" required minLength={3} maxLength={100} />
88 <div class="form-group row">
89 <label class="col-12 col-form-label">Sidebar</label>
91 <textarea value={this.state.communityForm.description} onInput={linkEvent(this, this.handleCommunityDescriptionChange)} class="form-control" rows={3} maxLength={10000} />
94 <div class="form-group row">
95 <label class="col-12 col-form-label">Category</label>
97 <select class="form-control" value={this.state.communityForm.category_id} onInput={linkEvent(this, this.handleCommunityCategoryChange)}>
98 {this.state.categories.map(category =>
99 <option value={category.id}>{category.name}</option>
104 <div class="form-group row">
106 <button type="submit" class="btn btn-secondary mr-2">
107 {this.state.loading ?
108 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
109 this.props.community ? 'Save' : 'Create'}</button>
110 {this.props.community && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}>Cancel</button>}
117 handleCreateCommunitySubmit(i: CommunityForm, event: any) {
118 event.preventDefault();
119 i.state.loading = true;
120 if (i.props.community) {
121 WebSocketService.Instance.editCommunity(i.state.communityForm);
123 WebSocketService.Instance.createCommunity(i.state.communityForm);
128 handleCommunityNameChange(i: CommunityForm, event: any) {
129 i.state.communityForm.name = event.target.value;
133 handleCommunityTitleChange(i: CommunityForm, event: any) {
134 i.state.communityForm.title = event.target.value;
138 handleCommunityDescriptionChange(i: CommunityForm, event: any) {
139 i.state.communityForm.description = event.target.value;
143 handleCommunityCategoryChange(i: CommunityForm, event: any) {
144 i.state.communityForm.category_id = Number(event.target.value);
148 handleCancel(i: CommunityForm) {
152 parseMessage(msg: any) {
153 let op: UserOperation = msgOp(msg);
157 this.state.loading = false;
158 this.setState(this.state);
160 } else if (op == UserOperation.ListCategories){
161 let res: ListCategoriesResponse = msg;
162 this.state.categories = res.categories;
163 if (!this.props.community) {
164 this.state.communityForm.category_id = res.categories[0].id;
166 this.setState(this.state);
167 } else if (op == UserOperation.CreateCommunity) {
168 let res: CommunityResponse = msg;
169 this.state.loading = false;
170 this.props.onCreate(res.community);
173 // TODO is this necessary?
174 else if (op == UserOperation.EditCommunity) {
175 let res: CommunityResponse = msg;
176 this.state.loading = false;
177 this.props.onEdit(res.community);