]> Untitled Git - lemmy.git/blob - ui/src/components/post-form.tsx
Fixing suggested title filling.
[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 {
6   PostForm as PostFormI,
7   PostFormParams,
8   Post,
9   PostResponse,
10   UserOperation,
11   Community,
12   ListCommunitiesResponse,
13   ListCommunitiesForm,
14   SortType,
15   SearchForm,
16   SearchType,
17   SearchResponse,
18 } from '../interfaces';
19 import { WebSocketService, UserService } from '../services';
20 import {
21   msgOp,
22   getPageTitle,
23   validURL,
24   capitalizeFirstLetter,
25   markdownHelpUrl,
26   archiveUrl,
27   mdToHtml,
28   debounce,
29 } from '../utils';
30 import * as autosize from 'autosize';
31 import { i18n } from '../i18next';
32 import { T } from 'inferno-i18next';
33
34 interface PostFormProps {
35   post?: Post; // If a post is given, that means this is an edit
36   params?: PostFormParams;
37   onCancel?(): any;
38   onCreate?(id: number): any;
39   onEdit?(post: Post): any;
40 }
41
42 interface PostFormState {
43   postForm: PostFormI;
44   communities: Array<Community>;
45   loading: boolean;
46   imageLoading: boolean;
47   previewMode: boolean;
48   suggestedTitle: string;
49   suggestedPosts: Array<Post>;
50   crossPosts: Array<Post>;
51 }
52
53 export class PostForm extends Component<PostFormProps, PostFormState> {
54   private subscription: Subscription;
55   private emptyState: PostFormState = {
56     postForm: {
57       name: null,
58       nsfw: false,
59       auth: null,
60       community_id: null,
61       creator_id: UserService.Instance.user
62         ? UserService.Instance.user.id
63         : null,
64     },
65     communities: [],
66     loading: false,
67     imageLoading: false,
68     previewMode: false,
69     suggestedTitle: undefined,
70     suggestedPosts: [],
71     crossPosts: [],
72   };
73
74   constructor(props: any, context: any) {
75     super(props, context);
76
77     this.state = this.emptyState;
78
79     if (this.props.post) {
80       this.state.postForm = {
81         body: this.props.post.body,
82         // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
83         name: undefined,
84         community_id: this.props.post.community_id,
85         edit_id: this.props.post.id,
86         creator_id: this.props.post.creator_id,
87         url: this.props.post.url,
88         nsfw: this.props.post.nsfw,
89         auth: null,
90       };
91     }
92
93     if (this.props.params) {
94       this.state.postForm.name = this.props.params.name;
95       if (this.props.params.url) {
96         this.state.postForm.url = this.props.params.url;
97       }
98       if (this.props.params.body) {
99         this.state.postForm.body = this.props.params.body;
100       }
101     }
102
103     this.subscription = WebSocketService.Instance.subject
104       .pipe(
105         retryWhen(errors =>
106           errors.pipe(
107             delay(3000),
108             take(10)
109           )
110         )
111       )
112       .subscribe(
113         msg => this.parseMessage(msg),
114         err => console.error(err),
115         () => console.log('complete')
116       );
117
118     let listCommunitiesForm: ListCommunitiesForm = {
119       sort: SortType[SortType.TopAll],
120       limit: 9999,
121     };
122
123     WebSocketService.Instance.listCommunities(listCommunitiesForm);
124   }
125
126   componentDidMount() {
127     autosize(document.querySelectorAll('textarea'));
128   }
129
130   componentWillUnmount() {
131     this.subscription.unsubscribe();
132   }
133
134   render() {
135     return (
136       <div>
137         <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
138           <div class="form-group row">
139             <label class="col-sm-2 col-form-label">
140               <T i18nKey="url">#</T>
141             </label>
142             <div class="col-sm-10">
143               <input
144                 type="url"
145                 class="form-control"
146                 defaultValue={this.state.postForm.url}
147                 onInput={linkEvent(this, this.handlePostUrlChange)}
148               />
149               {this.state.suggestedTitle && (
150                 <div
151                   class="mt-1 text-muted small font-weight-bold pointer"
152                   onClick={linkEvent(this, this.copySuggestedTitle)}
153                 >
154                   <T
155                     i18nKey="copy_suggested_title"
156                     interpolation={{ title: this.state.suggestedTitle }}
157                   >
158                     #
159                   </T>
160                 </div>
161               )}
162               <form>
163                 <label
164                   htmlFor="file-upload"
165                   className={`${UserService.Instance.user &&
166                     'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
167                 >
168                   <T i18nKey="upload_image">#</T>
169                 </label>
170                 <input
171                   id="file-upload"
172                   type="file"
173                   accept="image/*,video/*"
174                   name="file"
175                   class="d-none"
176                   disabled={!UserService.Instance.user}
177                   onChange={linkEvent(this, this.handleImageUpload)}
178                 />
179               </form>
180               {validURL(this.state.postForm.url) && (
181                 <a
182                   href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
183                     this.state.postForm.url
184                   )}`}
185                   target="_blank"
186                   class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
187                 >
188                   <T i18nKey="archive_link">#</T>
189                 </a>
190               )}
191               {this.state.imageLoading && (
192                 <svg class="icon icon-spinner spin">
193                   <use xlinkHref="#icon-spinner"></use>
194                 </svg>
195               )}
196               {this.state.crossPosts.length > 0 && (
197                 <>
198                   <div class="my-1 text-muted small font-weight-bold">
199                     <T i18nKey="cross_posts">#</T>
200                   </div>
201                   <PostListings showCommunity posts={this.state.crossPosts} />
202                 </>
203               )}
204             </div>
205           </div>
206           <div class="form-group row">
207             <label class="col-sm-2 col-form-label">
208               <T i18nKey="title">#</T>
209             </label>
210             <div class="col-sm-10">
211               <textarea
212                 defaultValue={
213                   this.props.post ? this.props.post.name : undefined
214                 }
215                 /* This needs to be undefined for some weird reason */
216                 value={this.state.postForm.name}
217                 onInput={linkEvent(this, this.handlePostNameChange)}
218                 class="form-control"
219                 required
220                 rows={2}
221                 minLength={3}
222                 maxLength={100}
223               />
224               {this.state.suggestedPosts.length > 0 && (
225                 <>
226                   <div class="my-1 text-muted small font-weight-bold">
227                     <T i18nKey="related_posts">#</T>
228                   </div>
229                   <PostListings posts={this.state.suggestedPosts} />
230                 </>
231               )}
232             </div>
233           </div>
234           <div class="form-group row">
235             <label class="col-sm-2 col-form-label">
236               <T i18nKey="body">#</T>
237             </label>
238             <div class="col-sm-10">
239               <textarea
240                 value={this.state.postForm.body}
241                 onInput={linkEvent(this, this.handlePostBodyChange)}
242                 className={`form-control ${this.state.previewMode && 'd-none'}`}
243                 rows={4}
244                 maxLength={10000}
245               />
246               {this.state.previewMode && (
247                 <div
248                   className="md-div"
249                   dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
250                 />
251               )}
252               {this.state.postForm.body && (
253                 <button
254                   className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
255                     .previewMode && 'active'}`}
256                   onClick={linkEvent(this, this.handlePreviewToggle)}
257                 >
258                   <T i18nKey="preview">#</T>
259                 </button>
260               )}
261               <a
262                 href={markdownHelpUrl}
263                 target="_blank"
264                 class="d-inline-block float-right text-muted small font-weight-bold"
265               >
266                 <T i18nKey="formatting_help">#</T>
267               </a>
268             </div>
269           </div>
270           {!this.props.post && (
271             <div class="form-group row">
272               <label class="col-sm-2 col-form-label">
273                 <T i18nKey="community">#</T>
274               </label>
275               <div class="col-sm-10">
276                 <select
277                   class="form-control"
278                   value={this.state.postForm.community_id}
279                   onInput={linkEvent(this, this.handlePostCommunityChange)}
280                 >
281                   {this.state.communities.map(community => (
282                     <option value={community.id}>{community.name}</option>
283                   ))}
284                 </select>
285               </div>
286             </div>
287           )}
288           {WebSocketService.Instance.site.enable_nsfw && (
289             <div class="form-group row">
290               <div class="col-sm-10">
291                 <div class="form-check">
292                   <input
293                     class="form-check-input"
294                     type="checkbox"
295                     checked={this.state.postForm.nsfw}
296                     onChange={linkEvent(this, this.handlePostNsfwChange)}
297                   />
298                   <label class="form-check-label">
299                     <T i18nKey="nsfw">#</T>
300                   </label>
301                 </div>
302               </div>
303             </div>
304           )}
305           <div class="form-group row">
306             <div class="col-sm-10">
307               <button type="submit" class="btn btn-secondary mr-2">
308                 {this.state.loading ? (
309                   <svg class="icon icon-spinner spin">
310                     <use xlinkHref="#icon-spinner"></use>
311                   </svg>
312                 ) : this.props.post ? (
313                   capitalizeFirstLetter(i18n.t('save'))
314                 ) : (
315                   capitalizeFirstLetter(i18n.t('create'))
316                 )}
317               </button>
318               {this.props.post && (
319                 <button
320                   type="button"
321                   class="btn btn-secondary"
322                   onClick={linkEvent(this, this.handleCancel)}
323                 >
324                   <T i18nKey="cancel">#</T>
325                 </button>
326               )}
327             </div>
328           </div>
329         </form>
330       </div>
331     );
332   }
333
334   handlePostSubmit(i: PostForm, event: any) {
335     event.preventDefault();
336     if (i.props.post) {
337       WebSocketService.Instance.editPost(i.state.postForm);
338     } else {
339       WebSocketService.Instance.createPost(i.state.postForm);
340     }
341     i.state.loading = true;
342     i.setState(i.state);
343   }
344
345   copySuggestedTitle(i: PostForm) {
346     i.state.postForm.name = i.state.suggestedTitle;
347     i.state.suggestedTitle = undefined;
348     i.setState(i.state);
349   }
350
351   handlePostUrlChange = debounce((i: PostForm, event: any) => {
352     i.state.postForm.url = event.target.value;
353     if (validURL(i.state.postForm.url)) {
354       let form: SearchForm = {
355         q: i.state.postForm.url,
356         type_: SearchType[SearchType.Url],
357         sort: SortType[SortType.TopAll],
358         page: 1,
359         limit: 6,
360       };
361
362       WebSocketService.Instance.search(form);
363
364       // Fetch the page title
365       getPageTitle(i.state.postForm.url).then(d => {
366         i.state.suggestedTitle = d;
367         i.setState(i.state);
368       });
369     } else {
370       i.state.suggestedTitle = undefined;
371       i.state.crossPosts = [];
372     }
373
374     i.setState(i.state);
375   });
376
377   handlePostNameChange = debounce((i: PostForm, event: any) => {
378     i.state.postForm.name = event.target.value;
379     let form: SearchForm = {
380       q: i.state.postForm.name,
381       type_: SearchType[SearchType.Posts],
382       sort: SortType[SortType.TopAll],
383       community_id: i.state.postForm.community_id,
384       page: 1,
385       limit: 6,
386     };
387
388     if (i.state.postForm.name !== '') {
389       WebSocketService.Instance.search(form);
390     } else {
391       i.state.suggestedPosts = [];
392     }
393
394     i.setState(i.state);
395   });
396
397   handlePostBodyChange(i: PostForm, event: any) {
398     i.state.postForm.body = event.target.value;
399     i.setState(i.state);
400   }
401
402   handlePostCommunityChange(i: PostForm, event: any) {
403     i.state.postForm.community_id = Number(event.target.value);
404     i.setState(i.state);
405   }
406
407   handlePostNsfwChange(i: PostForm, event: any) {
408     i.state.postForm.nsfw = event.target.checked;
409     i.setState(i.state);
410   }
411
412   handleCancel(i: PostForm) {
413     i.props.onCancel();
414   }
415
416   handlePreviewToggle(i: PostForm, event: any) {
417     event.preventDefault();
418     i.state.previewMode = !i.state.previewMode;
419     i.setState(i.state);
420   }
421
422   handleImageUpload(i: PostForm, event: any) {
423     event.preventDefault();
424     let file = event.target.files[0];
425     const imageUploadUrl = `/pictshare/api/upload.php`;
426     const formData = new FormData();
427     formData.append('file', file);
428
429     i.state.imageLoading = true;
430     i.setState(i.state);
431
432     fetch(imageUploadUrl, {
433       method: 'POST',
434       body: formData,
435     })
436       .then(res => res.json())
437       .then(res => {
438         let url = `${window.location.origin}/pictshare/${res.url}`;
439         if (res.filetype == 'mp4') {
440           url += '/raw';
441         }
442         i.state.postForm.url = url;
443         i.state.imageLoading = false;
444         i.setState(i.state);
445       })
446       .catch(error => {
447         i.state.imageLoading = false;
448         i.setState(i.state);
449         alert(error);
450       });
451   }
452
453   parseMessage(msg: any) {
454     let op: UserOperation = msgOp(msg);
455     if (msg.error) {
456       alert(i18n.t(msg.error));
457       this.state.loading = false;
458       this.setState(this.state);
459       return;
460     } else if (op == UserOperation.ListCommunities) {
461       let res: ListCommunitiesResponse = msg;
462       this.state.communities = res.communities;
463       if (this.props.post) {
464         this.state.postForm.community_id = this.props.post.community_id;
465       } else if (this.props.params && this.props.params.community) {
466         let foundCommunityId = res.communities.find(
467           r => r.name == this.props.params.community
468         ).id;
469         this.state.postForm.community_id = foundCommunityId;
470       } else {
471         this.state.postForm.community_id = res.communities[0].id;
472       }
473       this.setState(this.state);
474     } else if (op == UserOperation.CreatePost) {
475       this.state.loading = false;
476       let res: PostResponse = msg;
477       this.props.onCreate(res.post.id);
478     } else if (op == UserOperation.EditPost) {
479       this.state.loading = false;
480       let res: PostResponse = msg;
481       this.props.onEdit(res.post);
482     } else if (op == UserOperation.Search) {
483       let res: SearchResponse = msg;
484
485       if (res.type_ == SearchType[SearchType.Posts]) {
486         this.state.suggestedPosts = res.posts;
487       } else if (res.type_ == SearchType[SearchType.Url]) {
488         this.state.crossPosts = res.posts;
489       }
490       this.setState(this.state);
491     }
492   }
493 }