1 import { Component, linkEvent } from 'inferno';
2 import { Subscription } from "rxjs";
3 import { retryWhen, delay, take } from 'rxjs/operators';
4 import { CommunityForm, UserOperation, Category, ListCategoriesResponse } from '../interfaces';
5 import { WebSocketService, UserService } from '../services';
6 import { msgOp } from '../utils';
8 import { Community } from '../interfaces';
11 communityForm: CommunityForm;
12 categories: Array<Category>;
15 export class CreateCommunity extends Component<any, State> {
16 private subscription: Subscription;
18 private emptyState: State = {
27 constructor(props, context) {
28 super(props, context);
30 this.state = this.emptyState;
32 this.subscription = WebSocketService.Instance.subject
33 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
35 (msg) => this.parseMessage(msg),
36 (err) => console.error(err),
37 () => console.log("complete")
40 WebSocketService.Instance.listCategories();
43 componentWillUnmount() {
44 this.subscription.unsubscribe();
49 <div class="container">
51 <div class="col-12 col-lg-6 mb-4">
52 {this.communityForm()}
62 <form onSubmit={linkEvent(this, this.handleCreateCommunitySubmit)}>
64 <div class="form-group row">
65 <label class="col-sm-2 col-form-label">Name</label>
66 <div class="col-sm-10">
67 <input type="text" class="form-control" value={this.state.communityForm.name} onInput={linkEvent(this, this.handleCommunityNameChange)} required minLength={3} />
70 <div class="form-group row">
71 <label class="col-sm-2 col-form-label">Title / Headline</label>
72 <div class="col-sm-10">
73 <input type="text" value={this.state.communityForm.title} onInput={linkEvent(this, this.handleCommunityTitleChange)} class="form-control" required minLength={3} />
76 <div class="form-group row">
77 <label class="col-sm-2 col-form-label">Description / Sidebar</label>
78 <div class="col-sm-10">
79 <textarea value={this.state.communityForm.description} onInput={linkEvent(this, this.handleCommunityDescriptionChange)} class="form-control" rows={6} />
82 <div class="form-group row">
83 <label class="col-sm-2 col-form-label">Category</label>
84 <div class="col-sm-10">
85 <select class="form-control" value={this.state.communityForm.category_id} onInput={linkEvent(this, this.handleCommunityCategoryChange)}>
86 {this.state.categories.map(category =>
87 <option value={category.id}>{category.name}</option>
92 <div class="form-group row">
93 <div class="col-sm-10">
94 <button type="submit" class="btn btn-secondary">Create</button>
102 handleCreateCommunitySubmit(i: CreateCommunity, event) {
103 event.preventDefault();
104 WebSocketService.Instance.createCommunity(i.state.communityForm);
107 handleCommunityNameChange(i: CreateCommunity, event) {
108 i.state.communityForm.name = event.target.value;
112 handleCommunityTitleChange(i: CreateCommunity, event) {
113 i.state.communityForm.title = event.target.value;
117 handleCommunityDescriptionChange(i: CreateCommunity, event) {
118 i.state.communityForm.description = event.target.value;
122 handleCommunityCategoryChange(i: CreateCommunity, event) {
123 i.state.communityForm.category_id = Number(event.target.value);
127 parseMessage(msg: any) {
128 let op: UserOperation = msgOp(msg);
133 } else if (op == UserOperation.ListCategories){
134 let res: ListCategoriesResponse = msg;
135 this.state.categories = res.categories;
136 this.state.communityForm.category_id = res.categories[0].id;
137 this.setState(this.state);
138 } else if (op == UserOperation.CreateCommunity) {
139 let community: Community = msg.community;
140 this.props.history.push(`/community/${community.id}`);