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