CommentCreate.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { useIntl } from "react-intl";
  2. import { message } from "antd";
  3. import {
  4. ProForm,
  5. ProFormInstance,
  6. ProFormText,
  7. ProFormTextArea,
  8. } from "@ant-design/pro-components";
  9. import { Col, Row, Space } from "antd";
  10. import { IComment } from "./CommentItem";
  11. import { post } from "../../request";
  12. import { ICommentRequest, ICommentResponse } from "../api/Comment";
  13. import { useAppSelector } from "../../hooks";
  14. import { currentUser as _currentUser } from "../../reducers/current-user";
  15. import { useRef } from "react";
  16. interface IWidget {
  17. resId: string;
  18. resType: string;
  19. parent?: string;
  20. onCreated?: Function;
  21. }
  22. const Widget = ({ resId, resType, parent, onCreated }: IWidget) => {
  23. const intl = useIntl();
  24. const formRef = useRef<ProFormInstance>();
  25. const _currUser = useAppSelector(_currentUser);
  26. const formItemLayout = {
  27. labelCol: { span: 4 },
  28. wrapperCol: { span: 20 },
  29. };
  30. return (
  31. <div>
  32. <div>{_currUser?.nickName}:</div>
  33. <ProForm<IComment>
  34. {...formItemLayout}
  35. layout="horizontal"
  36. formRef={formRef}
  37. submitter={{
  38. render: (props, doms) => {
  39. return (
  40. <Row>
  41. <Col span={14} offset={4}>
  42. <Space>{doms}</Space>
  43. </Col>
  44. </Row>
  45. );
  46. },
  47. }}
  48. onFinish={async (values) => {
  49. //新建
  50. console.log("create", resId, resType, parent);
  51. post<ICommentRequest, ICommentResponse>(`/v2/discussion`, {
  52. res_id: resId,
  53. res_type: resType,
  54. parent: parent,
  55. title: values.title,
  56. content: values.content,
  57. })
  58. .then((json) => {
  59. console.log("new discussion", json);
  60. if (json.ok) {
  61. formRef.current?.resetFields();
  62. if (typeof onCreated !== "undefined") {
  63. onCreated({
  64. id: json.data.id,
  65. resId: json.data.res_id,
  66. resType: json.data.res_type,
  67. user: {
  68. id: json.data.editor?.id ? json.data.editor.id : "null",
  69. nickName: json.data.editor?.nickName
  70. ? json.data.editor.nickName
  71. : "null",
  72. realName: json.data.editor?.userName
  73. ? json.data.editor.userName
  74. : "null",
  75. avatar: json.data.editor?.avatar
  76. ? json.data.editor.avatar
  77. : "null",
  78. },
  79. title: json.data.title,
  80. parent: json.data.parent,
  81. content: json.data.content,
  82. createdAt: json.data.created_at,
  83. updatedAt: json.data.updated_at,
  84. });
  85. }
  86. } else {
  87. message.error(json.message);
  88. }
  89. })
  90. .catch((e) => {
  91. message.error(e.message);
  92. });
  93. }}
  94. params={{}}
  95. >
  96. {parent ? (
  97. <></>
  98. ) : (
  99. <ProFormText
  100. name="title"
  101. label={intl.formatMessage({ id: "forms.fields.title.label" })}
  102. tooltip="最长为 24 位"
  103. placeholder={intl.formatMessage({
  104. id: "forms.message.title.required",
  105. })}
  106. rules={[{ required: true, message: "这是必填项" }]}
  107. />
  108. )}
  109. <ProFormTextArea
  110. name="content"
  111. label={intl.formatMessage({ id: "forms.fields.content.label" })}
  112. placeholder={intl.formatMessage({
  113. id: "forms.fields.content.placeholder",
  114. })}
  115. />
  116. </ProForm>
  117. </div>
  118. );
  119. };
  120. export default Widget;