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