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';
12 ListCommunitiesResponse,
19 } from '../interfaces';
20 import { WebSocketService, UserService } from '../services';
25 capitalizeFirstLetter,
33 import autosize from 'autosize';
34 import { i18n } from '../i18next';
35 import { T } from 'inferno-i18next';
37 interface PostFormProps {
38 post?: Post; // If a post is given, that means this is an edit
39 params?: PostFormParams;
41 onCreate?(id: number): any;
42 onEdit?(post: Post): any;
45 interface PostFormState {
47 communities: Array<Community>;
49 imageLoading: boolean;
51 suggestedTitle: string;
52 suggestedPosts: Array<Post>;
53 crossPosts: Array<Post>;
57 export class PostForm extends Component<PostFormProps, PostFormState> {
58 private subscription: Subscription;
59 private emptyState: PostFormState = {
65 creator_id: UserService.Instance.user
66 ? UserService.Instance.user.id
73 suggestedTitle: undefined,
76 enable_nsfw: undefined,
79 constructor(props: any, context: any) {
80 super(props, context);
81 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
82 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
84 this.state = this.emptyState;
86 if (this.props.post) {
87 this.state.postForm = {
88 body: this.props.post.body,
89 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
90 name: this.props.post.name,
91 community_id: this.props.post.community_id,
92 edit_id: this.props.post.id,
93 creator_id: this.props.post.creator_id,
94 url: this.props.post.url,
95 nsfw: this.props.post.nsfw,
100 if (this.props.params) {
101 this.state.postForm.name = this.props.params.name;
102 if (this.props.params.url) {
103 this.state.postForm.url = this.props.params.url;
105 if (this.props.params.body) {
106 this.state.postForm.body = this.props.params.body;
110 this.subscription = WebSocketService.Instance.subject
111 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
113 msg => this.parseMessage(msg),
114 err => console.error(err),
115 () => console.log('complete')
118 let listCommunitiesForm: ListCommunitiesForm = {
119 sort: SortType[SortType.TopAll],
123 WebSocketService.Instance.listCommunities(listCommunitiesForm);
124 WebSocketService.Instance.getSite();
127 componentDidMount() {
128 autosize(document.querySelectorAll('textarea'));
131 componentWillUnmount() {
132 this.subscription.unsubscribe();
138 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
139 <div class="form-group row">
140 <label class="col-sm-2 col-form-label">
141 <T i18nKey="url">#</T>
143 <div class="col-sm-10">
147 value={this.state.postForm.url}
148 onInput={linkEvent(this, this.handlePostUrlChange)}
150 {this.state.suggestedTitle && (
152 class="mt-1 text-muted small font-weight-bold pointer"
153 onClick={linkEvent(this, this.copySuggestedTitle)}
156 i18nKey="copy_suggested_title"
157 interpolation={{ title: this.state.suggestedTitle }}
165 htmlFor="file-upload"
166 className={`${UserService.Instance.user &&
167 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
169 <T i18nKey="upload_image">#</T>
174 accept="image/*,video/*"
177 disabled={!UserService.Instance.user}
178 onChange={linkEvent(this, this.handleImageUpload)}
181 {validURL(this.state.postForm.url) && (
183 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
184 this.state.postForm.url
187 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
189 <T i18nKey="archive_link">#</T>
192 {this.state.imageLoading && (
193 <svg class="icon icon-spinner spin">
194 <use xlinkHref="#icon-spinner"></use>
197 {isImage(this.state.postForm.url) && (
198 <img src={this.state.postForm.url} class="img-fluid" />
200 {this.state.crossPosts.length > 0 && (
202 <div class="my-1 text-muted small font-weight-bold">
203 <T i18nKey="cross_posts">#</T>
205 <PostListings showCommunity posts={this.state.crossPosts} />
210 <div class="form-group row">
211 <label class="col-sm-2 col-form-label">
212 <T i18nKey="title">#</T>
214 <div class="col-sm-10">
216 value={this.state.postForm.name}
217 onInput={linkEvent(this, this.handlePostNameChange)}
224 {this.state.suggestedPosts.length > 0 && (
226 <div class="my-1 text-muted small font-weight-bold">
227 <T i18nKey="related_posts">#</T>
229 <PostListings posts={this.state.suggestedPosts} />
234 <div class="form-group row">
235 <label class="col-sm-2 col-form-label">
236 <T i18nKey="body">#</T>
238 <div class="col-sm-10">
240 value={this.state.postForm.body}
241 onInput={linkEvent(this, this.handlePostBodyChange)}
242 className={`form-control ${this.state.previewMode && 'd-none'}`}
246 {this.state.previewMode && (
249 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
252 {this.state.postForm.body && (
254 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
255 .previewMode && 'active'}`}
256 onClick={linkEvent(this, this.handlePreviewToggle)}
258 <T i18nKey="preview">#</T>
262 href={markdownHelpUrl}
264 class="d-inline-block float-right text-muted small font-weight-bold"
266 <T i18nKey="formatting_help">#</T>
270 {!this.props.post && (
271 <div class="form-group row">
272 <label class="col-sm-2 col-form-label">
273 <T i18nKey="community">#</T>
275 <div class="col-sm-10">
278 value={this.state.postForm.community_id}
279 onInput={linkEvent(this, this.handlePostCommunityChange)}
281 {this.state.communities.map(community => (
282 <option value={community.id}>{community.name}</option>
288 {this.state.enable_nsfw && (
289 <div class="form-group row">
290 <div class="col-sm-10">
291 <div class="form-check">
293 class="form-check-input"
295 checked={this.state.postForm.nsfw}
296 onChange={linkEvent(this, this.handlePostNsfwChange)}
298 <label class="form-check-label">
299 <T i18nKey="nsfw">#</T>
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>
312 ) : this.props.post ? (
313 capitalizeFirstLetter(i18n.t('save'))
315 capitalizeFirstLetter(i18n.t('create'))
318 {this.props.post && (
321 class="btn btn-secondary"
322 onClick={linkEvent(this, this.handleCancel)}
324 <T i18nKey="cancel">#</T>
334 handlePostSubmit(i: PostForm, event: any) {
335 event.preventDefault();
337 WebSocketService.Instance.editPost(i.state.postForm);
339 WebSocketService.Instance.createPost(i.state.postForm);
341 i.state.loading = true;
345 copySuggestedTitle(i: PostForm) {
346 i.state.postForm.name = i.state.suggestedTitle;
347 i.state.suggestedTitle = undefined;
351 handlePostUrlChange(i: PostForm, event: any) {
352 i.state.postForm.url = event.target.value;
358 if (validURL(this.state.postForm.url)) {
359 let form: SearchForm = {
360 q: this.state.postForm.url,
361 type_: SearchType[SearchType.Url],
362 sort: SortType[SortType.TopAll],
367 WebSocketService.Instance.search(form);
369 // Fetch the page title
370 getPageTitle(this.state.postForm.url).then(d => {
371 this.state.suggestedTitle = d;
372 this.setState(this.state);
375 this.state.suggestedTitle = undefined;
376 this.state.crossPosts = [];
380 handlePostNameChange(i: PostForm, event: any) {
381 i.state.postForm.name = event.target.value;
383 i.fetchSimilarPosts();
386 fetchSimilarPosts() {
387 let form: SearchForm = {
388 q: this.state.postForm.name,
389 type_: SearchType[SearchType.Posts],
390 sort: SortType[SortType.TopAll],
391 community_id: this.state.postForm.community_id,
396 if (this.state.postForm.name !== '') {
397 WebSocketService.Instance.search(form);
399 this.state.suggestedPosts = [];
402 this.setState(this.state);
405 handlePostBodyChange(i: PostForm, event: any) {
406 i.state.postForm.body = event.target.value;
410 handlePostCommunityChange(i: PostForm, event: any) {
411 i.state.postForm.community_id = Number(event.target.value);
415 handlePostNsfwChange(i: PostForm, event: any) {
416 i.state.postForm.nsfw = event.target.checked;
420 handleCancel(i: PostForm) {
424 handlePreviewToggle(i: PostForm, event: any) {
425 event.preventDefault();
426 i.state.previewMode = !i.state.previewMode;
430 handleImageUpload(i: PostForm, event: any) {
431 event.preventDefault();
432 let file = event.target.files[0];
433 const imageUploadUrl = `/pictshare/api/upload.php`;
434 const formData = new FormData();
435 formData.append('file', file);
437 i.state.imageLoading = true;
440 fetch(imageUploadUrl, {
444 .then(res => res.json())
446 let url = `${window.location.origin}/pictshare/${res.url}`;
447 if (res.filetype == 'mp4') {
450 i.state.postForm.url = url;
451 i.state.imageLoading = false;
455 i.state.imageLoading = false;
457 toast(error, 'danger');
461 parseMessage(msg: any) {
462 let op: UserOperation = msgOp(msg);
464 toast(i18n.t(msg.error), 'danger');
465 this.state.loading = false;
466 this.setState(this.state);
468 } else if (op == UserOperation.ListCommunities) {
469 let res: ListCommunitiesResponse = msg;
470 this.state.communities = res.communities;
471 if (this.props.post) {
472 this.state.postForm.community_id = this.props.post.community_id;
473 } else if (this.props.params && this.props.params.community) {
474 let foundCommunityId = res.communities.find(
475 r => r.name == this.props.params.community
477 this.state.postForm.community_id = foundCommunityId;
479 this.state.postForm.community_id = res.communities[0].id;
481 this.setState(this.state);
482 } else if (op == UserOperation.CreatePost) {
483 this.state.loading = false;
484 let res: PostResponse = msg;
485 this.props.onCreate(res.post.id);
486 } else if (op == UserOperation.EditPost) {
487 this.state.loading = false;
488 let res: PostResponse = msg;
489 this.props.onEdit(res.post);
490 } else if (op == UserOperation.Search) {
491 let res: SearchResponse = msg;
493 if (res.type_ == SearchType[SearchType.Posts]) {
494 this.state.suggestedPosts = res.posts;
495 } else if (res.type_ == SearchType[SearchType.Url]) {
496 this.state.crossPosts = res.posts;
498 this.setState(this.state);
499 } else if (op == UserOperation.GetSite) {
500 let res: GetSiteResponse = msg;
501 this.state.enable_nsfw = res.site.enable_nsfw;
502 this.setState(this.state);