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,
32 import autosize from 'autosize';
33 import { i18n } from '../i18next';
34 import { T } from 'inferno-i18next';
36 interface PostFormProps {
37 post?: Post; // If a post is given, that means this is an edit
38 params?: PostFormParams;
40 onCreate?(id: number): any;
41 onEdit?(post: Post): any;
44 interface PostFormState {
46 communities: Array<Community>;
48 imageLoading: boolean;
50 suggestedTitle: string;
51 suggestedPosts: Array<Post>;
52 crossPosts: Array<Post>;
56 export class PostForm extends Component<PostFormProps, PostFormState> {
57 private subscription: Subscription;
58 private emptyState: PostFormState = {
64 creator_id: UserService.Instance.user
65 ? UserService.Instance.user.id
72 suggestedTitle: undefined,
75 enable_nsfw: undefined,
78 constructor(props: any, context: any) {
79 super(props, context);
80 this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
81 this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
83 this.state = this.emptyState;
85 if (this.props.post) {
86 this.state.postForm = {
87 body: this.props.post.body,
88 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
89 name: this.props.post.name,
90 community_id: this.props.post.community_id,
91 edit_id: this.props.post.id,
92 creator_id: this.props.post.creator_id,
93 url: this.props.post.url,
94 nsfw: this.props.post.nsfw,
99 if (this.props.params) {
100 this.state.postForm.name = this.props.params.name;
101 if (this.props.params.url) {
102 this.state.postForm.url = this.props.params.url;
104 if (this.props.params.body) {
105 this.state.postForm.body = this.props.params.body;
109 this.subscription = WebSocketService.Instance.subject
110 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
112 msg => this.parseMessage(msg),
113 err => console.error(err),
114 () => console.log('complete')
117 let listCommunitiesForm: ListCommunitiesForm = {
118 sort: SortType[SortType.TopAll],
122 WebSocketService.Instance.listCommunities(listCommunitiesForm);
123 WebSocketService.Instance.getSite();
126 componentDidMount() {
127 autosize(document.querySelectorAll('textarea'));
130 componentWillUnmount() {
131 this.subscription.unsubscribe();
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>
142 <div class="col-sm-10">
146 value={this.state.postForm.url}
147 onInput={linkEvent(this, this.handlePostUrlChange)}
149 {this.state.suggestedTitle && (
151 class="mt-1 text-muted small font-weight-bold pointer"
152 onClick={linkEvent(this, this.copySuggestedTitle)}
155 i18nKey="copy_suggested_title"
156 interpolation={{ title: this.state.suggestedTitle }}
164 htmlFor="file-upload"
165 className={`${UserService.Instance.user &&
166 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
168 <T i18nKey="upload_image">#</T>
173 accept="image/*,video/*"
176 disabled={!UserService.Instance.user}
177 onChange={linkEvent(this, this.handleImageUpload)}
180 {validURL(this.state.postForm.url) && (
182 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
183 this.state.postForm.url
186 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
188 <T i18nKey="archive_link">#</T>
191 {this.state.imageLoading && (
192 <svg class="icon icon-spinner spin">
193 <use xlinkHref="#icon-spinner"></use>
196 {isImage(this.state.postForm.url) && (
197 <img src={this.state.postForm.url} class="img-fluid" />
199 {this.state.crossPosts.length > 0 && (
201 <div class="my-1 text-muted small font-weight-bold">
202 <T i18nKey="cross_posts">#</T>
204 <PostListings showCommunity posts={this.state.crossPosts} />
209 <div class="form-group row">
210 <label class="col-sm-2 col-form-label">
211 <T i18nKey="title">#</T>
213 <div class="col-sm-10">
215 value={this.state.postForm.name}
216 onInput={linkEvent(this, this.handlePostNameChange)}
223 {this.state.suggestedPosts.length > 0 && (
225 <div class="my-1 text-muted small font-weight-bold">
226 <T i18nKey="related_posts">#</T>
228 <PostListings posts={this.state.suggestedPosts} />
233 <div class="form-group row">
234 <label class="col-sm-2 col-form-label">
235 <T i18nKey="body">#</T>
237 <div class="col-sm-10">
239 value={this.state.postForm.body}
240 onInput={linkEvent(this, this.handlePostBodyChange)}
241 className={`form-control ${this.state.previewMode && 'd-none'}`}
245 {this.state.previewMode && (
248 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
251 {this.state.postForm.body && (
253 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
254 .previewMode && 'active'}`}
255 onClick={linkEvent(this, this.handlePreviewToggle)}
257 <T i18nKey="preview">#</T>
261 href={markdownHelpUrl}
263 class="d-inline-block float-right text-muted small font-weight-bold"
265 <T i18nKey="formatting_help">#</T>
269 {!this.props.post && (
270 <div class="form-group row">
271 <label class="col-sm-2 col-form-label">
272 <T i18nKey="community">#</T>
274 <div class="col-sm-10">
277 value={this.state.postForm.community_id}
278 onInput={linkEvent(this, this.handlePostCommunityChange)}
280 {this.state.communities.map(community => (
281 <option value={community.id}>{community.name}</option>
287 {this.state.enable_nsfw && (
288 <div class="form-group row">
289 <div class="col-sm-10">
290 <div class="form-check">
292 class="form-check-input"
294 checked={this.state.postForm.nsfw}
295 onChange={linkEvent(this, this.handlePostNsfwChange)}
297 <label class="form-check-label">
298 <T i18nKey="nsfw">#</T>
304 <div class="form-group row">
305 <div class="col-sm-10">
306 <button type="submit" class="btn btn-secondary mr-2">
307 {this.state.loading ? (
308 <svg class="icon icon-spinner spin">
309 <use xlinkHref="#icon-spinner"></use>
311 ) : this.props.post ? (
312 capitalizeFirstLetter(i18n.t('save'))
314 capitalizeFirstLetter(i18n.t('create'))
317 {this.props.post && (
320 class="btn btn-secondary"
321 onClick={linkEvent(this, this.handleCancel)}
323 <T i18nKey="cancel">#</T>
333 handlePostSubmit(i: PostForm, event: any) {
334 event.preventDefault();
336 WebSocketService.Instance.editPost(i.state.postForm);
338 WebSocketService.Instance.createPost(i.state.postForm);
340 i.state.loading = true;
344 copySuggestedTitle(i: PostForm) {
345 i.state.postForm.name = i.state.suggestedTitle;
346 i.state.suggestedTitle = undefined;
350 handlePostUrlChange(i: PostForm, event: any) {
351 i.state.postForm.url = event.target.value;
357 if (validURL(this.state.postForm.url)) {
358 let form: SearchForm = {
359 q: this.state.postForm.url,
360 type_: SearchType[SearchType.Url],
361 sort: SortType[SortType.TopAll],
366 WebSocketService.Instance.search(form);
368 // Fetch the page title
369 getPageTitle(this.state.postForm.url).then(d => {
370 this.state.suggestedTitle = d;
371 this.setState(this.state);
374 this.state.suggestedTitle = undefined;
375 this.state.crossPosts = [];
379 handlePostNameChange(i: PostForm, event: any) {
380 i.state.postForm.name = event.target.value;
382 i.fetchSimilarPosts();
385 fetchSimilarPosts() {
386 let form: SearchForm = {
387 q: this.state.postForm.name,
388 type_: SearchType[SearchType.Posts],
389 sort: SortType[SortType.TopAll],
390 community_id: this.state.postForm.community_id,
395 if (this.state.postForm.name !== '') {
396 WebSocketService.Instance.search(form);
398 this.state.suggestedPosts = [];
401 this.setState(this.state);
404 handlePostBodyChange(i: PostForm, event: any) {
405 i.state.postForm.body = event.target.value;
409 handlePostCommunityChange(i: PostForm, event: any) {
410 i.state.postForm.community_id = Number(event.target.value);
414 handlePostNsfwChange(i: PostForm, event: any) {
415 i.state.postForm.nsfw = event.target.checked;
419 handleCancel(i: PostForm) {
423 handlePreviewToggle(i: PostForm, event: any) {
424 event.preventDefault();
425 i.state.previewMode = !i.state.previewMode;
429 handleImageUpload(i: PostForm, event: any) {
430 event.preventDefault();
431 let file = event.target.files[0];
432 const imageUploadUrl = `/pictshare/api/upload.php`;
433 const formData = new FormData();
434 formData.append('file', file);
436 i.state.imageLoading = true;
439 fetch(imageUploadUrl, {
443 .then(res => res.json())
445 let url = `${window.location.origin}/pictshare/${res.url}`;
446 if (res.filetype == 'mp4') {
449 i.state.postForm.url = url;
450 i.state.imageLoading = false;
454 i.state.imageLoading = false;
460 parseMessage(msg: any) {
461 let res = wsJsonToRes(msg);
463 alert(i18n.t(res.error));
464 this.state.loading = false;
465 this.setState(this.state);
467 } else if (res.op == UserOperation.ListCommunities) {
468 let data = res.data as ListCommunitiesResponse;
469 this.state.communities = data.communities;
470 if (this.props.post) {
471 this.state.postForm.community_id = this.props.post.community_id;
472 } else if (this.props.params && this.props.params.community) {
473 let foundCommunityId = data.communities.find(
474 r => r.name == this.props.params.community
476 this.state.postForm.community_id = foundCommunityId;
478 this.state.postForm.community_id = data.communities[0].id;
480 this.setState(this.state);
481 } else if (res.op == UserOperation.CreatePost) {
482 let data = res.data as PostResponse;
483 this.state.loading = false;
484 this.props.onCreate(data.post.id);
485 } else if (res.op == UserOperation.EditPost) {
486 let data = res.data as PostResponse;
487 this.state.loading = false;
488 this.props.onEdit(data.post);
489 } else if (res.op == UserOperation.Search) {
490 let data = res.data as SearchResponse;
492 if (data.type_ == SearchType[SearchType.Posts]) {
493 this.state.suggestedPosts = data.posts;
494 } else if (data.type_ == SearchType[SearchType.Url]) {
495 this.state.crossPosts = data.posts;
497 this.setState(this.state);
498 } else if (res.op == UserOperation.GetSite) {
499 let data = res.data as GetSiteResponse;
500 this.state.enable_nsfw = data.site.enable_nsfw;
501 this.setState(this.state);