]> Untitled Git - lemmy.git/blob - ui/src/components/post-form.tsx
Adding a debounce.
[lemmy.git] / ui / src / components / post-form.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { PostListings } from './post-listings';
3 import { Subscription } from "rxjs";
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { msgOp, getPageTitle, debounce } from '../utils';
8 import * as autosize from 'autosize';
9
10 interface PostFormProps {
11   post?: Post; // If a post is given, that means this is an edit
12   prevCommunityName?: string;
13   onCancel?(): any;
14   onCreate?(id: number): any;
15   onEdit?(post: Post): any;
16 }
17
18 interface PostFormState {
19   postForm: PostFormI;
20   communities: Array<Community>;
21   loading: boolean;
22   suggestedTitle: string;
23   suggestedPosts: Array<Post>;
24 }
25
26 export class PostForm extends Component<PostFormProps, PostFormState> {
27
28   private subscription: Subscription;
29   private emptyState: PostFormState = {
30     postForm: {
31       name: null,
32       auth: null,
33       community_id: null,
34       creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null,
35     },
36     communities: [],
37     loading: false,
38     suggestedTitle: undefined,
39     suggestedPosts: [],
40   }
41
42   constructor(props: any, context: any) {
43     super(props, context);
44
45     this.state = this.emptyState;
46
47     if (this.props.post) {
48       this.state.postForm = {
49         body: this.props.post.body,
50         name: this.props.post.name,
51         community_id: this.props.post.community_id,
52         edit_id: this.props.post.id,
53         creator_id: this.props.post.creator_id,
54         url: this.props.post.url,
55         auth: null
56       }
57     }
58
59     this.subscription = WebSocketService.Instance.subject
60       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
61       .subscribe(
62         (msg) => this.parseMessage(msg),
63         (err) => console.error(err),
64         () => console.log('complete')
65       );
66
67       let listCommunitiesForm: ListCommunitiesForm = {
68         sort: SortType[SortType.TopAll],
69         limit: 9999,
70       }
71
72       WebSocketService.Instance.listCommunities(listCommunitiesForm);
73   }
74
75   componentDidMount() {
76     autosize(document.querySelectorAll('textarea'));
77   }
78
79   componentWillUnmount() {
80     this.subscription.unsubscribe();
81   }
82
83   render() {
84     return (
85       <div>
86         <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
87           <div class="form-group row">
88             <label class="col-sm-2 col-form-label">URL</label>
89             <div class="col-sm-10">
90               <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, debounce(this.handlePostUrlChange))} />
91               {this.state.suggestedTitle && 
92                 <div class="mt-1 text-muted small font-weight-bold pointer" onClick={linkEvent(this, this.copySuggestedTitle)}>copy suggested title: {this.state.suggestedTitle}</div>
93               }
94             </div>
95           </div>
96           <div class="form-group row">
97             <label class="col-sm-2 col-form-label">Title</label>
98             <div class="col-sm-10">
99               <textarea value={this.state.postForm.name} onInput={linkEvent(this, debounce(this.handlePostNameChange))} class="form-control" required rows={2} minLength={3} maxLength={100} />
100               {this.state.suggestedPosts.length > 0 && 
101                 <>
102                   <div class="my-1 text-muted small font-weight-bold">These posts might be related</div>
103                   <PostListings posts={this.state.suggestedPosts} />
104                 </>
105               }
106             </div>
107           </div>
108           <div class="form-group row">
109             <label class="col-sm-2 col-form-label">Body</label>
110             <div class="col-sm-10">
111               <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} class="form-control" rows={4} maxLength={10000} />
112             </div>
113           </div>
114           {/* Cant change a community from an edit */}
115           {!this.props.post &&
116             <div class="form-group row">
117             <label class="col-sm-2 col-form-label">Community</label>
118             <div class="col-sm-10">
119               <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
120                 {this.state.communities.map(community =>
121                   <option value={community.id}>{community.name}</option>
122                 )}
123               </select>
124             </div>
125             </div>
126             }
127           <div class="form-group row">
128             <div class="col-sm-10">
129               <button type="submit" class="btn btn-secondary mr-2">
130               {this.state.loading ? 
131               <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> : 
132               this.props.post ? 'Save' : 'Create'}</button>
133               {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}>Cancel</button>}
134             </div>
135           </div>
136         </form>
137       </div>
138     );
139   }
140
141   handlePostSubmit(i: PostForm, event: any) {
142     event.preventDefault();
143     if (i.props.post) {
144       WebSocketService.Instance.editPost(i.state.postForm);
145     } else {
146       WebSocketService.Instance.createPost(i.state.postForm);
147     }
148     i.state.loading = true;
149     i.setState(i.state);
150   }
151
152   copySuggestedTitle(i: PostForm) {
153     i.state.postForm.name = i.state.suggestedTitle;
154     i.state.suggestedTitle = undefined;
155     i.setState(i.state);
156   }
157
158   handlePostUrlChange(i: PostForm, event: any) {
159     i.state.postForm.url = event.target.value;
160     getPageTitle(i.state.postForm.url).then(d => {
161       i.state.suggestedTitle = d;
162       i.setState(i.state);
163     });
164     i.setState(i.state);
165   }
166
167   handlePostNameChange(i: PostForm, event: any) {
168     i.state.postForm.name = event.target.value;
169     let form: SearchForm = {
170       q: i.state.postForm.name,
171       type_: SearchType[SearchType.Posts],
172       sort: SortType[SortType.TopAll],
173       community_id: i.state.postForm.community_id,
174       page: 1,
175       limit: 6,
176     };
177
178     if (i.state.postForm.name !== '') {
179       WebSocketService.Instance.search(form);
180     } else {
181       i.state.suggestedPosts = [];
182     }
183
184     i.setState(i.state);
185   }
186
187   handlePostBodyChange(i: PostForm, event: any) {
188     i.state.postForm.body = event.target.value;
189     i.setState(i.state);
190   }
191
192   handlePostCommunityChange(i: PostForm, event: any) {
193     i.state.postForm.community_id = Number(event.target.value);
194     i.setState(i.state);
195   }
196
197   handleCancel(i: PostForm) {
198     i.props.onCancel();
199   }
200
201   parseMessage(msg: any) {
202     let op: UserOperation = msgOp(msg);
203     if (msg.error) {
204       alert(msg.error);
205       this.state.loading = false;
206       this.setState(this.state);
207       return;
208     } else if (op == UserOperation.ListCommunities) {
209       let res: ListCommunitiesResponse = msg;
210       this.state.communities = res.communities;
211       if (this.props.post) {
212         this.state.postForm.community_id = this.props.post.community_id;
213       } else if (this.props.prevCommunityName) {
214         let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
215         this.state.postForm.community_id = foundCommunityId;
216       } else {
217         this.state.postForm.community_id = res.communities[0].id;
218       }
219       this.setState(this.state);
220     } else if (op == UserOperation.CreatePost) {
221       this.state.loading = false;
222       let res: PostResponse = msg;
223       this.props.onCreate(res.post.id);
224     } else if (op == UserOperation.EditPost) {
225       this.state.loading = false;
226       let res: PostResponse = msg;
227       this.props.onEdit(res.post);
228     } else if (op == UserOperation.Search) {
229       let res: SearchResponse = msg;
230       this.state.suggestedPosts = res.posts;
231       this.setState(this.state);
232     }
233   }
234
235 }
236
237