]> Untitled Git - lemmy.git/blob - ui/src/components/create-community.tsx
Adding forum / community page
[lemmy.git] / ui / src / components / create-community.tsx
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';
7
8 import { Community } from '../interfaces';
9
10 interface State {
11   communityForm: CommunityForm;
12   categories: Array<Category>;
13 }
14
15 export class CreateCommunity extends Component<any, State> {
16   private subscription: Subscription;
17
18   private emptyState: State = {
19     communityForm: {
20       name: null,
21       title: null,
22       category_id: null
23     },
24     categories: []
25   }
26
27   constructor(props, context) {
28     super(props, context);
29
30     this.state = this.emptyState;
31
32     this.subscription = WebSocketService.Instance.subject
33       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
34       .subscribe(
35         (msg) => this.parseMessage(msg),
36         (err) => console.error(err),
37         () => console.log("complete")
38       );
39
40     WebSocketService.Instance.listCategories();
41   }
42
43   componentWillUnmount() {
44     this.subscription.unsubscribe();
45   }
46
47   render() {
48     return (
49       <div class="container">
50         <div class="row">
51           <div class="col-12 col-lg-6 mb-4">
52             {this.communityForm()}
53           </div>
54         </div>
55       </div>
56     )
57   }
58
59   communityForm() {
60     return (
61       <div>
62         <form onSubmit={linkEvent(this, this.handleCreateCommunitySubmit)}>
63           <h3>Create Forum</h3>
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} pattern="[a-z0-9_]+" title="lowercase, underscores, and no spaces."/>
68             </div>
69           </div>
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} />
74             </div>
75           </div>
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} />
80             </div>
81           </div>
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>
88                 )}
89               </select>
90             </div>
91           </div>
92           <div class="form-group row">
93             <div class="col-sm-10">
94               <button type="submit" class="btn btn-secondary">Create</button>
95             </div>
96           </div>
97         </form>
98       </div>
99     );
100   }
101
102   handleCreateCommunitySubmit(i: CreateCommunity, event) {
103     event.preventDefault();
104     WebSocketService.Instance.createCommunity(i.state.communityForm);
105   }
106
107   handleCommunityNameChange(i: CreateCommunity, event) {
108     i.state.communityForm.name = event.target.value;
109     i.setState(i.state);
110   }
111
112   handleCommunityTitleChange(i: CreateCommunity, event) {
113     i.state.communityForm.title = event.target.value;
114     i.setState(i.state);
115   }
116
117   handleCommunityDescriptionChange(i: CreateCommunity, event) {
118     i.state.communityForm.description = event.target.value;
119     i.setState(i.state);
120   }
121
122   handleCommunityCategoryChange(i: CreateCommunity, event) {
123     i.state.communityForm.category_id = Number(event.target.value);
124     i.setState(i.state);
125   }
126
127   parseMessage(msg: any) {
128     let op: UserOperation = msgOp(msg);
129     console.log(msg);
130     if (msg.error) {
131       alert(msg.error);
132       return;
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}`);
141     }
142   }
143
144 }