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 defaultValue={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">
213 this.props.post ? this.props.post.name : undefined
215 /* This needs to be undefined for some weird reason */
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 {WebSocketService.Instance.site.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 = debounce((i: PostForm, event: any) => {
352 i.state.postForm.url = event.target.value;
353 if (validURL(i.state.postForm.url)) {
354 let form: SearchForm = {
355 q: i.state.postForm.url,
356 type_: SearchType[SearchType.Url],
357 sort: SortType[SortType.TopAll],
362 WebSocketService.Instance.search(form);
364 // Fetch the page title
365 getPageTitle(i.state.postForm.url).then(d => {
366 i.state.suggestedTitle = d;
370 i.state.suggestedTitle = undefined;
371 i.state.crossPosts = [];
377 handlePostNameChange = debounce((i: PostForm, event: any) => {
378 i.state.postForm.name = event.target.value;
379 let form: SearchForm = {
380 q: i.state.postForm.name,
381 type_: SearchType[SearchType.Posts],
382 sort: SortType[SortType.TopAll],
383 community_id: i.state.postForm.community_id,
388 if (i.state.postForm.name !== '') {
389 WebSocketService.Instance.search(form);
391 i.state.suggestedPosts = [];
397 handlePostBodyChange(i: PostForm, event: any) {
398 i.state.postForm.body = event.target.value;
402 handlePostCommunityChange(i: PostForm, event: any) {
403 i.state.postForm.community_id = Number(event.target.value);
407 handlePostNsfwChange(i: PostForm, event: any) {
408 i.state.postForm.nsfw = event.target.checked;
412 handleCancel(i: PostForm) {
416 handlePreviewToggle(i: PostForm, event: any) {
417 event.preventDefault();
418 i.state.previewMode = !i.state.previewMode;
422 handleImageUpload(i: PostForm, event: any) {
423 event.preventDefault();
424 let file = event.target.files[0];
425 const imageUploadUrl = `/pictshare/api/upload.php`;
426 const formData = new FormData();
427 formData.append('file', file);
429 i.state.imageLoading = true;
432 fetch(imageUploadUrl, {
436 .then(res => res.json())
438 let url = `${window.location.origin}/pictshare/${res.url}`;
439 if (res.filetype == 'mp4') {
442 i.state.postForm.url = url;
443 i.state.imageLoading = false;
447 i.state.imageLoading = false;
453 parseMessage(msg: any) {
454 let op: UserOperation = msgOp(msg);
456 alert(i18n.t(msg.error));
457 this.state.loading = false;
458 this.setState(this.state);
460 } else if (op == UserOperation.ListCommunities) {
461 let res: ListCommunitiesResponse = msg;
462 this.state.communities = res.communities;
463 if (this.props.post) {
464 this.state.postForm.community_id = this.props.post.community_id;
465 } else if (this.props.params && this.props.params.community) {
466 let foundCommunityId = res.communities.find(
467 r => r.name == this.props.params.community
469 this.state.postForm.community_id = foundCommunityId;
471 this.state.postForm.community_id = res.communities[0].id;
473 this.setState(this.state);
474 } else if (op == UserOperation.CreatePost) {
475 this.state.loading = false;
476 let res: PostResponse = msg;
477 this.props.onCreate(res.post.id);
478 } else if (op == UserOperation.EditPost) {
479 this.state.loading = false;
480 let res: PostResponse = msg;
481 this.props.onEdit(res.post);
482 } else if (op == UserOperation.Search) {
483 let res: SearchResponse = msg;
485 if (res.type_ == SearchType[SearchType.Posts]) {
486 this.state.suggestedPosts = res.posts;
487 } else if (res.type_ == SearchType[SearchType.Url]) {
488 this.state.crossPosts = res.posts;
490 this.setState(this.state);