import { myAuthRequired } from "@utils/app"; import { capitalizeFirstLetter } from "@utils/helpers"; import { Component, InfernoNode } from "inferno"; import { T } from "inferno-i18next-dess"; import { Prompt } from "inferno-router"; import { CreatePrivateMessage, EditPrivateMessage, Person, PrivateMessageView, } from "lemmy-js-client"; import { relTags } from "../../config"; import { I18NextService } from "../../services"; import { setupTippy } from "../../tippy"; import { Icon } from "../common/icon"; import { MarkdownTextArea } from "../common/markdown-textarea"; import { PersonListing } from "../person/person-listing"; interface PrivateMessageFormProps { recipient: Person; privateMessageView?: PrivateMessageView; // If a pm is given, that means this is an edit replyType?: boolean; onCancel?(): any; onCreate?(form: CreatePrivateMessage): void; onEdit?(form: EditPrivateMessage): void; } interface PrivateMessageFormState { content?: string; loading: boolean; previewMode: boolean; submitted: boolean; } export class PrivateMessageForm extends Component< PrivateMessageFormProps, PrivateMessageFormState > { state: PrivateMessageFormState = { loading: false, previewMode: false, content: this.props.privateMessageView ? this.props.privateMessageView.private_message.content : undefined, submitted: false, }; constructor(props: any, context: any) { super(props, context); this.handleContentChange = this.handleContentChange.bind(this); } componentDidMount() { setupTippy(); } componentWillReceiveProps( nextProps: Readonly<{ children?: InfernoNode } & PrivateMessageFormProps>, ): void { if (this.props !== nextProps) { this.setState({ loading: false, content: undefined, previewMode: false }); } } render() { return (
); } handlePrivateMessageSubmit(i: PrivateMessageForm, event: any) { event.preventDefault(); i.setState({ loading: true, submitted: true }); const pm = i.props.privateMessageView; const auth = myAuthRequired(); const content = i.state.content ?? ""; if (pm) { i.props.onEdit?.({ private_message_id: pm.private_message.id, content, auth, }); } else { i.props.onCreate?.({ content, recipient_id: i.props.recipient.id, auth, }); } } handleContentChange(val: string) { this.setState({ content: val }); } handleCancel(i: PrivateMessageForm) { i.props.onCancel?.(); } handlePreviewToggle(i: PrivateMessageForm, event: any) { event.preventDefault(); i.setState({ previewMode: !i.state.previewMode }); } }