DiscussionEdit.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { useIntl } from "react-intl";
  2. import { Button, Card, Form } from "antd";
  3. import { message } from "antd";
  4. import { ProForm, ProFormText } from "@ant-design/pro-components";
  5. import { Col, Row, Space } from "antd";
  6. import { CloseOutlined } from "@ant-design/icons";
  7. import type { IComment } from "./DiscussionItem";
  8. import { put } from "../../request";
  9. import type { ICommentRequest, ICommentResponse } from "../../api/Comment";
  10. import MDEditor from "@uiw/react-md-editor";
  11. interface IWidget {
  12. data: IComment;
  13. onCreated?: Function;
  14. onUpdated?: Function;
  15. onClose?: Function;
  16. }
  17. const DiscussionEditWidget = ({
  18. data,
  19. ___onCreated,
  20. onUpdated,
  21. onClose,
  22. }: IWidget) => {
  23. const intl = useIntl();
  24. return (
  25. <Card
  26. title={<span>{data.user.nickName}</span>}
  27. extra={
  28. <Button
  29. shape="circle"
  30. size="small"
  31. icon={<CloseOutlined />}
  32. onClick={() => {
  33. if (typeof onClose !== "undefined") {
  34. onClose();
  35. }
  36. }}
  37. />
  38. }
  39. style={{ width: "auto" }}
  40. >
  41. <ProForm<IComment>
  42. submitter={{
  43. render: (_props, doms) => {
  44. return (
  45. <Row>
  46. <Col span={14} offset={4}>
  47. <Space>{doms}</Space>
  48. </Col>
  49. </Row>
  50. );
  51. },
  52. }}
  53. onFinish={async (values) => {
  54. const url = `/v2/discussion/${data.id}`;
  55. const newData: ICommentRequest = {
  56. title: values.title,
  57. content: values.content,
  58. };
  59. console.info("DiscussionEdit api request", url, newData);
  60. put<ICommentRequest, ICommentResponse>(url, newData)
  61. .then((json) => {
  62. console.debug("DiscussionEdit api response", json);
  63. if (json.ok) {
  64. console.log(intl.formatMessage({ id: "flashes.success" }));
  65. if (typeof onUpdated !== "undefined") {
  66. const newData = {
  67. id: json.data.id, //id未提供为新建
  68. resId: json.data.res_id,
  69. resType: json.data.res_type,
  70. user: json.data.editor,
  71. parent: json.data.parent,
  72. title: json.data.title,
  73. content: json.data.content,
  74. status: json.data.status,
  75. childrenCount: json.data.children_count,
  76. createdAt: json.data.created_at,
  77. updatedAt: json.data.updated_at,
  78. };
  79. onUpdated(newData);
  80. }
  81. } else {
  82. message.error(json.message);
  83. }
  84. })
  85. .catch((e) => {
  86. message.error(e.message);
  87. });
  88. }}
  89. params={{}}
  90. request={async () => {
  91. return data;
  92. }}
  93. >
  94. <ProForm.Group>
  95. <ProFormText
  96. name="title"
  97. hidden={data.parent ? true : false}
  98. label={intl.formatMessage({ id: "forms.fields.title.label" })}
  99. tooltip="最长为 24 位"
  100. rules={[{ required: data.parent ? false : true }]}
  101. />
  102. </ProForm.Group>
  103. <ProForm.Group>
  104. <Form.Item
  105. name="content"
  106. label={intl.formatMessage({ id: "forms.fields.content.label" })}
  107. >
  108. <MDEditor style={{ width: "100%" }} />
  109. </Form.Item>
  110. </ProForm.Group>
  111. </ProForm>
  112. </Card>
  113. );
  114. };
  115. export default DiscussionEditWidget;