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 WebSocketJsonResponse,
20 } from '../interfaces';
21 import { WebSocketService, UserService } from '../services';
26 capitalizeFirstLetter,
34 import autosize from 'autosize';
35 import { i18n } from '../i18next';
36 import { T } from 'inferno-i18next';
38 interface PostFormProps {
39 post?: Post; // If a post is given, that means this is an edit
40 params?: PostFormParams;
42 onCreate?(id: number): any;
43 onEdit?(post: Post): any;
46 interface PostFormState {
48 communities: Array<Community>;
50 imageLoading: boolean;
52 suggestedTitle: string;
53 suggestedPosts: Array<Post>;
54 crossPosts: Array<Post>;
58 export class PostForm extends Component<PostFormProps, PostFormState> {
59 private subscription: Subscription;
60 private emptyState: PostFormState = {
66 creator_id: UserService.Instance.user
67 ? UserService.Instance.user.id
74 suggestedTitle: undefined,
77 enable_nsfw: undefined,
80 constructor(props: any, context: any) {
81 super(props, context);
82 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
83 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
85 this.state = this.emptyState;
87 if (this.props.post) {
88 this.state.postForm = {
89 body: this.props.post.body,
90 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
91 name: this.props.post.name,
92 community_id: this.props.post.community_id,
93 edit_id: this.props.post.id,
94 creator_id: this.props.post.creator_id,
95 url: this.props.post.url,
96 nsfw: this.props.post.nsfw,
101 if (this.props.params) {
102 this.state.postForm.name = this.props.params.name;
103 if (this.props.params.url) {
104 this.state.postForm.url = this.props.params.url;
106 if (this.props.params.body) {
107 this.state.postForm.body = this.props.params.body;
111 this.subscription = WebSocketService.Instance.subject
112 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
114 msg => this.parseMessage(msg),
115 err => console.error(err),
116 () => console.log('complete')
119 let listCommunitiesForm: ListCommunitiesForm = {
120 sort: SortType[SortType.TopAll],
124 WebSocketService.Instance.listCommunities(listCommunitiesForm);
125 WebSocketService.Instance.getSite();
128 componentDidMount() {
129 autosize(document.querySelectorAll('textarea'));
132 componentWillUnmount() {
133 this.subscription.unsubscribe();
139 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
140 <div class="form-group row">
141 <label class="col-sm-2 col-form-label">
142 <T i18nKey="url">#</T>
144 <div class="col-sm-10">
148 value={this.state.postForm.url}
149 onInput={linkEvent(this, this.handlePostUrlChange)}
151 {this.state.suggestedTitle && (
153 class="mt-1 text-muted small font-weight-bold pointer"
154 onClick={linkEvent(this, this.copySuggestedTitle)}
157 i18nKey="copy_suggested_title"
158 interpolation={{ title: this.state.suggestedTitle }}
166 htmlFor="file-upload"
167 className={`${UserService.Instance.user &&
168 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
170 <T i18nKey="upload_image">#</T>
175 accept="image/*,video/*"
178 disabled={!UserService.Instance.user}
179 onChange={linkEvent(this, this.handleImageUpload)}
182 {validURL(this.state.postForm.url) && (
184 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
185 this.state.postForm.url
188 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
190 <T i18nKey="archive_link">#</T>
193 {this.state.imageLoading && (
194 <svg class="icon icon-spinner spin">
195 <use xlinkHref="#icon-spinner"></use>
198 {isImage(this.state.postForm.url) && (
199 <img src={this.state.postForm.url} class="img-fluid" />
201 {this.state.crossPosts.length > 0 && (
203 <div class="my-1 text-muted small font-weight-bold">
204 <T i18nKey="cross_posts">#</T>
206 <PostListings showCommunity posts={this.state.crossPosts} />
211 <div class="form-group row">
212 <label class="col-sm-2 col-form-label">
213 <T i18nKey="title">#</T>
215 <div class="col-sm-10">
217 value={this.state.postForm.name}
218 onInput={linkEvent(this, this.handlePostNameChange)}
225 {this.state.suggestedPosts.length > 0 && (
227 <div class="my-1 text-muted small font-weight-bold">
228 <T i18nKey="related_posts">#</T>
230 <PostListings posts={this.state.suggestedPosts} />
235 <div class="form-group row">
236 <label class="col-sm-2 col-form-label">
237 <T i18nKey="body">#</T>
239 <div class="col-sm-10">
241 value={this.state.postForm.body}
242 onInput={linkEvent(this, this.handlePostBodyChange)}
243 className={`form-control ${this.state.previewMode && 'd-none'}`}
247 {this.state.previewMode && (
250 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
253 {this.state.postForm.body && (
255 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
256 .previewMode && 'active'}`}
257 onClick={linkEvent(this, this.handlePreviewToggle)}
259 <T i18nKey="preview">#</T>
263 href={markdownHelpUrl}
265 class="d-inline-block float-right text-muted small font-weight-bold"
267 <T i18nKey="formatting_help">#</T>
271 {!this.props.post && (
272 <div class="form-group row">
273 <label class="col-sm-2 col-form-label">
274 <T i18nKey="community">#</T>
276 <div class="col-sm-10">
279 value={this.state.postForm.community_id}
280 onInput={linkEvent(this, this.handlePostCommunityChange)}
282 {this.state.communities.map(community => (
283 <option value={community.id}>{community.name}</option>
289 {this.state.enable_nsfw && (
290 <div class="form-group row">
291 <div class="col-sm-10">
292 <div class="form-check">
294 class="form-check-input"
296 checked={this.state.postForm.nsfw}
297 onChange={linkEvent(this, this.handlePostNsfwChange)}
299 <label class="form-check-label">
300 <T i18nKey="nsfw">#</T>
306 <div class="form-group row">
307 <div class="col-sm-10">
308 <button type="submit" class="btn btn-secondary mr-2">
309 {this.state.loading ? (
310 <svg class="icon icon-spinner spin">
311 <use xlinkHref="#icon-spinner"></use>
313 ) : this.props.post ? (
314 capitalizeFirstLetter(i18n.t('save'))
316 capitalizeFirstLetter(i18n.t('create'))
319 {this.props.post && (
322 class="btn btn-secondary"
323 onClick={linkEvent(this, this.handleCancel)}
325 <T i18nKey="cancel">#</T>
335 handlePostSubmit(i: PostForm, event: any) {
336 event.preventDefault();
338 WebSocketService.Instance.editPost(i.state.postForm);
340 WebSocketService.Instance.createPost(i.state.postForm);
342 i.state.loading = true;
346 copySuggestedTitle(i: PostForm) {
347 i.state.postForm.name = i.state.suggestedTitle;
348 i.state.suggestedTitle = undefined;
352 handlePostUrlChange(i: PostForm, event: any) {
353 i.state.postForm.url = event.target.value;
359 if (validURL(this.state.postForm.url)) {
360 let form: SearchForm = {
361 q: this.state.postForm.url,
362 type_: SearchType[SearchType.Url],
363 sort: SortType[SortType.TopAll],
368 WebSocketService.Instance.search(form);
370 // Fetch the page title
371 getPageTitle(this.state.postForm.url).then(d => {
372 this.state.suggestedTitle = d;
373 this.setState(this.state);
376 this.state.suggestedTitle = undefined;
377 this.state.crossPosts = [];
381 handlePostNameChange(i: PostForm, event: any) {
382 i.state.postForm.name = event.target.value;
384 i.fetchSimilarPosts();
387 fetchSimilarPosts() {
388 let form: SearchForm = {
389 q: this.state.postForm.name,
390 type_: SearchType[SearchType.Posts],
391 sort: SortType[SortType.TopAll],
392 community_id: this.state.postForm.community_id,
397 if (this.state.postForm.name !== '') {
398 WebSocketService.Instance.search(form);
400 this.state.suggestedPosts = [];
403 this.setState(this.state);
406 handlePostBodyChange(i: PostForm, event: any) {
407 i.state.postForm.body = event.target.value;
411 handlePostCommunityChange(i: PostForm, event: any) {
412 i.state.postForm.community_id = Number(event.target.value);
416 handlePostNsfwChange(i: PostForm, event: any) {
417 i.state.postForm.nsfw = event.target.checked;
421 handleCancel(i: PostForm) {
425 handlePreviewToggle(i: PostForm, event: any) {
426 event.preventDefault();
427 i.state.previewMode = !i.state.previewMode;
431 handleImageUpload(i: PostForm, event: any) {
432 event.preventDefault();
433 let file = event.target.files[0];
434 const imageUploadUrl = `/pictshare/api/upload.php`;
435 const formData = new FormData();
436 formData.append('file', file);
438 i.state.imageLoading = true;
441 fetch(imageUploadUrl, {
445 .then(res => res.json())
447 let url = `${window.location.origin}/pictshare/${res.url}`;
448 if (res.filetype == 'mp4') {
451 i.state.postForm.url = url;
452 i.state.imageLoading = false;
456 i.state.imageLoading = false;
458 toast(error, 'danger');
462 parseMessage(msg: WebSocketJsonResponse) {
463 let res = wsJsonToRes(msg);
465 toast(i18n.t(msg.error), 'danger');
466 this.state.loading = false;
467 this.setState(this.state);
469 } else if (res.op == UserOperation.ListCommunities) {
470 let data = res.data as ListCommunitiesResponse;
471 this.state.communities = data.communities;
472 if (this.props.post) {
473 this.state.postForm.community_id = this.props.post.community_id;
474 } else if (this.props.params && this.props.params.community) {
475 let foundCommunityId = data.communities.find(
476 r => r.name == this.props.params.community
478 this.state.postForm.community_id = foundCommunityId;
480 this.state.postForm.community_id = data.communities[0].id;
482 this.setState(this.state);
483 } else if (res.op == UserOperation.CreatePost) {
484 let data = res.data as PostResponse;
485 this.state.loading = false;
486 this.props.onCreate(data.post.id);
487 } else if (res.op == UserOperation.EditPost) {
488 let data = res.data as PostResponse;
489 this.state.loading = false;
490 this.props.onEdit(data.post);
491 } else if (res.op == UserOperation.Search) {
492 let data = res.data as SearchResponse;
494 if (data.type_ == SearchType[SearchType.Posts]) {
495 this.state.suggestedPosts = data.posts;
496 } else if (data.type_ == SearchType[SearchType.Url]) {
497 this.state.crossPosts = data.posts;
499 this.setState(this.state);
500 } else if (res.op == UserOperation.GetSite) {
501 let data = res.data as GetSiteResponse;
502 this.state.enable_nsfw = data.site.enable_nsfw;
503 this.setState(this.state);