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';
25 capitalizeFirstLetter,
29 import * as autosize from 'autosize';
30 import { i18n } from '../i18next';
31 import { T } from 'inferno-i18next';
33 interface PostFormProps {
34 post?: Post; // If a post is given, that means this is an edit
35 params?: PostFormParams;
37 onCreate?(id: number): any;
38 onEdit?(post: Post): any;
41 interface PostFormState {
43 communities: Array<Community>;
45 imageLoading: boolean;
47 suggestedTitle: string;
48 suggestedPosts: Array<Post>;
49 crossPosts: Array<Post>;
52 export class PostForm extends Component<PostFormProps, PostFormState> {
53 private subscription: Subscription;
54 private emptyState: PostFormState = {
60 creator_id: UserService.Instance.user
61 ? UserService.Instance.user.id
68 suggestedTitle: undefined,
73 constructor(props: any, context: any) {
74 super(props, context);
76 this.state = this.emptyState;
78 if (this.props.post) {
79 this.state.postForm = {
80 body: this.props.post.body,
81 name: this.props.post.name,
82 community_id: this.props.post.community_id,
83 edit_id: this.props.post.id,
84 creator_id: this.props.post.creator_id,
85 url: this.props.post.url,
86 nsfw: this.props.post.nsfw,
91 if (this.props.params) {
92 this.state.postForm.name = this.props.params.name;
93 if (this.props.params.url) {
94 this.state.postForm.url = this.props.params.url;
96 if (this.props.params.body) {
97 this.state.postForm.body = this.props.params.body;
101 this.subscription = WebSocketService.Instance.subject
111 msg => this.parseMessage(msg),
112 err => console.error(err),
113 () => console.log('complete')
116 let listCommunitiesForm: ListCommunitiesForm = {
117 sort: SortType[SortType.TopAll],
121 WebSocketService.Instance.listCommunities(listCommunitiesForm);
124 componentDidMount() {
125 autosize(document.querySelectorAll('textarea'));
128 componentWillUnmount() {
129 this.subscription.unsubscribe();
135 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
136 <div class="form-group row">
137 <label class="col-sm-2 col-form-label">
138 <T i18nKey="url">#</T>
140 <div class="col-sm-10">
144 value={this.state.postForm.url}
145 onInput={linkEvent(this, this.handlePostUrlChange)}
147 {this.state.suggestedTitle && (
149 class="mt-1 text-muted small font-weight-bold pointer"
150 onClick={linkEvent(this, this.copySuggestedTitle)}
153 i18nKey="copy_suggested_title"
154 interpolation={{ title: this.state.suggestedTitle }}
162 htmlFor="file-upload"
163 className={`${UserService.Instance.user &&
164 'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
166 <T i18nKey="upload_image">#</T>
171 accept="image/*,video/*"
174 disabled={!UserService.Instance.user}
175 onChange={linkEvent(this, this.handleImageUpload)}
178 {this.state.imageLoading && (
179 <svg class="icon icon-spinner spin">
180 <use xlinkHref="#icon-spinner"></use>
183 {this.state.crossPosts.length > 0 && (
185 <div class="my-1 text-muted small font-weight-bold">
186 <T i18nKey="cross_posts">#</T>
188 <PostListings showCommunity posts={this.state.crossPosts} />
193 <div class="form-group row">
194 <label class="col-sm-2 col-form-label">
195 <T i18nKey="title">#</T>
197 <div class="col-sm-10">
199 value={this.state.postForm.name}
200 onInput={linkEvent(this, this.handlePostNameChange)}
207 {this.state.suggestedPosts.length > 0 && (
209 <div class="my-1 text-muted small font-weight-bold">
210 <T i18nKey="related_posts">#</T>
212 <PostListings posts={this.state.suggestedPosts} />
217 <div class="form-group row">
218 <label class="col-sm-2 col-form-label">
219 <T i18nKey="body">#</T>
221 <div class="col-sm-10">
223 value={this.state.postForm.body}
224 onInput={linkEvent(this, this.handlePostBodyChange)}
225 className={`form-control ${this.state.previewMode && 'd-none'}`}
229 {this.state.previewMode && (
232 dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
235 {this.state.postForm.body && (
237 className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
238 .previewMode && 'active'}`}
239 onClick={linkEvent(this, this.handlePreviewToggle)}
241 <T i18nKey="preview">#</T>
245 href={markdownHelpUrl}
247 class="d-inline-block float-right text-muted small font-weight-bold"
249 <T i18nKey="formatting_help">#</T>
253 {!this.props.post && (
254 <div class="form-group row">
255 <label class="col-sm-2 col-form-label">
256 <T i18nKey="community">#</T>
258 <div class="col-sm-10">
261 value={this.state.postForm.community_id}
262 onInput={linkEvent(this, this.handlePostCommunityChange)}
264 {this.state.communities.map(community => (
265 <option value={community.id}>{community.name}</option>
271 <div class="form-group row">
272 <div class="col-sm-10">
273 <div class="form-check">
275 class="form-check-input"
277 checked={this.state.postForm.nsfw}
278 onChange={linkEvent(this, this.handlePostNsfwChange)}
280 <label class="form-check-label">
281 <T i18nKey="nsfw">#</T>
286 <div class="form-group row">
287 <div class="col-sm-10">
288 <button type="submit" class="btn btn-secondary mr-2">
289 {this.state.loading ? (
290 <svg class="icon icon-spinner spin">
291 <use xlinkHref="#icon-spinner"></use>
293 ) : this.props.post ? (
294 capitalizeFirstLetter(i18n.t('save'))
296 capitalizeFirstLetter(i18n.t('create'))
299 {this.props.post && (
302 class="btn btn-secondary"
303 onClick={linkEvent(this, this.handleCancel)}
305 <T i18nKey="cancel">#</T>
315 handlePostSubmit(i: PostForm, event: any) {
316 event.preventDefault();
318 WebSocketService.Instance.editPost(i.state.postForm);
320 WebSocketService.Instance.createPost(i.state.postForm);
322 i.state.loading = true;
326 copySuggestedTitle(i: PostForm) {
327 i.state.postForm.name = i.state.suggestedTitle;
328 i.state.suggestedTitle = undefined;
332 handlePostUrlChange(i: PostForm, event: any) {
333 i.state.postForm.url = event.target.value;
334 if (validURL(i.state.postForm.url)) {
335 let form: SearchForm = {
336 q: i.state.postForm.url,
337 type_: SearchType[SearchType.Url],
338 sort: SortType[SortType.TopAll],
343 WebSocketService.Instance.search(form);
345 // Fetch the page title
346 getPageTitle(i.state.postForm.url).then(d => {
347 i.state.suggestedTitle = d;
351 i.state.suggestedTitle = undefined;
352 i.state.crossPosts = [];
358 handlePostNameChange(i: PostForm, event: any) {
359 i.state.postForm.name = event.target.value;
360 let form: SearchForm = {
361 q: i.state.postForm.name,
362 type_: SearchType[SearchType.Posts],
363 sort: SortType[SortType.TopAll],
364 community_id: i.state.postForm.community_id,
369 if (i.state.postForm.name !== '') {
370 WebSocketService.Instance.search(form);
372 i.state.suggestedPosts = [];
378 handlePostBodyChange(i: PostForm, event: any) {
379 i.state.postForm.body = event.target.value;
383 handlePostCommunityChange(i: PostForm, event: any) {
384 i.state.postForm.community_id = Number(event.target.value);
388 handlePostNsfwChange(i: PostForm, event: any) {
389 i.state.postForm.nsfw = event.target.checked;
393 handleCancel(i: PostForm) {
397 handlePreviewToggle(i: PostForm, event: any) {
398 event.preventDefault();
399 i.state.previewMode = !i.state.previewMode;
403 handleImageUpload(i: PostForm, event: any) {
404 event.preventDefault();
405 let file = event.target.files[0];
406 const imageUploadUrl = `/pictshare/api/upload.php`;
407 const formData = new FormData();
408 formData.append('file', file);
410 i.state.imageLoading = true;
413 fetch(imageUploadUrl, {
417 .then(res => res.json())
419 let url = `${window.location.origin}/pictshare/${res.url}`;
420 if (res.filetype == 'mp4') {
423 i.state.postForm.url = url;
424 i.state.imageLoading = false;
428 i.state.imageLoading = false;
434 parseMessage(msg: any) {
435 let op: UserOperation = msgOp(msg);
437 alert(i18n.t(msg.error));
438 this.state.loading = false;
439 this.setState(this.state);
441 } else if (op == UserOperation.ListCommunities) {
442 let res: ListCommunitiesResponse = msg;
443 this.state.communities = res.communities;
444 if (this.props.post) {
445 this.state.postForm.community_id = this.props.post.community_id;
446 } else if (this.props.params && this.props.params.community) {
447 let foundCommunityId = res.communities.find(
448 r => r.name == this.props.params.community
450 this.state.postForm.community_id = foundCommunityId;
452 this.state.postForm.community_id = res.communities[0].id;
454 this.setState(this.state);
455 } else if (op == UserOperation.CreatePost) {
456 this.state.loading = false;
457 let res: PostResponse = msg;
458 this.props.onCreate(res.post.id);
459 } else if (op == UserOperation.EditPost) {
460 this.state.loading = false;
461 let res: PostResponse = msg;
462 this.props.onEdit(res.post);
463 } else if (op == UserOperation.Search) {
464 let res: SearchResponse = msg;
466 if (res.type_ == SearchType[SearchType.Posts]) {
467 this.state.suggestedPosts = res.posts;
468 } else if (res.type_ == SearchType[SearchType.Url]) {
469 this.state.crossPosts = res.posts;
471 this.setState(this.state);