]> Untitled Git - lemmy.git/blob - ui/src/components/create-community.tsx
Adding a few endpoints.
[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 } 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 }
13
14 export class CreateCommunity extends Component<any, State> {
15   private subscription: Subscription;
16
17   private emptyState: State = {
18     communityForm: {
19       name: null,
20     }
21   }
22
23   constructor(props, context) {
24     super(props, context);
25
26     this.state = this.emptyState;
27     
28     this.subscription = WebSocketService.Instance.subject
29       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
30       .subscribe(
31         (msg) => this.parseMessage(msg),
32         (err) => console.error(err),
33         () => console.log("complete")
34       );
35   }
36
37   componentWillUnmount() {
38     this.subscription.unsubscribe();
39   }
40
41   render() {
42     return (
43       <div class="container">
44         <div class="row">
45           <div class="col-12 col-lg-6 mb-4">
46             {this.communityForm()}
47           </div>
48         </div>
49       </div>
50     )
51   }
52
53   communityForm() {
54     return (
55       <div>
56         <form onSubmit={linkEvent(this, this.handleCreateCommunitySubmit)}>
57           <h3>Create Forum</h3>
58           <div class="form-group row">
59             <label class="col-sm-2 col-form-label">Name</label>
60             <div class="col-sm-10">
61               <input type="text" class="form-control" value={this.state.communityForm.name} onInput={linkEvent(this, this.handleCommunityNameChange)} required minLength={3} />
62             </div>
63           </div>
64           <div class="form-group row">
65             <div class="col-sm-10">
66               <button type="submit" class="btn btn-secondary">Create</button>
67             </div>
68           </div>
69         </form>
70       </div>
71     );
72   }
73   
74   handleCreateCommunitySubmit(i: CreateCommunity, event) {
75     event.preventDefault();
76     WebSocketService.Instance.createCommunity(i.state.communityForm);
77   }
78
79   handleCommunityNameChange(i: CreateCommunity, event) {
80     i.state.communityForm.name = event.target.value;
81     i.setState(i.state);
82   }
83
84   parseMessage(msg: any) {
85     let op: UserOperation = msgOp(msg);
86     console.log(msg);
87     if (msg.error) {
88       alert(msg.error);
89       return;
90     } else {
91       if (op == UserOperation.CreateCommunity) {
92         let community: Community = msg.community;
93         this.props.history.push(`/community/${community.id}`);
94       }
95     }
96   }
97
98 }