DiscussionCreate.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { useIntl } from "react-intl";
  2. import { Form, message } from "antd";
  3. import {
  4. ProForm,
  5. ProFormInstance,
  6. ProFormText,
  7. ProFormTextArea,
  8. } from "@ant-design/pro-components";
  9. import ReactQuill from "react-quill";
  10. import "react-quill/dist/quill.snow.css";
  11. import { IComment } from "./DiscussionItem";
  12. import { post } from "../../request";
  13. import { ICommentRequest, ICommentResponse } from "../api/Comment";
  14. import { useAppSelector } from "../../hooks";
  15. import { currentUser as _currentUser } from "../../reducers/current-user";
  16. import { useRef } from "react";
  17. import MDEditor from "@uiw/react-md-editor";
  18. export type TContentType = "text" | "markdown" | "html" | "json";
  19. interface IWidget {
  20. resId?: string;
  21. resType?: string;
  22. parent?: string;
  23. topic?: IComment;
  24. onCreated?: Function;
  25. contentType?: TContentType;
  26. }
  27. const DiscussionCreateWidget = ({
  28. resId,
  29. resType,
  30. contentType = "html",
  31. parent,
  32. topic,
  33. onCreated,
  34. }: IWidget) => {
  35. const intl = useIntl();
  36. const formRef = useRef<ProFormInstance>();
  37. const _currUser = useAppSelector(_currentUser);
  38. if (typeof _currUser === "undefined") {
  39. return <></>;
  40. } else {
  41. return (
  42. <div>
  43. <div>{_currUser?.nickName}:</div>
  44. <div>
  45. <ProForm<IComment>
  46. formRef={formRef}
  47. onFinish={async (values) => {
  48. //新建
  49. console.log("create", resId, resType, parent);
  50. console.log("value", values);
  51. if (typeof parent === "undefined") {
  52. if (typeof topic !== "undefined" && topic.tplId) {
  53. const newTopic = await post<
  54. ICommentRequest,
  55. ICommentResponse
  56. >(`/v2/discussion`, {
  57. res_id: resId,
  58. res_type: resType,
  59. title: topic.title,
  60. tpl_id: topic.tplId,
  61. content: topic.content,
  62. content_type: "markdown",
  63. });
  64. if (newTopic.ok) {
  65. parent = newTopic.data.id;
  66. } else {
  67. console.error("no parent id");
  68. return;
  69. }
  70. }
  71. }
  72. console.log("parent", parent);
  73. post<ICommentRequest, ICommentResponse>(`/v2/discussion`, {
  74. res_id: resId,
  75. res_type: resType,
  76. parent: parent,
  77. title: values.title,
  78. content: values.content,
  79. content_type: contentType,
  80. })
  81. .then((json) => {
  82. console.log("new discussion", json);
  83. if (json.ok) {
  84. formRef.current?.resetFields();
  85. if (typeof onCreated !== "undefined") {
  86. onCreated({
  87. id: json.data.id,
  88. resId: json.data.res_id,
  89. resType: json.data.res_type,
  90. user: {
  91. id: json.data.editor?.id
  92. ? json.data.editor.id
  93. : "null",
  94. nickName: json.data.editor?.nickName
  95. ? json.data.editor.nickName
  96. : "null",
  97. realName: json.data.editor?.userName
  98. ? json.data.editor.userName
  99. : "null",
  100. avatar: json.data.editor?.avatar
  101. ? json.data.editor.avatar
  102. : "null",
  103. },
  104. title: json.data.title,
  105. parent: json.data.parent,
  106. content: json.data.content,
  107. createdAt: json.data.created_at,
  108. updatedAt: json.data.updated_at,
  109. });
  110. }
  111. } else {
  112. message.error(json.message);
  113. }
  114. })
  115. .catch((e) => {
  116. message.error(e.message);
  117. });
  118. }}
  119. params={{}}
  120. >
  121. <ProForm.Group>
  122. <ProFormText
  123. name="title"
  124. width={"lg"}
  125. hidden={
  126. typeof parent !== "undefined" ||
  127. typeof topic?.tplId !== "undefined"
  128. }
  129. label={intl.formatMessage({ id: "forms.fields.title.label" })}
  130. tooltip="最长为 24 位"
  131. placeholder={intl.formatMessage({
  132. id: "forms.message.question.required",
  133. })}
  134. rules={[{ required: parent || topic?.tplId ? false : true }]}
  135. />
  136. </ProForm.Group>
  137. <ProForm.Group>
  138. {contentType === "text" ? (
  139. <ProFormTextArea
  140. name="content"
  141. label={intl.formatMessage({
  142. id: "forms.fields.content.label",
  143. })}
  144. placeholder={intl.formatMessage({
  145. id: "forms.fields.content.placeholder",
  146. })}
  147. />
  148. ) : contentType === "html" ? (
  149. <Form.Item
  150. name="content"
  151. label={intl.formatMessage({
  152. id: "forms.fields.content.label",
  153. })}
  154. tooltip="可以直接粘贴屏幕截图"
  155. >
  156. <ReactQuill
  157. theme="snow"
  158. style={{ height: 180 }}
  159. modules={{
  160. toolbar: [
  161. ["bold", "italic", "underline", "strike"],
  162. ["blockquote", "code-block"],
  163. [{ header: 1 }, { header: 2 }],
  164. [{ list: "ordered" }, { list: "bullet" }],
  165. [{ indent: "-1" }, { indent: "+1" }],
  166. [{ size: ["small", false, "large", "huge"] }],
  167. [{ header: [1, 2, 3, 4, 5, 6, false] }],
  168. ["link", "image", "video"],
  169. [{ color: [] }, { background: [] }],
  170. [{ font: [] }],
  171. [{ align: [] }],
  172. ],
  173. }}
  174. />
  175. </Form.Item>
  176. ) : contentType === "markdown" ? (
  177. <Form.Item
  178. name="content"
  179. rules={[
  180. {
  181. required:
  182. typeof parent !== "undefined" ||
  183. typeof topic?.tplId !== "undefined",
  184. },
  185. ]}
  186. label={
  187. typeof parent === "undefined" &&
  188. typeof topic?.tplId === "undefined"
  189. ? intl.formatMessage({
  190. id: "forms.message.question.description.option",
  191. })
  192. : intl.formatMessage({
  193. id: "forms.fields.replay.label",
  194. })
  195. }
  196. >
  197. <MDEditor
  198. placeholder={
  199. "问题的详细描述" +
  200. (typeof parent !== "undefined" &&
  201. typeof topic?.tplId !== "undefined"
  202. ? ""
  203. : "(选填)")
  204. }
  205. />
  206. </Form.Item>
  207. ) : (
  208. <></>
  209. )}
  210. </ProForm.Group>
  211. </ProForm>
  212. </div>
  213. </div>
  214. );
  215. }
  216. };
  217. export default DiscussionCreateWidget;