import { useIntl } from "react-intl"; import { App, Form } from "antd"; import { ProForm, type ProFormInstance, ProFormText, ProFormTextArea, } from "@ant-design/pro-components"; import { post } from "../../request"; import type { ICommentRequest, ICommentResponse } from "../../api/Comment"; import { useAppSelector } from "../../hooks"; import { currentUser as _currentUser } from "../../reducers/current-user"; import { useEffect, useRef, useState } from "react"; import MDEditor from "@uiw/react-md-editor"; import type { IComment, TDiscussionType } from "../../api/discussion"; import type { TContentType } from "../../api/article"; import { discussionCountUpgrade, toIComment } from "./utils"; interface IWidget { resId?: string; resType?: string; //TODO change parent?: string; topicId?: string; type?: TDiscussionType; topic?: IComment; contentType?: TContentType; onCreated?: (value: IComment) => void; onTopicCreated?: (value: IComment) => void; } const DiscussionCreateWidget = ({ resId, resType, contentType = "html", parent, topicId, topic, type = "discussion", onCreated, onTopicCreated, }: IWidget) => { const intl = useIntl(); const { message } = App.useApp(); const formRef = useRef(undefined); const _currUser = useAppSelector(_currentUser); const [currParent, setCurrParent] = useState(parent); useEffect(() => { setCurrParent(parent); }, [parent]); if (typeof _currUser === "undefined") { return <>; } else { return (
{_currUser?.nickName}:
formRef={formRef} autoFocusFirstInput={false} onFinish={async (values) => { //新建 console.log("create", resId, resType, currParent, topic); console.log("value", values); let newParent: string | undefined; if (typeof currParent === "undefined") { if (typeof topic !== "undefined" && topic.tplId) { /** * 在模版下跟帖 * 先建立模版topic,再建立跟帖 */ const topicData: ICommentRequest = { res_id: resId, res_type: resType, title: topic.title, tpl_id: topic.tplId, content: topic.content, content_type: "markdown", type: topic.type, }; const url = `/api/v2/discussion`; console.log("create topic api request", url, topicData); const newTopic = await post< ICommentRequest, ICommentResponse >(url, topicData); if (newTopic.ok) { discussionCountUpgrade(resId); setCurrParent(newTopic.data.id); newParent = newTopic.data.id; if (typeof onTopicCreated !== "undefined") { onTopicCreated(toIComment(newTopic.data)); } } else { console.error("no parent id"); return; } } } const url = `/api/v2/discussion`; const data: ICommentRequest = { res_id: resId, res_type: resType, parent: newParent ? newParent : currParent, topicId: topicId, title: values.title, content: values.content, content_type: contentType, type: topic ? topic.type : type, }; console.info("api request", url, data); post(url, data) .then((json) => { console.debug("new discussion api response", json); if (json.ok) { formRef.current?.resetFields(); discussionCountUpgrade(resId); if (typeof onCreated !== "undefined") { onCreated(toIComment(json.data)); } } else { message.error(json.message); } }) .catch((e) => { message.error(e.message); }); }} params={{}} > {contentType === "text" ? ( ) : contentType === "html" ? ( ) : contentType === "markdown" ? ( ) : ( <> )}
); } }; export default DiscussionCreateWidget;