2023-06-20 14:21:31 +00:00
|
|
|
import { debounce } from "@utils/helpers";
|
2021-07-17 20:42:55 +00:00
|
|
|
import autosize from "autosize";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { Component, InfernoNode, linkEvent } from "inferno";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-06-14 12:20:40 +00:00
|
|
|
CommunityView,
|
2020-12-24 01:58:27 +00:00
|
|
|
CreatePost,
|
|
|
|
EditPost,
|
2023-06-14 12:20:40 +00:00
|
|
|
GetSiteMetadataResponse,
|
2022-09-22 15:14:58 +00:00
|
|
|
Language,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostView,
|
2020-09-06 16:15:25 +00:00
|
|
|
SearchResponse,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { i18n } from "../../i18next";
|
|
|
|
import { PostFormParams } from "../../interfaces";
|
2023-06-14 12:20:40 +00:00
|
|
|
import { UserService } from "../../services";
|
|
|
|
import { HttpService, RequestState } from "../../services/HttpService";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
2023-06-08 19:08:28 +00:00
|
|
|
Choice,
|
2021-10-29 01:58:49 +00:00
|
|
|
archiveTodayUrl,
|
2021-07-17 20:42:55 +00:00
|
|
|
capitalizeFirstLetter,
|
|
|
|
communityToChoice,
|
|
|
|
fetchCommunities,
|
2023-04-15 14:47:10 +00:00
|
|
|
getIdFromString,
|
2021-10-29 01:58:49 +00:00
|
|
|
ghostArchiveUrl,
|
2020-09-06 16:15:25 +00:00
|
|
|
isImage,
|
2023-01-04 16:56:24 +00:00
|
|
|
myAuth,
|
2023-06-14 12:20:40 +00:00
|
|
|
myAuthRequired,
|
2022-02-24 15:31:44 +00:00
|
|
|
relTags,
|
2021-07-17 20:42:55 +00:00
|
|
|
setupTippy,
|
|
|
|
toast,
|
2022-06-21 21:42:29 +00:00
|
|
|
trendingFetchLimit,
|
2020-09-06 16:15:25 +00:00
|
|
|
validTitle,
|
2021-07-17 20:42:55 +00:00
|
|
|
validURL,
|
2021-10-29 01:58:49 +00:00
|
|
|
webArchiveUrl,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { Icon, Spinner } from "../common/icon";
|
2022-09-22 15:14:58 +00:00
|
|
|
import { LanguageSelect } from "../common/language-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { MarkdownTextArea } from "../common/markdown-textarea";
|
2023-06-13 10:33:27 +00:00
|
|
|
import NavigationPrompt from "../common/navigation-prompt";
|
2023-04-15 14:47:10 +00:00
|
|
|
import { SearchableSelect } from "../common/searchable-select";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { PostListings } from "./post-listings";
|
2020-09-07 22:24:48 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
const MAX_POST_TITLE_LENGTH = 200;
|
|
|
|
|
|
|
|
interface PostFormProps {
|
2023-01-04 16:56:24 +00:00
|
|
|
post_view?: PostView; // If a post is given, that means this is an edit
|
2023-06-14 12:20:40 +00:00
|
|
|
crossPosts?: PostView[];
|
2022-09-22 15:14:58 +00:00
|
|
|
allLanguages: Language[];
|
2022-12-19 15:57:29 +00:00
|
|
|
siteLanguages: number[];
|
2023-01-04 16:56:24 +00:00
|
|
|
params?: PostFormParams;
|
2023-06-14 12:20:40 +00:00
|
|
|
onCancel?(): void;
|
|
|
|
onCreate?(form: CreatePost): void;
|
|
|
|
onEdit?(form: EditPost): void;
|
2022-06-21 21:42:29 +00:00
|
|
|
enableNsfw?: boolean;
|
|
|
|
enableDownvotes?: boolean;
|
2023-04-15 14:47:10 +00:00
|
|
|
selectedCommunityChoice?: Choice;
|
|
|
|
onSelectCommunity?: (choice: Choice) => void;
|
2023-06-14 12:20:40 +00:00
|
|
|
initialCommunities?: CommunityView[];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface PostFormState {
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
name?: string;
|
|
|
|
url?: string;
|
|
|
|
body?: string;
|
|
|
|
nsfw?: boolean;
|
|
|
|
language_id?: number;
|
|
|
|
community_id?: number;
|
|
|
|
honeypot?: string;
|
|
|
|
};
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
2023-06-14 12:20:40 +00:00
|
|
|
suggestedPostsRes: RequestState<SearchResponse>;
|
|
|
|
metadataRes: RequestState<GetSiteMetadataResponse>;
|
2020-09-06 16:15:25 +00:00
|
|
|
imageLoading: boolean;
|
2023-06-14 20:29:04 +00:00
|
|
|
imageDeleteUrl: string;
|
2022-09-22 18:13:22 +00:00
|
|
|
communitySearchLoading: boolean;
|
2023-04-15 14:47:10 +00:00
|
|
|
communitySearchOptions: Choice[];
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: boolean;
|
2023-06-14 12:20:40 +00:00
|
|
|
submitted: boolean;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PostForm extends Component<PostFormProps, PostFormState> {
|
2023-01-04 16:56:24 +00:00
|
|
|
state: PostFormState = {
|
2023-06-14 12:20:40 +00:00
|
|
|
suggestedPostsRes: { state: "empty" },
|
|
|
|
metadataRes: { state: "empty" },
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {},
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: false,
|
|
|
|
imageLoading: false,
|
2023-06-14 20:29:04 +00:00
|
|
|
imageDeleteUrl: "",
|
2022-09-22 18:13:22 +00:00
|
|
|
communitySearchLoading: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
previewMode: false,
|
2023-04-15 14:47:10 +00:00
|
|
|
communitySearchOptions: [],
|
2023-06-14 12:20:40 +00:00
|
|
|
submitted: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
constructor(props: PostFormProps, context: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
super(props, context);
|
2021-04-07 16:46:12 +00:00
|
|
|
this.fetchSimilarPosts = debounce(this.fetchSimilarPosts.bind(this));
|
|
|
|
this.fetchPageTitle = debounce(this.fetchPageTitle.bind(this));
|
2020-09-06 16:15:25 +00:00
|
|
|
this.handlePostBodyChange = this.handlePostBodyChange.bind(this);
|
2022-09-22 15:14:58 +00:00
|
|
|
this.handleLanguageChange = this.handleLanguageChange.bind(this);
|
2023-04-15 14:47:10 +00:00
|
|
|
this.handleCommunitySelect = this.handleCommunitySelect.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
const { post_view, selectedCommunityChoice, params } = this.props;
|
2022-09-22 15:03:35 +00:00
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
// Means its an edit
|
2023-06-14 12:20:40 +00:00
|
|
|
if (post_view) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
2023-06-14 12:20:40 +00:00
|
|
|
body: post_view.post.body,
|
|
|
|
name: post_view.post.name,
|
|
|
|
community_id: post_view.community.id,
|
|
|
|
url: post_view.post.url,
|
|
|
|
nsfw: post_view.post.nsfw,
|
|
|
|
language_id: post_view.post.language_id,
|
2023-01-04 16:56:24 +00:00
|
|
|
},
|
2022-09-22 15:03:35 +00:00
|
|
|
};
|
2023-06-14 12:20:40 +00:00
|
|
|
} else if (selectedCommunityChoice) {
|
2023-04-15 14:47:10 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
form: {
|
|
|
|
...this.state.form,
|
|
|
|
community_id: getIdFromString(selectedCommunityChoice.value),
|
|
|
|
},
|
2023-06-14 12:20:40 +00:00
|
|
|
communitySearchOptions: [selectedCommunityChoice]
|
|
|
|
.concat(
|
|
|
|
this.props.initialCommunities?.map(
|
|
|
|
({ community: { id, title } }) => ({
|
|
|
|
label: title,
|
|
|
|
value: id.toString(),
|
|
|
|
})
|
|
|
|
) ?? []
|
|
|
|
)
|
|
|
|
.filter(option => option.value !== selectedCommunityChoice.value),
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this.state = {
|
|
|
|
...this.state,
|
|
|
|
communitySearchOptions:
|
|
|
|
this.props.initialCommunities?.map(
|
|
|
|
({ community: { id, title } }) => ({
|
|
|
|
label: title,
|
|
|
|
value: id.toString(),
|
|
|
|
})
|
|
|
|
) ?? [],
|
2023-04-15 14:47:10 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
if (params) {
|
2022-09-22 15:03:35 +00:00
|
|
|
this.state = {
|
|
|
|
...this.state,
|
2023-01-04 16:56:24 +00:00
|
|
|
form: {
|
|
|
|
...this.state.form,
|
2023-04-15 14:47:10 +00:00
|
|
|
...params,
|
2022-09-22 15:03:35 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
setupTippy();
|
2023-04-15 14:47:10 +00:00
|
|
|
const textarea: any = document.getElementById("post-title");
|
|
|
|
|
2021-01-27 15:29:01 +00:00
|
|
|
if (textarea) {
|
|
|
|
autosize(textarea);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
componentWillReceiveProps(
|
|
|
|
nextProps: Readonly<{ children?: InfernoNode } & PostFormProps>
|
|
|
|
): void {
|
|
|
|
if (this.props != nextProps) {
|
|
|
|
this.setState(
|
|
|
|
s => (
|
|
|
|
(s.form.community_id = getIdFromString(
|
|
|
|
nextProps.selectedCommunityChoice?.value
|
|
|
|
)),
|
|
|
|
s
|
|
|
|
)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const firstLang = this.state.form.language_id;
|
|
|
|
const selectedLangs = firstLang ? Array.of(firstLang) : undefined;
|
2022-09-22 15:14:58 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const url = this.state.form.url;
|
2023-06-14 12:20:40 +00:00
|
|
|
|
|
|
|
// TODO
|
|
|
|
// const promptCheck =
|
|
|
|
// !!this.state.form.name || !!this.state.form.url || !!this.state.form.body;
|
|
|
|
// <Prompt when={promptCheck} message={i18n.t("block_leaving")} />
|
2020-09-06 16:15:25 +00:00
|
|
|
return (
|
2023-06-20 18:46:16 +00:00
|
|
|
<form
|
|
|
|
className="post-form"
|
|
|
|
onSubmit={linkEvent(this, this.handlePostSubmit)}
|
|
|
|
>
|
2023-06-13 10:33:27 +00:00
|
|
|
<NavigationPrompt
|
2020-09-06 16:15:25 +00:00
|
|
|
when={
|
2023-06-13 10:33:27 +00:00
|
|
|
!!(
|
|
|
|
this.state.form.name ||
|
2023-01-04 16:56:24 +00:00
|
|
|
this.state.form.url ||
|
2023-06-13 10:33:27 +00:00
|
|
|
this.state.form.body
|
2023-06-14 12:20:40 +00:00
|
|
|
) && !this.state.submitted
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
/>
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-url">
|
|
|
|
{i18n.t("url")}
|
|
|
|
</label>
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<input
|
|
|
|
type="url"
|
|
|
|
id="post-url"
|
|
|
|
className="form-control"
|
|
|
|
value={this.state.form.url}
|
|
|
|
onInput={linkEvent(this, this.handlePostUrlChange)}
|
|
|
|
onPaste={linkEvent(this, this.handleImageUploadPaste)}
|
|
|
|
/>
|
|
|
|
{this.renderSuggestedTitleCopy()}
|
|
|
|
<form>
|
|
|
|
<label
|
|
|
|
htmlFor="file-upload"
|
|
|
|
className={`${
|
|
|
|
UserService.Instance.myUserInfo && "pointer"
|
|
|
|
} d-inline-block float-right text-muted font-weight-bold`}
|
|
|
|
data-tippy-content={i18n.t("upload_image")}
|
|
|
|
>
|
|
|
|
<Icon icon="image" classes="icon-inline" />
|
|
|
|
</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<input
|
2023-06-14 12:20:40 +00:00
|
|
|
id="file-upload"
|
|
|
|
type="file"
|
|
|
|
accept="image/*,video/*"
|
|
|
|
name="file"
|
|
|
|
className="d-none"
|
|
|
|
disabled={!UserService.Instance.myUserInfo}
|
|
|
|
onChange={linkEvent(this, this.handleImageUpload)}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
2023-06-14 12:20:40 +00:00
|
|
|
</form>
|
|
|
|
{url && validURL(url) && (
|
|
|
|
<div>
|
|
|
|
<a
|
|
|
|
href={`${webArchiveUrl}/save/${encodeURIComponent(url)}`}
|
2023-06-20 12:01:29 +00:00
|
|
|
className="me-2 d-inline-block float-right text-muted small font-weight-bold"
|
2023-06-14 12:20:40 +00:00
|
|
|
rel={relTags}
|
2023-01-04 16:56:24 +00:00
|
|
|
>
|
2023-06-14 12:20:40 +00:00
|
|
|
archive.org {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${ghostArchiveUrl}/search?term=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2023-06-20 12:01:29 +00:00
|
|
|
className="me-2 d-inline-block float-right text-muted small font-weight-bold"
|
2023-06-14 12:20:40 +00:00
|
|
|
rel={relTags}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2023-06-14 12:20:40 +00:00
|
|
|
ghostarchive.org {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
<a
|
|
|
|
href={`${archiveTodayUrl}/?run=1&url=${encodeURIComponent(
|
|
|
|
url
|
|
|
|
)}`}
|
2023-06-20 12:01:29 +00:00
|
|
|
className="me-2 d-inline-block float-right text-muted small font-weight-bold"
|
2023-06-14 12:20:40 +00:00
|
|
|
rel={relTags}
|
|
|
|
>
|
|
|
|
archive.today {i18n.t("archive_link")}
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{this.state.imageLoading && <Spinner />}
|
|
|
|
{url && isImage(url) && (
|
|
|
|
<img src={url} className="img-fluid" alt="" />
|
|
|
|
)}
|
2023-06-14 20:29:04 +00:00
|
|
|
{this.state.imageDeleteUrl && (
|
|
|
|
<button
|
2023-06-14 20:45:56 +00:00
|
|
|
className="btn btn-danger btn-sm mt-2"
|
2023-06-14 20:29:04 +00:00
|
|
|
onClick={linkEvent(this, this.handleImageDelete)}
|
|
|
|
aria-label={i18n.t("delete")}
|
|
|
|
data-tippy-content={i18n.t("delete")}
|
|
|
|
>
|
2023-06-20 12:01:29 +00:00
|
|
|
<Icon icon="x" classes="icon-inline me-1" />
|
2023-06-14 20:29:04 +00:00
|
|
|
{capitalizeFirstLetter(i18n.t("delete"))}
|
|
|
|
</button>
|
|
|
|
)}
|
2023-06-14 12:20:40 +00:00
|
|
|
{this.props.crossPosts && this.props.crossPosts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
|
|
|
{i18n.t("cross_posts")}
|
2023-01-04 16:56:24 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={this.props.crossPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
viewOnly
|
|
|
|
// All of these are unused, since its view only
|
|
|
|
onPostEdit={() => {}}
|
|
|
|
onPostVote={() => {}}
|
|
|
|
onPostReport={() => {}}
|
|
|
|
onBlockPerson={() => {}}
|
|
|
|
onLockPost={() => {}}
|
|
|
|
onDeletePost={() => {}}
|
|
|
|
onRemovePost={() => {}}
|
|
|
|
onSavePost={() => {}}
|
|
|
|
onFeaturePost={() => {}}
|
|
|
|
onPurgePerson={() => {}}
|
|
|
|
onPurgePost={() => {}}
|
|
|
|
onBanPersonFromCommunity={() => {}}
|
|
|
|
onBanPerson={() => {}}
|
|
|
|
onAddModToCommunity={() => {}}
|
|
|
|
onAddAdmin={() => {}}
|
|
|
|
onTransferCommunity={() => {}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-title">
|
|
|
|
{i18n.t("title")}
|
|
|
|
</label>
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<textarea
|
|
|
|
value={this.state.form.name}
|
|
|
|
id="post-title"
|
|
|
|
onInput={linkEvent(this, this.handlePostNameChange)}
|
|
|
|
className={`form-control ${
|
|
|
|
!validTitle(this.state.form.name) && "is-invalid"
|
|
|
|
}`}
|
|
|
|
required
|
|
|
|
rows={1}
|
|
|
|
minLength={3}
|
|
|
|
maxLength={MAX_POST_TITLE_LENGTH}
|
|
|
|
/>
|
|
|
|
{!validTitle(this.state.form.name) && (
|
|
|
|
<div className="invalid-feedback">
|
|
|
|
{i18n.t("invalid_post_title")}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{this.renderSuggestedPosts()}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label">{i18n.t("body")}</label>
|
|
|
|
<div className="col-sm-10">
|
|
|
|
<MarkdownTextArea
|
|
|
|
initialContent={this.state.form.body}
|
|
|
|
onContentChange={this.handlePostBodyChange}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
hideNavigationWarnings
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{!this.props.post_view && (
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<label className="col-sm-2 col-form-label" htmlFor="post-community">
|
|
|
|
{i18n.t("community")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2023-06-14 12:20:40 +00:00
|
|
|
<SearchableSelect
|
|
|
|
id="post-community"
|
|
|
|
value={this.state.form.community_id}
|
|
|
|
options={[
|
|
|
|
{
|
|
|
|
label: i18n.t("select_a_community"),
|
|
|
|
value: "",
|
|
|
|
disabled: true,
|
|
|
|
} as Choice,
|
|
|
|
].concat(this.state.communitySearchOptions)}
|
|
|
|
loading={this.state.communitySearchLoading}
|
|
|
|
onChange={this.handleCommunitySelect}
|
|
|
|
onSearch={this.handleCommunitySearch}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
)}
|
|
|
|
{this.props.enableNsfw && (
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<legend className="col-form-label col-sm-2 pt-0">
|
|
|
|
{i18n.t("nsfw")}
|
|
|
|
</legend>
|
2022-09-22 15:03:35 +00:00
|
|
|
<div className="col-sm-10">
|
2023-06-14 12:20:40 +00:00
|
|
|
<div className="form-check">
|
|
|
|
<input
|
|
|
|
className="form-check-input position-static"
|
|
|
|
id="post-nsfw"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.form.nsfw}
|
|
|
|
onChange={linkEvent(this, this.handlePostNsfwChange)}
|
2023-04-15 14:47:10 +00:00
|
|
|
/>
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<LanguageSelect
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
selectedLanguageIds={selectedLangs}
|
|
|
|
multiple={false}
|
|
|
|
onChange={this.handleLanguageChange}
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
tabIndex={-1}
|
|
|
|
autoComplete="false"
|
|
|
|
name="a_password"
|
|
|
|
type="text"
|
|
|
|
className="form-control honeypot"
|
|
|
|
id="register-honey"
|
|
|
|
value={this.state.form.honeypot}
|
|
|
|
onInput={linkEvent(this, this.handleHoneyPotChange)}
|
|
|
|
/>
|
2023-06-20 12:01:29 +00:00
|
|
|
<div className="mb-3 row">
|
2023-06-14 12:20:40 +00:00
|
|
|
<div className="col-sm-10">
|
|
|
|
<button
|
|
|
|
disabled={!this.state.form.community_id || this.state.loading}
|
|
|
|
type="submit"
|
2023-06-20 12:01:29 +00:00
|
|
|
className="btn btn-secondary me-2"
|
2023-06-14 12:20:40 +00:00
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
|
|
|
<Spinner />
|
|
|
|
) : this.props.post_view ? (
|
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
|
|
|
) : (
|
|
|
|
capitalizeFirstLetter(i18n.t("create"))
|
|
|
|
)}
|
|
|
|
</button>
|
|
|
|
{this.props.post_view && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
2023-06-14 12:20:40 +00:00
|
|
|
type="button"
|
|
|
|
className="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.handleCancel)}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2023-06-14 12:20:40 +00:00
|
|
|
{i18n.t("cancel")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
2023-06-14 12:20:40 +00:00
|
|
|
)}
|
2020-09-06 16:15:25 +00:00
|
|
|
</div>
|
2023-06-14 12:20:40 +00:00
|
|
|
</div>
|
|
|
|
</form>
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
renderSuggestedTitleCopy() {
|
|
|
|
switch (this.state.metadataRes.state) {
|
|
|
|
case "loading":
|
|
|
|
return <Spinner />;
|
|
|
|
case "success": {
|
|
|
|
const suggestedTitle = this.state.metadataRes.data.metadata.title;
|
|
|
|
|
|
|
|
return (
|
|
|
|
suggestedTitle && (
|
|
|
|
<div
|
|
|
|
className="mt-1 text-muted small font-weight-bold pointer"
|
|
|
|
role="button"
|
|
|
|
onClick={linkEvent(
|
|
|
|
{ i: this, suggestedTitle },
|
|
|
|
this.copySuggestedTitle
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{i18n.t("copy_suggested_title", { title: "" })} {suggestedTitle}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
renderSuggestedPosts() {
|
|
|
|
switch (this.state.suggestedPostsRes.state) {
|
|
|
|
case "loading":
|
|
|
|
return <Spinner />;
|
|
|
|
case "success": {
|
|
|
|
const suggestedPosts = this.state.suggestedPostsRes.data.posts;
|
|
|
|
|
|
|
|
return (
|
|
|
|
suggestedPosts &&
|
|
|
|
suggestedPosts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<div className="my-1 text-muted small font-weight-bold">
|
|
|
|
{i18n.t("related_posts")}
|
|
|
|
</div>
|
|
|
|
<PostListings
|
|
|
|
showCommunity
|
|
|
|
posts={suggestedPosts}
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
allLanguages={this.props.allLanguages}
|
|
|
|
siteLanguages={this.props.siteLanguages}
|
|
|
|
viewOnly
|
|
|
|
// All of these are unused, since its view only
|
|
|
|
onPostEdit={() => {}}
|
|
|
|
onPostVote={() => {}}
|
|
|
|
onPostReport={() => {}}
|
|
|
|
onBlockPerson={() => {}}
|
|
|
|
onLockPost={() => {}}
|
|
|
|
onDeletePost={() => {}}
|
|
|
|
onRemovePost={() => {}}
|
|
|
|
onSavePost={() => {}}
|
|
|
|
onFeaturePost={() => {}}
|
|
|
|
onPurgePerson={() => {}}
|
|
|
|
onPurgePost={() => {}}
|
|
|
|
onBanPersonFromCommunity={() => {}}
|
|
|
|
onBanPerson={() => {}}
|
|
|
|
onAddModToCommunity={() => {}}
|
|
|
|
onAddAdmin={() => {}}
|
|
|
|
onTransferCommunity={() => {}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 15:03:35 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
handlePostSubmit(i: PostForm, event: any) {
|
|
|
|
event.preventDefault();
|
2020-09-06 16:15:25 +00:00
|
|
|
// Coerce empty url string to undefined
|
2023-06-14 12:20:40 +00:00
|
|
|
if ((i.state.form.url ?? "") === "") {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.url = undefined), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
i.setState({ loading: true, submitted: true });
|
|
|
|
const auth = myAuthRequired();
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-05 21:31:12 +00:00
|
|
|
const pForm = i.state.form;
|
|
|
|
const pv = i.props.post_view;
|
2023-06-14 12:20:40 +00:00
|
|
|
|
|
|
|
if (pv) {
|
|
|
|
i.props.onEdit?.({
|
|
|
|
name: pForm.name,
|
|
|
|
url: pForm.url,
|
|
|
|
body: pForm.body,
|
|
|
|
nsfw: pForm.nsfw,
|
|
|
|
post_id: pv.post.id,
|
|
|
|
language_id: pForm.language_id,
|
|
|
|
auth,
|
|
|
|
});
|
|
|
|
} else if (pForm.name && pForm.community_id) {
|
|
|
|
i.props.onCreate?.({
|
|
|
|
name: pForm.name,
|
|
|
|
community_id: pForm.community_id,
|
|
|
|
url: pForm.url,
|
|
|
|
body: pForm.body,
|
|
|
|
nsfw: pForm.nsfw,
|
|
|
|
language_id: pForm.language_id,
|
|
|
|
honeypot: pForm.honeypot,
|
|
|
|
auth,
|
|
|
|
});
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
copySuggestedTitle(d: { i: PostForm; suggestedTitle?: string }) {
|
|
|
|
const sTitle = d.suggestedTitle;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (sTitle) {
|
2023-06-14 12:20:40 +00:00
|
|
|
d.i.setState(
|
2023-01-04 16:56:24 +00:00
|
|
|
s => ((s.form.name = sTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s)
|
|
|
|
);
|
2023-06-14 12:20:40 +00:00
|
|
|
d.i.setState({ suggestedPostsRes: { state: "empty" } });
|
2023-01-04 16:56:24 +00:00
|
|
|
setTimeout(() => {
|
2023-06-05 21:31:12 +00:00
|
|
|
const textarea: any = document.getElementById("post-title");
|
2023-01-04 16:56:24 +00:00
|
|
|
autosize.update(textarea);
|
|
|
|
}, 10);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostUrlChange(i: PostForm, event: any) {
|
2023-06-14 20:29:04 +00:00
|
|
|
const url = event.target.value;
|
|
|
|
|
|
|
|
i.setState({
|
|
|
|
form: {
|
|
|
|
url,
|
|
|
|
},
|
|
|
|
imageDeleteUrl: "",
|
|
|
|
});
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
i.fetchPageTitle();
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchPageTitle() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const url = this.state.form.url;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (url && validURL(url)) {
|
2023-06-14 12:20:40 +00:00
|
|
|
this.setState({ metadataRes: { state: "loading" } });
|
|
|
|
this.setState({
|
|
|
|
metadataRes: await HttpService.client.getSiteMetadata({ url }),
|
2023-01-04 16:56:24 +00:00
|
|
|
});
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNameChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.name = event.target.value), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
i.fetchSimilarPosts();
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
async fetchSimilarPosts() {
|
2023-06-05 21:31:12 +00:00
|
|
|
const q = this.state.form.name;
|
2023-01-04 16:56:24 +00:00
|
|
|
if (q && q !== "") {
|
2023-06-14 12:20:40 +00:00
|
|
|
this.setState({ suggestedPostsRes: { state: "loading" } });
|
|
|
|
this.setState({
|
|
|
|
suggestedPostsRes: await HttpService.client.search({
|
|
|
|
q,
|
|
|
|
type_: "Posts",
|
|
|
|
sort: "TopAll",
|
|
|
|
listing_type: "All",
|
|
|
|
community_id: this.state.form.community_id,
|
|
|
|
page: 1,
|
|
|
|
limit: trendingFetchLimit,
|
|
|
|
auth: myAuth(),
|
|
|
|
}),
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handlePostBodyChange(val: string) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState(s => ((s.form.body = val), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostCommunityChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.community_id = Number(event.target.value)), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePostNsfwChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.nsfw = event.target.checked), s));
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-22 15:14:58 +00:00
|
|
|
handleLanguageChange(val: number[]) {
|
2023-01-04 16:56:24 +00:00
|
|
|
this.setState(s => ((s.form.language_id = val.at(0)), s));
|
2022-09-22 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:19:47 +00:00
|
|
|
handleHoneyPotChange(i: PostForm, event: any) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.setState(s => ((s.form.honeypot = event.target.value), s));
|
2021-10-01 14:19:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
handleCancel(i: PostForm) {
|
2023-01-04 16:56:24 +00:00
|
|
|
i.props.onCancel?.();
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handlePreviewToggle(i: PostForm, event: any) {
|
|
|
|
event.preventDefault();
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ previewMode: !i.state.previewMode });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUploadPaste(i: PostForm, event: any) {
|
2023-06-05 21:31:12 +00:00
|
|
|
const image = event.clipboardData.files[0];
|
2020-09-06 16:15:25 +00:00
|
|
|
if (image) {
|
|
|
|
i.handleImageUpload(i, image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleImageUpload(i: PostForm, event: any) {
|
|
|
|
let file: any;
|
|
|
|
if (event.target) {
|
|
|
|
event.preventDefault();
|
|
|
|
file = event.target.files[0];
|
|
|
|
} else {
|
|
|
|
file = event;
|
|
|
|
}
|
|
|
|
|
2022-09-22 15:03:35 +00:00
|
|
|
i.setState({ imageLoading: true });
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2023-06-14 12:20:40 +00:00
|
|
|
HttpService.client.uploadImage({ image: file }).then(res => {
|
|
|
|
console.log("pictrs upload:");
|
|
|
|
console.log(res);
|
|
|
|
if (res.state === "success") {
|
|
|
|
if (res.data.msg === "ok") {
|
|
|
|
i.state.form.url = res.data.url;
|
2023-06-14 20:29:04 +00:00
|
|
|
i.setState({
|
|
|
|
imageLoading: false,
|
|
|
|
imageDeleteUrl: res.data.delete_url as string,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(JSON.stringify(res), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2023-06-14 12:20:40 +00:00
|
|
|
} else if (res.state === "failed") {
|
|
|
|
console.error(res.msg);
|
|
|
|
toast(res.msg, "danger");
|
2023-06-14 20:29:04 +00:00
|
|
|
i.setState({ imageLoading: false });
|
2023-06-14 12:20:40 +00:00
|
|
|
}
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 20:29:04 +00:00
|
|
|
handleImageDelete(i: PostForm) {
|
|
|
|
const { imageDeleteUrl } = i.state;
|
|
|
|
|
|
|
|
fetch(imageDeleteUrl);
|
|
|
|
|
|
|
|
i.setState({
|
|
|
|
imageDeleteUrl: "",
|
|
|
|
imageLoading: false,
|
|
|
|
form: {
|
|
|
|
url: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
handleCommunitySearch = debounce(async (text: string) => {
|
|
|
|
const { selectedCommunityChoice } = this.props;
|
|
|
|
this.setState({ communitySearchLoading: true });
|
|
|
|
|
|
|
|
const newOptions: Choice[] = [];
|
|
|
|
|
|
|
|
if (selectedCommunityChoice) {
|
|
|
|
newOptions.push(selectedCommunityChoice);
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
2020-09-24 13:42:20 +00:00
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
if (text.length > 0) {
|
2023-06-14 12:20:40 +00:00
|
|
|
newOptions.push(...(await fetchCommunities(text)).map(communityToChoice));
|
2023-04-15 14:47:10 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
communitySearchOptions: newOptions,
|
|
|
|
});
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 14:47:10 +00:00
|
|
|
this.setState({
|
|
|
|
communitySearchLoading: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handleCommunitySelect(choice: Choice) {
|
|
|
|
if (this.props.onSelectCommunity) {
|
|
|
|
this.props.onSelectCommunity(choice);
|
2020-09-24 13:42:20 +00:00
|
|
|
}
|
2020-09-07 22:24:48 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|