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,
18 } from '../interfaces';
19 import { WebSocketService, UserService } from '../services';
24 capitalizeFirstLetter,
30 import * as autosize from 'autosize';
31 import { i18n } from '../i18next';
32 import { T } from 'inferno-i18next';
34 interface PostFormProps {
35 post?: Post; // If a post is given, that means this is an edit
36 params?: PostFormParams;
38 onCreate?(id: number): any;
39 onEdit?(post: Post): any;
42 interface PostFormState {
44 communities: Array<Community>;
46 imageLoading: boolean;
48 suggestedTitle: string;
49 suggestedPosts: Array<Post>;
50 crossPosts: Array<Post>;
53 export class PostForm extends Component<PostFormProps, PostFormState> {
54 private subscription: Subscription;
55 private emptyState: PostFormState = {
61 creator_id: UserService.Instance.user
62 ? UserService.Instance.user.id
69 suggestedTitle: undefined,
74 constructor(props: any, context: any) {
75 super(props, context);
77 this.state = this.emptyState;
79 if (this.props.post) {
80 this.state.postForm = {
81 body: this.props.post.body,
82 // NOTE: debouncing breaks both these for some reason, unless you use defaultValue
84 community_id: this.props.post.community_id,
85 edit_id: this.props.post.id,
86 creator_id: this.props.post.creator_id,
87 url: this.props.post.url,
88 nsfw: this.props.post.nsfw,
93 if (this.props.params) {
94 this.state.postForm.name = this.props.params.name;
95 if (this.props.params.url) {
96 this.state.postForm.url = this.props.params.url;
98 if (this.props.params.body) {
99 this.state.postForm.body = this.props.params.body;
103 this.subscription = WebSocketService.Instance.subject
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);
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 {this.state.crossPosts.length > 0 && (
198 <div class="my-1 text-muted small font-weight-bold">
199 <T i18nKey="cross_posts">#</T>
201 <PostListings showCommunity posts={this.state.crossPosts} />
206 <div class="form-group row">
207 <label class="col-sm-2 col-form-label">
208 <T i18nKey="title">#</T>
210 <div class="col-sm-10">
212 value={this.state.postForm.name}
213 onInput={linkEvent(this, this.handlePostNameChange)}
220 {this.state.suggestedPosts.length > 0 && (
222 <div class="my-1 text-muted small font-weight-bold">
223 <T i18nKey="related_posts">#</T>
225 <PostListings posts={this.state.suggestedPosts} />
230 <div class="form-group row">
231 <label class="col-sm-2 col-form-label">
232 <T i18nKey="body">#</T>
234 <div class="col-sm-10">
236 value={this.state.postForm.body}
237 onInput={linkEvent(this, this.handlePostBodyChange)}
238 className={`form-control ${this.state.previewMode && 'd-none'}`}
242 {this.state.previewMode && (
245 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
248 {this.state.postForm.body && (
250 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
251 .previewMode && 'active'}`}
252 onClick={linkEvent(this, this.handlePreviewToggle)}
254 <T i18nKey="preview">#</T>
258 href={markdownHelpUrl}
260 class="d-inline-block float-right text-muted small font-weight-bold"
262 <T i18nKey="formatting_help">#</T>
266 {!this.props.post && (
267 <div class="form-group row">
268 <label class="col-sm-2 col-form-label">
269 <T i18nKey="community">#</T>
271 <div class="col-sm-10">
274 value={this.state.postForm.community_id}
275 onInput={linkEvent(this, this.handlePostCommunityChange)}
277 {this.state.communities.map(community => (
278 <option value={community.id}>{community.name}</option>
284 {WebSocketService.Instance.site.enable_nsfw && (
285 <div class="form-group row">
286 <div class="col-sm-10">
287 <div class="form-check">
289 class="form-check-input"
291 checked={this.state.postForm.nsfw}
292 onChange={linkEvent(this, this.handlePostNsfwChange)}
294 <label class="form-check-label">
295 <T i18nKey="nsfw">#</T>
301 <div class="form-group row">
302 <div class="col-sm-10">
303 <button type="submit" class="btn btn-secondary mr-2">
304 {this.state.loading ? (
305 <svg class="icon icon-spinner spin">
306 <use xlinkHref="#icon-spinner"></use>
308 ) : this.props.post ? (
309 capitalizeFirstLetter(i18n.t('save'))
311 capitalizeFirstLetter(i18n.t('create'))
314 {this.props.post && (
317 class="btn btn-secondary"
318 onClick={linkEvent(this, this.handleCancel)}
320 <T i18nKey="cancel">#</T>
330 handlePostSubmit(i: PostForm, event: any) {
331 event.preventDefault();
333 WebSocketService.Instance.editPost(i.state.postForm);
335 WebSocketService.Instance.createPost(i.state.postForm);
337 i.state.loading = true;
341 copySuggestedTitle(i: PostForm) {
342 i.state.postForm.name = i.state.suggestedTitle;
343 i.state.suggestedTitle = undefined;
347 handlePostUrlChange(i: PostForm, event: any) {
348 i.state.postForm.url = event.target.value;
349 if (validURL(i.state.postForm.url)) {
350 let form: SearchForm = {
351 q: i.state.postForm.url,
352 type_: SearchType[SearchType.Url],
353 sort: SortType[SortType.TopAll],
358 WebSocketService.Instance.search(form);
360 // Fetch the page title
361 getPageTitle(i.state.postForm.url).then(d => {
362 i.state.suggestedTitle = d;
366 i.state.suggestedTitle = undefined;
367 i.state.crossPosts = [];
373 handlePostNameChange(i: PostForm, event: any) {
374 i.state.postForm.name = event.target.value;
375 let form: SearchForm = {
376 q: i.state.postForm.name,
377 type_: SearchType[SearchType.Posts],
378 sort: SortType[SortType.TopAll],
379 community_id: i.state.postForm.community_id,
384 if (i.state.postForm.name !== '') {
385 WebSocketService.Instance.search(form);
387 i.state.suggestedPosts = [];
393 handlePostBodyChange(i: PostForm, event: any) {
394 i.state.postForm.body = event.target.value;
398 handlePostCommunityChange(i: PostForm, event: any) {
399 i.state.postForm.community_id = Number(event.target.value);
403 handlePostNsfwChange(i: PostForm, event: any) {
404 i.state.postForm.nsfw = event.target.checked;
408 handleCancel(i: PostForm) {
412 handlePreviewToggle(i: PostForm, event: any) {
413 event.preventDefault();
414 i.state.previewMode = !i.state.previewMode;
418 handleImageUpload(i: PostForm, event: any) {
419 event.preventDefault();
420 let file = event.target.files[0];
421 const imageUploadUrl = `/pictshare/api/upload.php`;
422 const formData = new FormData();
423 formData.append('file', file);
425 i.state.imageLoading = true;
428 fetch(imageUploadUrl, {
432 .then(res => res.json())
434 let url = `${window.location.origin}/pictshare/${res.url}`;
435 if (res.filetype == 'mp4') {
438 i.state.postForm.url = url;
439 i.state.imageLoading = false;
443 i.state.imageLoading = false;
449 parseMessage(msg: any) {
450 let op: UserOperation = msgOp(msg);
452 alert(i18n.t(msg.error));
453 this.state.loading = false;
454 this.setState(this.state);
456 } else if (op == UserOperation.ListCommunities) {
457 let res: ListCommunitiesResponse = msg;
458 this.state.communities = res.communities;
459 if (this.props.post) {
460 this.state.postForm.community_id = this.props.post.community_id;
461 } else if (this.props.params && this.props.params.community) {
462 let foundCommunityId = res.communities.find(
463 r => r.name == this.props.params.community
465 this.state.postForm.community_id = foundCommunityId;
467 this.state.postForm.community_id = res.communities[0].id;
469 this.setState(this.state);
470 } else if (op == UserOperation.CreatePost) {
471 this.state.loading = false;
472 let res: PostResponse = msg;
473 this.props.onCreate(res.post.id);
474 } else if (op == UserOperation.EditPost) {
475 this.state.loading = false;
476 let res: PostResponse = msg;
477 this.props.onEdit(res.post);
478 } else if (op == UserOperation.Search) {
479 let res: SearchResponse = msg;
481 if (res.type_ == SearchType[SearchType.Posts]) {
482 this.state.suggestedPosts = res.posts;
483 } else if (res.type_ == SearchType[SearchType.Url]) {
484 this.state.crossPosts = res.posts;
486 this.setState(this.state);