2019-04-03 20:59:37 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-07-29 01:57:09 +00:00
|
|
|
import { PostListings } from './post-listings';
|
2019-10-19 00:20:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2019-04-03 20:59:37 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
PostForm as PostFormI,
|
|
|
|
PostFormParams,
|
|
|
|
Post,
|
|
|
|
PostResponse,
|
|
|
|
UserOperation,
|
|
|
|
Community,
|
|
|
|
ListCommunitiesResponse,
|
|
|
|
ListCommunitiesForm,
|
|
|
|
SortType,
|
|
|
|
SearchForm,
|
|
|
|
SearchType,
|
|
|
|
SearchResponse,
|
2020-01-03 18:52:21 +00:00
|
|
|
GetSiteResponse,
|
2020-01-19 05:38:45 +00:00
|
|
|
WebSocketJsonResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2019-04-15 23:12:06 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
2020-01-19 04:54:10 +00:00
|
|
|
wsJsonToRes,
|
2019-10-19 00:20:27 +00:00
|
|
|
getPageTitle,
|
|
|
|
validURL,
|
|
|
|
capitalizeFirstLetter,
|
|
|
|
markdownHelpUrl,
|
2019-11-22 02:14:59 +00:00
|
|
|
archiveUrl,
|
2019-10-19 00:20:27 +00:00
|
|
|
mdToHtml,
|
2019-12-15 00:15:28 +00:00
|
|
|
debounce,
|
2019-12-29 21:56:55 +00:00
|
|
|
isImage,
|
2020-01-23 03:29:11 +00:00
|
|
|
toast,
|
2020-01-24 18:59:50 +00:00
|
|
|
randomStr,
|
|
|
|
setupTribute,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../utils';
|
2020-01-06 16:22:51 +00:00
|
|
|
import autosize from 'autosize';
|
2020-01-24 18:59:50 +00:00
|
|
|
import Tribute from 'tributejs/src/Tribute.js';
|
2019-08-09 01:58:04 +00:00
|
|
|
import { i18n } from '../i18next';
|
2019-04-03 20:59:37 +00:00
|
|
|
|
|
|
|
interface PostFormProps {
|
|
|
|
post?: Post; // If a post is given, that means this is an edit
|
2019-08-22 23:13:26 +00:00
|
|
|
params?: PostFormParams;
|
2019-04-08 05:19:02 +00:00
|
|
|
onCancel?(): any;
|
|
|
|
onCreate?(id: number): any;
|
|
|
|
onEdit?(post: Post): any;
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PostFormState {
|
|
|
|
postForm: PostFormI;
|
|
|
|
communities: Array<Community>;
|
2019-04-08 21:46:09 +00:00
|
|
|
loading: boolean;
|
2019-09-08 16:54:53 +00:00
|
|
|
imageLoading: boolean;
|
2019-09-01 04:10:48 +00:00
|
|
|
previewMode: boolean;
|
2019-04-30 14:45:50 +00:00
|
|
|
suggestedTitle: string;
|
2019-07-29 01:57:09 +00:00
|
|
|
suggestedPosts: Array<Post>;
|
2019-08-22 05:17:15 +00:00
|
|
|
crossPosts: Array<Post>;
|
2020-01-03 18:52:21 +00:00
|
|
|
enable_nsfw: boolean;
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PostForm extends Component<PostFormProps, PostFormState> {
|
2020-01-24 18:59:50 +00:00
|
|
|
private id = `post-form-${randomStr()}`;
|
|
|
|
private tribute: Tribute;
|
2019-04-03 20:59:37 +00:00
|
|
|
private subscription: Subscription;
|
|
|
|
private emptyState: PostFormState = {
|
|
|
|
postForm: {
|
|
|
|
name: null,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: false,
|
2019-04-03 20:59:37 +00:00
|
|
|
auth: null,
|
2019-04-15 23:12:06 +00:00
|
|
|
community_id: null,
|
2019-10-19 00:20:27 +00:00
|
|
|
creator_id: UserService.Instance.user
|
|
|
|
? UserService.Instance.user.id
|
|
|
|
: null,
|
2019-04-03 20:59:37 +00:00
|
|
|
},
|
2019-04-08 21:46:09 +00:00
|
|
|
communities: [],
|
2019-04-30 14:45:50 +00:00
|
|
|
loading: false,
|
2019-09-08 16:54:53 +00:00
|
|
|
imageLoading: false,
|
2019-09-01 04:10:48 +00:00
|
|
|
previewMode: false,
|
2019-04-30 14:45:50 +00:00
|
|
|
suggestedTitle: undefined,
|
2019-07-29 01:57:09 +00:00
|
|
|
suggestedPosts: [],
|
2019-08-22 05:17:15 +00:00
|
|
|
crossPosts: [],
|
2020-01-03 18:52:21 +00:00
|
|
|
enable_nsfw: undefined,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
constructor(props: any, context: any) {
|
2019-04-03 20:59:37 +00:00
|
|
|
super(props, context);
|
2020-01-01 16:09:56 +00:00
|
|
|
this.fetchSimilarPosts = debounce(this.fetchSimilarPosts).bind(this);
|
|
|
|
this.fetchPageTitle = debounce(this.fetchPageTitle).bind(this);
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2020-01-24 18:59:50 +00:00
|
|
|
this.tribute = setupTribute();
|
2019-04-03 20:59:37 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
|
|
|
|
if (this.props.post) {
|
|
|
|
this.state.postForm = {
|
|
|
|
body: this.props.post.body,
|
2019-12-15 00:15:28 +00:00
|
|
|
// NOTE: debouncing breaks both these for some reason, unless you use defaultValue
|
2019-12-29 02:10:07 +00:00
|
|
|
name: this.props.post.name,
|
2019-04-03 20:59:37 +00:00
|
|
|
community_id: this.props.post.community_id,
|
|
|
|
edit_id: this.props.post.id,
|
2019-04-15 23:12:06 +00:00
|
|
|
creator_id: this.props.post.creator_id,
|
2019-04-03 20:59:37 +00:00
|
|
|
url: this.props.post.url,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: this.props.post.nsfw,
|
2019-10-19 00:20:27 +00:00
|
|
|
auth: null,
|
|
|
|
};
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 23:13:26 +00:00
|
|
|
if (this.props.params) {
|
|
|
|
this.state.postForm.name = this.props.params.name;
|
|
|
|
if (this.props.params.url) {
|
|
|
|
this.state.postForm.url = this.props.params.url;
|
|
|
|
}
|
|
|
|
if (this.props.params.body) {
|
|
|
|
this.state.postForm.body = this.props.params.body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
this.subscription = WebSocketService.Instance.subject
|
2020-01-06 16:22:51 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
2019-10-19 00:20:27 +00:00
|
|
|
.subscribe(
|
|
|
|
msg => this.parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2019-04-03 20:59:37 +00:00
|
|
|
() => console.log('complete')
|
2019-10-19 00:20:27 +00:00
|
|
|
);
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-08-22 23:13:26 +00:00
|
|
|
let listCommunitiesForm: ListCommunitiesForm = {
|
|
|
|
sort: SortType[SortType.TopAll],
|
|
|
|
limit: 9999,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-10 06:19:12 +00:00
|
|
|
|
2019-08-22 23:13:26 +00:00
|
|
|
WebSocketService.Instance.listCommunities(listCommunitiesForm);
|
2020-01-03 18:52:21 +00:00
|
|
|
WebSocketService.Instance.getSite();
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 21:46:09 +00:00
|
|
|
componentDidMount() {
|
2020-01-24 18:59:50 +00:00
|
|
|
var textarea: any = document.getElementById(this.id);
|
|
|
|
autosize(textarea);
|
|
|
|
this.tribute.attach(textarea);
|
|
|
|
textarea.addEventListener('tribute-replaced', () => {
|
|
|
|
this.state.postForm.body = textarea.value;
|
|
|
|
this.setState(this.state);
|
|
|
|
autosize.update(textarea);
|
|
|
|
});
|
2019-04-08 21:46:09 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={linkEvent(this, this.handlePostSubmit)}>
|
|
|
|
<div class="form-group row">
|
2020-02-02 18:50:44 +00:00
|
|
|
<label class="col-sm-2 col-form-label" htmlFor="post-url">
|
|
|
|
{i18n.t('url')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
2019-04-03 20:59:37 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<input
|
|
|
|
type="url"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="post-url"
|
2019-10-19 00:20:27 +00:00
|
|
|
class="form-control"
|
2019-12-29 00:06:19 +00:00
|
|
|
value={this.state.postForm.url}
|
2019-10-19 00:20:27 +00:00
|
|
|
onInput={linkEvent(this, this.handlePostUrlChange)}
|
2020-01-28 02:59:38 +00:00
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
2019-10-19 00:20:27 +00:00
|
|
|
/>
|
|
|
|
{this.state.suggestedTitle && (
|
|
|
|
<div
|
|
|
|
class="mt-1 text-muted small font-weight-bold pointer"
|
|
|
|
onClick={linkEvent(this, this.copySuggestedTitle)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('copy_suggested_title', {
|
|
|
|
title: this.state.suggestedTitle,
|
|
|
|
})}
|
2019-10-19 00:20:27 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2019-09-08 03:42:01 +00:00
|
|
|
<form>
|
2019-10-19 00:20:27 +00:00
|
|
|
<label
|
|
|
|
htmlFor="file-upload"
|
|
|
|
className={`${UserService.Instance.user &&
|
|
|
|
'pointer'} d-inline-block mr-2 float-right text-muted small font-weight-bold`}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('upload_image')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
id="file-upload"
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name="file"
|
|
|
|
class="d-none"
|
|
|
|
disabled={!UserService.Instance.user}
|
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
|
|
|
/>
|
2019-09-08 03:42:01 +00:00
|
|
|
</form>
|
2019-11-22 02:14:59 +00:00
|
|
|
{validURL(this.state.postForm.url) && (
|
|
|
|
<a
|
|
|
|
href={`${archiveUrl}/?run=1&url=${encodeURIComponent(
|
|
|
|
this.state.postForm.url
|
|
|
|
)}`}
|
|
|
|
target="_blank"
|
|
|
|
class="mr-2 d-inline-block float-right text-muted small font-weight-bold"
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('archive_link')}
|
2019-11-22 02:14:59 +00:00
|
|
|
</a>
|
|
|
|
)}
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.imageLoading && (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
)}
|
2019-12-29 21:56:55 +00:00
|
|
|
{isImage(this.state.postForm.url) && (
|
|
|
|
<img src={this.state.postForm.url} class="img-fluid" />
|
|
|
|
)}
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.crossPosts.length > 0 && (
|
2019-08-22 05:17:15 +00:00
|
|
|
<>
|
2019-10-19 00:20:27 +00:00
|
|
|
<div class="my-1 text-muted small font-weight-bold">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('cross_posts')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</div>
|
2019-08-22 05:17:15 +00:00
|
|
|
<PostListings showCommunity posts={this.state.crossPosts} />
|
|
|
|
</>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-04-03 20:59:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
2020-02-02 18:50:44 +00:00
|
|
|
<label class="col-sm-2 col-form-label" htmlFor="post-title">
|
|
|
|
{i18n.t('title')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
2019-04-03 20:59:37 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<textarea
|
2019-12-15 19:29:50 +00:00
|
|
|
value={this.state.postForm.name}
|
2020-01-31 20:52:27 +00:00
|
|
|
id="post-title"
|
2019-10-19 00:20:27 +00:00
|
|
|
onInput={linkEvent(this, this.handlePostNameChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
rows={2}
|
|
|
|
minLength={3}
|
|
|
|
maxLength={100}
|
|
|
|
/>
|
|
|
|
{this.state.suggestedPosts.length > 0 && (
|
2019-07-29 01:57:09 +00:00
|
|
|
<>
|
2019-10-19 00:20:27 +00:00
|
|
|
<div class="my-1 text-muted small font-weight-bold">
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('related_posts')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</div>
|
2019-07-29 01:57:09 +00:00
|
|
|
<PostListings posts={this.state.suggestedPosts} />
|
|
|
|
</>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2019-04-03 20:59:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-01-31 20:52:27 +00:00
|
|
|
|
2019-04-03 20:59:37 +00:00
|
|
|
<div class="form-group row">
|
2020-02-02 18:50:44 +00:00
|
|
|
<label class="col-sm-2 col-form-label" htmlFor={this.id}>
|
|
|
|
{i18n.t('body')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
2019-04-03 20:59:37 +00:00
|
|
|
<div class="col-sm-10">
|
2019-10-19 00:20:27 +00:00
|
|
|
<textarea
|
2020-01-24 18:59:50 +00:00
|
|
|
id={this.id}
|
2019-10-19 00:20:27 +00:00
|
|
|
value={this.state.postForm.body}
|
|
|
|
onInput={linkEvent(this, this.handlePostBodyChange)}
|
|
|
|
className={`form-control ${this.state.previewMode && 'd-none'}`}
|
|
|
|
rows={4}
|
|
|
|
maxLength={10000}
|
|
|
|
/>
|
|
|
|
{this.state.previewMode && (
|
|
|
|
<div
|
|
|
|
className="md-div"
|
|
|
|
dangerouslySetInnerHTML={mdToHtml(this.state.postForm.body)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{this.state.postForm.body && (
|
|
|
|
<button
|
|
|
|
className={`mt-1 mr-2 btn btn-sm btn-secondary ${this.state
|
|
|
|
.previewMode && 'active'}`}
|
|
|
|
onClick={linkEvent(this, this.handlePreviewToggle)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('preview')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<a
|
|
|
|
href={markdownHelpUrl}
|
|
|
|
target="_blank"
|
|
|
|
class="d-inline-block float-right text-muted small font-weight-bold"
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('formatting_help')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</a>
|
2019-04-03 20:59:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
{!this.props.post && (
|
2019-04-16 23:04:23 +00:00
|
|
|
<div class="form-group row">
|
2020-02-02 18:50:44 +00:00
|
|
|
<label class="col-sm-2 col-form-label" htmlFor="post-community">
|
|
|
|
{i18n.t('community')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<select
|
|
|
|
class="form-control"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="post-community"
|
2019-10-19 00:20:27 +00:00
|
|
|
value={this.state.postForm.community_id}
|
|
|
|
onInput={linkEvent(this, this.handlePostCommunityChange)}
|
|
|
|
>
|
|
|
|
{this.state.communities.map(community => (
|
|
|
|
<option value={community.id}>{community.name}</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
2019-04-16 23:04:23 +00:00
|
|
|
</div>
|
2019-10-19 00:20:27 +00:00
|
|
|
)}
|
2020-01-03 18:52:21 +00:00
|
|
|
{this.state.enable_nsfw && (
|
2019-12-11 20:21:47 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
2020-01-31 20:52:27 +00:00
|
|
|
id="post-nsfw"
|
2019-12-11 20:21:47 +00:00
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.postForm.nsfw}
|
|
|
|
onChange={linkEvent(this, this.handlePostNsfwChange)}
|
|
|
|
/>
|
2020-02-02 18:50:44 +00:00
|
|
|
<label class="form-check-label" htmlFor="post-nsfw">
|
|
|
|
{i18n.t('nsfw')}
|
2019-12-11 20:21:47 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
2019-08-14 02:52:43 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-12-11 20:21:47 +00:00
|
|
|
)}
|
2019-04-03 20:59:37 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-10">
|
2019-04-08 21:46:09 +00:00
|
|
|
<button type="submit" class="btn btn-secondary mr-2">
|
2019-10-19 00:20:27 +00:00
|
|
|
{this.state.loading ? (
|
|
|
|
<svg class="icon icon-spinner spin">
|
|
|
|
<use xlinkHref="#icon-spinner"></use>
|
|
|
|
</svg>
|
|
|
|
) : this.props.post ? (
|
|
|
|
capitalizeFirstLetter(i18n.t('save'))
|
|
|
|
) : (
|
|
|
|
capitalizeFirstLetter(i18n.t('create'))
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
{this.props.post && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.handleCancel)}
|
|
|
|
>
|
2020-02-02 18:50:44 +00:00
|
|
|
{i18n.t('cancel')}
|
2019-10-19 00:20:27 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2019-04-03 20:59:37 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostSubmit(i: PostForm, event: any) {
|
2019-04-03 20:59:37 +00:00
|
|
|
event.preventDefault();
|
|
|
|
if (i.props.post) {
|
|
|
|
WebSocketService.Instance.editPost(i.state.postForm);
|
|
|
|
} else {
|
|
|
|
WebSocketService.Instance.createPost(i.state.postForm);
|
|
|
|
}
|
2019-04-08 21:46:09 +00:00
|
|
|
i.state.loading = true;
|
|
|
|
i.setState(i.state);
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:45:50 +00:00
|
|
|
copySuggestedTitle(i: PostForm) {
|
|
|
|
i.state.postForm.name = i.state.suggestedTitle;
|
|
|
|
i.state.suggestedTitle = undefined;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-12-29 00:06:19 +00:00
|
|
|
handlePostUrlChange(i: PostForm, event: any) {
|
2019-04-03 20:59:37 +00:00
|
|
|
i.state.postForm.url = event.target.value;
|
2020-01-01 16:09:56 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
i.fetchPageTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchPageTitle() {
|
|
|
|
if (validURL(this.state.postForm.url)) {
|
2019-08-22 05:17:15 +00:00
|
|
|
let form: SearchForm = {
|
2020-01-01 16:09:56 +00:00
|
|
|
q: this.state.postForm.url,
|
2019-08-22 05:17:15 +00:00
|
|
|
type_: SearchType[SearchType.Url],
|
|
|
|
sort: SortType[SortType.TopAll],
|
|
|
|
page: 1,
|
|
|
|
limit: 6,
|
|
|
|
};
|
|
|
|
|
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
|
|
|
|
// Fetch the page title
|
2020-01-01 16:09:56 +00:00
|
|
|
getPageTitle(this.state.postForm.url).then(d => {
|
|
|
|
this.state.suggestedTitle = d;
|
|
|
|
this.setState(this.state);
|
2019-08-17 01:46:39 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-01-01 16:09:56 +00:00
|
|
|
this.state.suggestedTitle = undefined;
|
|
|
|
this.state.crossPosts = [];
|
2019-08-17 01:46:39 +00:00
|
|
|
}
|
2019-12-29 00:06:19 +00:00
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-12-29 00:06:19 +00:00
|
|
|
handlePostNameChange(i: PostForm, event: any) {
|
2019-04-03 20:59:37 +00:00
|
|
|
i.state.postForm.name = event.target.value;
|
2020-01-01 16:09:56 +00:00
|
|
|
i.setState(i.state);
|
|
|
|
i.fetchSimilarPosts();
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchSimilarPosts() {
|
2019-07-29 01:57:09 +00:00
|
|
|
let form: SearchForm = {
|
2020-01-01 16:09:56 +00:00
|
|
|
q: this.state.postForm.name,
|
2019-07-29 01:57:09 +00:00
|
|
|
type_: SearchType[SearchType.Posts],
|
|
|
|
sort: SortType[SortType.TopAll],
|
2020-01-01 16:09:56 +00:00
|
|
|
community_id: this.state.postForm.community_id,
|
2019-07-29 01:57:09 +00:00
|
|
|
page: 1,
|
|
|
|
limit: 6,
|
|
|
|
};
|
|
|
|
|
2020-01-01 16:09:56 +00:00
|
|
|
if (this.state.postForm.name !== '') {
|
2019-07-29 01:57:09 +00:00
|
|
|
WebSocketService.Instance.search(form);
|
|
|
|
} else {
|
2020-01-01 16:09:56 +00:00
|
|
|
this.state.suggestedPosts = [];
|
2019-07-29 01:57:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-01 16:09:56 +00:00
|
|
|
this.setState(this.state);
|
2019-12-29 00:06:19 +00:00
|
|
|
}
|
2019-04-03 20:59:37 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostBodyChange(i: PostForm, event: any) {
|
2019-04-03 20:59:37 +00:00
|
|
|
i.state.postForm.body = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handlePostCommunityChange(i: PostForm, event: any) {
|
2019-04-03 20:59:37 +00:00
|
|
|
i.state.postForm.community_id = Number(event.target.value);
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
handlePostNsfwChange(i: PostForm, event: any) {
|
|
|
|
i.state.postForm.nsfw = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
handleCancel(i: PostForm) {
|
2019-04-05 02:08:21 +00:00
|
|
|
i.props.onCancel();
|
|
|
|
}
|
|
|
|
|
2019-09-01 04:10:48 +00:00
|
|
|
handlePreviewToggle(i: PostForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.previewMode = !i.state.previewMode;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:59:38 +00:00
|
|
|
handleImageUploadPaste(i: PostForm, event: any) {
|
|
|
|
let image = event.clipboardData.files[0];
|
|
|
|
if (image) {
|
|
|
|
i.handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 03:42:01 +00:00
|
|
|
handleImageUpload(i: PostForm, event: any) {
|
2020-01-28 02:59:38 +00:00
|
|
|
let file: any;
|
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
file = event.target.files[0];
|
|
|
|
} else {
|
|
|
|
file = event;
|
|
|
|
}
|
|
|
|
|
2019-09-08 03:42:01 +00:00
|
|
|
const imageUploadUrl = `/pictshare/api/upload.php`;
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file);
|
2019-09-08 16:54:53 +00:00
|
|
|
|
|
|
|
i.state.imageLoading = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
|
2019-09-08 03:42:01 +00:00
|
|
|
fetch(imageUploadUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body: formData,
|
|
|
|
})
|
2019-10-19 00:20:27 +00:00
|
|
|
.then(res => res.json())
|
|
|
|
.then(res => {
|
|
|
|
let url = `${window.location.origin}/pictshare/${res.url}`;
|
|
|
|
if (res.filetype == 'mp4') {
|
|
|
|
url += '/raw';
|
|
|
|
}
|
|
|
|
i.state.postForm.url = url;
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
i.state.imageLoading = false;
|
|
|
|
i.setState(i.state);
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(error, 'danger');
|
2019-10-19 00:20:27 +00:00
|
|
|
});
|
2019-09-08 03:42:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 05:38:45 +00:00
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let res = wsJsonToRes(msg);
|
2020-01-25 14:58:53 +00:00
|
|
|
if (msg.error) {
|
2020-01-23 03:29:11 +00:00
|
|
|
toast(i18n.t(msg.error), 'danger');
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2019-04-09 18:35:16 +00:00
|
|
|
this.setState(this.state);
|
2019-04-03 20:59:37 +00:00
|
|
|
return;
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.ListCommunities) {
|
|
|
|
let data = res.data as ListCommunitiesResponse;
|
|
|
|
this.state.communities = data.communities;
|
2019-04-03 20:59:37 +00:00
|
|
|
if (this.props.post) {
|
|
|
|
this.state.postForm.community_id = this.props.post.community_id;
|
2019-08-22 23:13:26 +00:00
|
|
|
} else if (this.props.params && this.props.params.community) {
|
2020-01-19 04:54:10 +00:00
|
|
|
let foundCommunityId = data.communities.find(
|
2019-10-19 00:20:27 +00:00
|
|
|
r => r.name == this.props.params.community
|
|
|
|
).id;
|
2019-04-29 16:07:41 +00:00
|
|
|
this.state.postForm.community_id = foundCommunityId;
|
2019-04-03 20:59:37 +00:00
|
|
|
} else {
|
2020-01-19 04:54:10 +00:00
|
|
|
this.state.postForm.community_id = data.communities[0].id;
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.CreatePost) {
|
|
|
|
let data = res.data as PostResponse;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2020-01-19 04:54:10 +00:00
|
|
|
this.props.onCreate(data.post.id);
|
|
|
|
} else if (res.op == UserOperation.EditPost) {
|
|
|
|
let data = res.data as PostResponse;
|
2019-04-08 21:46:09 +00:00
|
|
|
this.state.loading = false;
|
2020-01-19 04:54:10 +00:00
|
|
|
this.props.onEdit(data.post);
|
|
|
|
} else if (res.op == UserOperation.Search) {
|
|
|
|
let data = res.data as SearchResponse;
|
|
|
|
|
|
|
|
if (data.type_ == SearchType[SearchType.Posts]) {
|
|
|
|
this.state.suggestedPosts = data.posts;
|
|
|
|
} else if (data.type_ == SearchType[SearchType.Url]) {
|
|
|
|
this.state.crossPosts = data.posts;
|
2019-08-22 05:17:15 +00:00
|
|
|
}
|
2019-07-29 01:57:09 +00:00
|
|
|
this.setState(this.state);
|
2020-01-19 04:54:10 +00:00
|
|
|
} else if (res.op == UserOperation.GetSite) {
|
|
|
|
let data = res.data as GetSiteResponse;
|
|
|
|
this.state.enable_nsfw = data.site.enable_nsfw;
|
2020-01-03 18:52:21 +00:00
|
|
|
this.setState(this.state);
|
2019-04-03 20:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|