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 * as 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
119 msg => this.parseMessage(msg),
120 err => console.error(err),
121 () => console.log('complete')
124 let listCommunitiesForm: ListCommunitiesForm = {
125 sort: SortType[SortType.TopAll],
129 WebSocketService.Instance.listCommunities(listCommunitiesForm);
130 WebSocketService.Instance.getSite();
133 componentDidMount() {
134 autosize(document.querySelectorAll('textarea'));
137 componentWillUnmount() {
138 this.subscription.unsubscribe();
144 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
145 <div class="form-group row">
146 <label class="col-sm-2 col-form-label">
147 <T i18nKey="url">#</T>
149 <div class="col-sm-10">
153 value={this.state.postForm.url}
154 onInput={linkEvent(this, this.handlePostUrlChange)}
156 {this.state.suggestedTitle && (
158 class="mt-1 text-muted small font-weight-bold pointer"
159 onClick={linkEvent(this, this.copySuggestedTitle)}
162 i18nKey="copy_suggested_title"
163 interpolation={{ title: this.state.suggestedTitle }}
171 htmlFor="file-upload"
172 className={`${UserService.Instance.user &&
173 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
175 <T i18nKey="upload_image">#</T>
180 accept="image/*,video/*"
183 disabled={!UserService.Instance.user}
184 onChange={linkEvent(this, this.handleImageUpload)}
187 {validURL(this.state.postForm.url) && (
189 href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
190 this.state.postForm.url
193 class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
195 <T i18nKey="archive_link">#</T>
198 {this.state.imageLoading && (
199 <svg class="icon icon-spinner spin">
200 <use xlinkHref="#icon-spinner"></use>
203 {isImage(this.state.postForm.url) && (
204 <img src={this.state.postForm.url} class="img-fluid" />
206 {this.state.crossPosts.length > 0 && (
208 <div class="my-1 text-muted small font-weight-bold">
209 <T i18nKey="cross_posts">#</T>
211 <PostListings showCommunity posts={this.state.crossPosts} />
216 <div class="form-group row">
217 <label class="col-sm-2 col-form-label">
218 <T i18nKey="title">#</T>
220 <div class="col-sm-10">
222 value={this.state.postForm.name}
223 onInput={linkEvent(this, this.handlePostNameChange)}
230 {this.state.suggestedPosts.length > 0 && (
232 <div class="my-1 text-muted small font-weight-bold">
233 <T i18nKey="related_posts">#</T>
235 <PostListings posts={this.state.suggestedPosts} />
240 <div class="form-group row">
241 <label class="col-sm-2 col-form-label">
242 <T i18nKey="body">#</T>
244 <div class="col-sm-10">
246 value={this.state.postForm.body}
247 onInput={linkEvent(this, this.handlePostBodyChange)}
248 className={`form-control ${this.state.previewMode && 'd-none'}`}
252 {this.state.previewMode && (
255 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
258 {this.state.postForm.body && (
260 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
261 .previewMode && 'active'}`}
262 onClick={linkEvent(this, this.handlePreviewToggle)}
264 <T i18nKey="preview">#</T>
268 href={markdownHelpUrl}
270 class="d-inline-block float-right text-muted small font-weight-bold"
272 <T i18nKey="formatting_help">#</T>
276 {!this.props.post && (
277 <div class="form-group row">
278 <label class="col-sm-2 col-form-label">
279 <T i18nKey="community">#</T>
281 <div class="col-sm-10">
284 value={this.state.postForm.community_id}
285 onInput={linkEvent(this, this.handlePostCommunityChange)}
287 {this.state.communities.map(community => (
288 <option value={community.id}>{community.name}</option>
294 {this.state.enable_nsfw && (
295 <div class="form-group row">
296 <div class="col-sm-10">
297 <div class="form-check">
299 class="form-check-input"
301 checked={this.state.postForm.nsfw}
302 onChange={linkEvent(this, this.handlePostNsfwChange)}
304 <label class="form-check-label">
305 <T i18nKey="nsfw">#</T>
311 <div class="form-group row">
312 <div class="col-sm-10">
313 <button type="submit" class="btn btn-secondary mr-2">
314 {this.state.loading ? (
315 <svg class="icon icon-spinner spin">
316 <use xlinkHref="#icon-spinner"></use>
318 ) : this.props.post ? (
319 capitalizeFirstLetter(i18n.t('save'))
321 capitalizeFirstLetter(i18n.t('create'))
324 {this.props.post && (
327 class="btn btn-secondary"
328 onClick={linkEvent(this, this.handleCancel)}
330 <T i18nKey="cancel">#</T>
340 handlePostSubmit(i: PostForm, event: any) {
341 event.preventDefault();
343 WebSocketService.Instance.editPost(i.state.postForm);
345 WebSocketService.Instance.createPost(i.state.postForm);
347 i.state.loading = true;
351 copySuggestedTitle(i: PostForm) {
352 i.state.postForm.name = i.state.suggestedTitle;
353 i.state.suggestedTitle = undefined;
357 handlePostUrlChange(i: PostForm, event: any) {
358 i.state.postForm.url = event.target.value;
364 if (validURL(this.state.postForm.url)) {
365 let form: SearchForm = {
366 q: this.state.postForm.url,
367 type_: SearchType[SearchType.Url],
368 sort: SortType[SortType.TopAll],
373 WebSocketService.Instance.search(form);
375 // Fetch the page title
376 getPageTitle(this.state.postForm.url).then(d => {
377 this.state.suggestedTitle = d;
378 this.setState(this.state);
381 this.state.suggestedTitle = undefined;
382 this.state.crossPosts = [];
386 handlePostNameChange(i: PostForm, event: any) {
387 i.state.postForm.name = event.target.value;
389 i.fetchSimilarPosts();
392 fetchSimilarPosts() {
393 let form: SearchForm = {
394 q: this.state.postForm.name,
395 type_: SearchType[SearchType.Posts],
396 sort: SortType[SortType.TopAll],
397 community_id: this.state.postForm.community_id,
402 if (this.state.postForm.name !== '') {
403 WebSocketService.Instance.search(form);
405 this.state.suggestedPosts = [];
408 this.setState(this.state);
411 handlePostBodyChange(i: PostForm, event: any) {
412 i.state.postForm.body = event.target.value;
416 handlePostCommunityChange(i: PostForm, event: any) {
417 i.state.postForm.community_id = Number(event.target.value);
421 handlePostNsfwChange(i: PostForm, event: any) {
422 i.state.postForm.nsfw = event.target.checked;
426 handleCancel(i: PostForm) {
430 handlePreviewToggle(i: PostForm, event: any) {
431 event.preventDefault();
432 i.state.previewMode = !i.state.previewMode;
436 handleImageUpload(i: PostForm, event: any) {
437 event.preventDefault();
438 let file = event.target.files[0];
439 const imageUploadUrl = `/pictshare/api/upload.php`;
440 const formData = new FormData();
441 formData.append('file', file);
443 i.state.imageLoading = true;
446 fetch(imageUploadUrl, {
450 .then(res => res.json())
452 let url = `${window.location.origin}/pictshare/${res.url}`;
453 if (res.filetype == 'mp4') {
456 i.state.postForm.url = url;
457 i.state.imageLoading = false;
461 i.state.imageLoading = false;
467 parseMessage(msg: any) {
468 let op: UserOperation = msgOp(msg);
470 alert(i18n.t(msg.error));
471 this.state.loading = false;
472 this.setState(this.state);
474 } else if (op == UserOperation.ListCommunities) {
475 let res: ListCommunitiesResponse = msg;
476 this.state.communities = res.communities;
477 if (this.props.post) {
478 this.state.postForm.community_id = this.props.post.community_id;
479 } else if (this.props.params && this.props.params.community) {
480 let foundCommunityId = res.communities.find(
481 r => r.name == this.props.params.community
483 this.state.postForm.community_id = foundCommunityId;
485 this.state.postForm.community_id = res.communities[0].id;
487 this.setState(this.state);
488 } else if (op == UserOperation.CreatePost) {
489 this.state.loading = false;
490 let res: PostResponse = msg;
491 this.props.onCreate(res.post.id);
492 } else if (op == UserOperation.EditPost) {
493 this.state.loading = false;
494 let res: PostResponse = msg;
495 this.props.onEdit(res.post);
496 } else if (op == UserOperation.Search) {
497 let res: SearchResponse = msg;
499 if (res.type_ == SearchType[SearchType.Posts]) {
500 this.state.suggestedPosts = res.posts;
501 } else if (res.type_ == SearchType[SearchType.Url]) {
502 this.state.crossPosts = res.posts;
504 this.setState(this.state);
505 } else if (op == UserOperation.GetSite) {
506 let res: GetSiteResponse = msg;
507 this.state.enable_nsfw = res.site.enable_nsfw;
508 this.setState(this.state);