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';
5 import { PostForm as PostFormI, PostFormParams, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { msgOp, getPageTitle, debounce, validURL, capitalizeFirstLetter, imageUploadUrl, markdownHelpUrl, mdToHtml } from '../utils';
8 import * as autosize from 'autosize';
9 import { i18n } from '../i18next';
10 import { T } from 'inferno-i18next';
12 interface PostFormProps {
13 post?: Post; // If a post is given, that means this is an edit
14 params?: PostFormParams;
16 onCreate?(id: number): any;
17 onEdit?(post: Post): any;
20 interface PostFormState {
22 communities: Array<Community>;
25 suggestedTitle: string;
26 suggestedPosts: Array<Post>;
27 crossPosts: Array<Post>;
30 export class PostForm extends Component<PostFormProps, PostFormState> {
32 private subscription: Subscription;
33 private emptyState: PostFormState = {
39 creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null,
44 suggestedTitle: undefined,
49 constructor(props: any, context: any) {
50 super(props, context);
52 this.state = this.emptyState;
54 if (this.props.post) {
55 this.state.postForm = {
56 body: this.props.post.body,
57 name: this.props.post.name,
58 community_id: this.props.post.community_id,
59 edit_id: this.props.post.id,
60 creator_id: this.props.post.creator_id,
61 url: this.props.post.url,
62 nsfw: this.props.post.nsfw,
67 if (this.props.params) {
68 this.state.postForm.name = this.props.params.name;
69 if (this.props.params.url) {
70 this.state.postForm.url = this.props.params.url;
72 if (this.props.params.body) {
73 this.state.postForm.body = this.props.params.body;
77 this.subscription = WebSocketService.Instance.subject
78 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
80 (msg) => this.parseMessage(msg),
81 (err) => console.error(err),
82 () => console.log('complete')
85 let listCommunitiesForm: ListCommunitiesForm = {
86 sort: SortType[SortType.TopAll],
90 WebSocketService.Instance.listCommunities(listCommunitiesForm);
94 autosize(document.querySelectorAll('textarea'));
97 componentWillUnmount() {
98 this.subscription.unsubscribe();
104 <form onSubmit={linkEvent(this, this.handlePostSubmit)}>
105 <div class="form-group row">
106 <label class="col-sm-2 col-form-label"><T i18nKey="url">#</T></label>
107 <div class="col-sm-10">
108 <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} />
109 {this.state.suggestedTitle &&
110 <div class="mt-1 text-muted small font-weight-bold pointer" onClick={linkEvent(this, this.copySuggestedTitle)}><T i18nKey="copy_suggested_title" interpolation={{title: this.state.suggestedTitle}}>#</T></div>
113 <label htmlFor="file-upload" class="pointer d-inline-block mr-2 float-right text-muted small font-weight-bold"><T i18nKey="upload_image">#</T></label>
114 <input id="file-upload" type="file" name="file" class="d-none" onChange={linkEvent(this, this.handleImageUpload)} />
116 {this.state.crossPosts.length > 0 &&
118 <div class="my-1 text-muted small font-weight-bold"><T i18nKey="cross_posts">#</T></div>
119 <PostListings showCommunity posts={this.state.crossPosts} />
124 <div class="form-group row">
125 <label class="col-sm-2 col-form-label"><T i18nKey="title">#</T></label>
126 <div class="col-sm-10">
127 <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={2} minLength={3} maxLength={100} />
128 {this.state.suggestedPosts.length > 0 &&
130 <div class="my-1 text-muted small font-weight-bold"><T i18nKey="related_posts">#</T></div>
131 <PostListings posts={this.state.suggestedPosts} />
136 <div class="form-group row">
137 <label class="col-sm-2 col-form-label"><T i18nKey="body">#</T></label>
138 <div class="col-sm-10">
139 <textarea value={this.state.postForm.body} onInput={linkEvent(this, this.handlePostBodyChange)} className={`form-control ${this.state.previewMode && 'd-none'}`} rows={4} maxLength={10000} />
140 {this.state.previewMode &&
141 <div className="md-div" dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)} />
143 {this.state.postForm.body &&
144 <button className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state.previewMode && 'active'}`} onClick={linkEvent(this, this.handlePreviewToggle)}><T i18nKey="preview">#</T></button>
146 <a href={markdownHelpUrl} target="_blank" class="d-inline-block float-right text-muted small font-weight-bold"><T i18nKey="formatting_help">#</T></a>
150 <div class="form-group row">
151 <label class="col-sm-2 col-form-label"><T i18nKey="community">#</T></label>
152 <div class="col-sm-10">
153 <select class="form-control" value={this.state.postForm.community_id} onInput={linkEvent(this, this.handlePostCommunityChange)}>
154 {this.state.communities.map(community =>
155 <option value={community.id}>{community.name}</option>
161 <div class="form-group row">
162 <div class="col-sm-10">
163 <div class="form-check">
164 <input class="form-check-input" type="checkbox" checked={this.state.postForm.nsfw} onChange={linkEvent(this, this.handlePostNsfwChange)}/>
165 <label class="form-check-label"><T i18nKey="nsfw">#</T></label>
169 <div class="form-group row">
170 <div class="col-sm-10">
171 <button type="submit" class="btn btn-secondary mr-2">
172 {this.state.loading ?
173 <svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg> :
174 this.props.post ? capitalizeFirstLetter(i18n.t('save')) : capitalizeFirstLetter(i18n.t('create'))}</button>
175 {this.props.post && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleCancel)}><T i18nKey="cancel">#</T></button>}
183 handlePostSubmit(i: PostForm, event: any) {
184 event.preventDefault();
186 WebSocketService.Instance.editPost(i.state.postForm);
188 WebSocketService.Instance.createPost(i.state.postForm);
190 i.state.loading = true;
194 copySuggestedTitle(i: PostForm) {
195 i.state.postForm.name = i.state.suggestedTitle;
196 i.state.suggestedTitle = undefined;
200 handlePostUrlChange(i: PostForm, event: any) {
201 i.state.postForm.url = event.target.value;
202 if (validURL(i.state.postForm.url)) {
204 let form: SearchForm = {
205 q: i.state.postForm.url,
206 type_: SearchType[SearchType.Url],
207 sort: SortType[SortType.TopAll],
212 WebSocketService.Instance.search(form);
214 // Fetch the page title
215 getPageTitle(i.state.postForm.url).then(d => {
216 i.state.suggestedTitle = d;
220 i.state.suggestedTitle = undefined;
221 i.state.crossPosts = [];
227 handlePostNameChange(i: PostForm, event: any) {
228 i.state.postForm.name = event.target.value;
229 let form: SearchForm = {
230 q: i.state.postForm.name,
231 type_: SearchType[SearchType.Posts],
232 sort: SortType[SortType.TopAll],
233 community_id: i.state.postForm.community_id,
238 if (i.state.postForm.name !== '') {
239 WebSocketService.Instance.search(form);
241 i.state.suggestedPosts = [];
247 handlePostBodyChange(i: PostForm, event: any) {
248 i.state.postForm.body = event.target.value;
252 handlePostCommunityChange(i: PostForm, event: any) {
253 i.state.postForm.community_id = Number(event.target.value);
257 handlePostNsfwChange(i: PostForm, event: any) {
258 i.state.postForm.nsfw = event.target.checked;
262 handleCancel(i: PostForm) {
266 handlePreviewToggle(i: PostForm, event: any) {
267 event.preventDefault();
268 i.state.previewMode = !i.state.previewMode;
272 handleImageUpload(i: PostForm, event: any) {
273 event.preventDefault();
274 let file = event.target.files[0];
275 const imageUploadUrl = `/pictshare/api/upload.php`;
276 const formData = new FormData();
277 formData.append('file', file);
278 fetch(imageUploadUrl, {
282 .then(res => res.json())
284 let url = `${window.location.origin}/pictshare/${res.url}`;
285 if (res.filetype == 'mp4') {
289 i.state.postForm.url = url;
292 .catch((error) => alert(error));
295 parseMessage(msg: any) {
296 let op: UserOperation = msgOp(msg);
298 alert(i18n.t(msg.error));
299 this.state.loading = false;
300 this.setState(this.state);
302 } else if (op == UserOperation.ListCommunities) {
303 let res: ListCommunitiesResponse = msg;
304 this.state.communities = res.communities;
305 if (this.props.post) {
306 this.state.postForm.community_id = this.props.post.community_id;
307 } else if (this.props.params && this.props.params.community) {
308 let foundCommunityId = res.communities.find(r => r.name == this.props.params.community).id;
309 this.state.postForm.community_id = foundCommunityId;
311 this.state.postForm.community_id = res.communities[0].id;
313 this.setState(this.state);
314 } else if (op == UserOperation.CreatePost) {
315 this.state.loading = false;
316 let res: PostResponse = msg;
317 this.props.onCreate(res.post.id);
318 } else if (op == UserOperation.EditPost) {
319 this.state.loading = false;
320 let res: PostResponse = msg;
321 this.props.onEdit(res.post);
322 } else if (op == UserOperation.Search) {
323 let res: SearchResponse = msg;
325 if (res.type_ == SearchType[SearchType.Posts]) {
326 this.state.suggestedPosts = res.posts;
327 } else if (res.type_ == SearchType[SearchType.Url]) {
328 this.state.crossPosts = res.posts;
330 this.setState(this.state);