CommentEdit.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Button, Card } from "antd";
  4. import { Input, message } from "antd";
  5. import { SaveOutlined } from "@ant-design/icons";
  6. import {
  7. ProForm,
  8. ProFormText,
  9. ProFormTextArea,
  10. } from "@ant-design/pro-components";
  11. import { Col, Row, Space } from "antd";
  12. import { IComment } from "./CommentItem";
  13. import { post, put } from "../../request";
  14. import { ICommentRequest, ICommentResponse } from "../api/Comment";
  15. const { TextArea } = Input;
  16. interface IWidget {
  17. data: IComment;
  18. onCreated?: Function;
  19. }
  20. const Widget = ({ data, onCreated }: IWidget) => {
  21. const intl = useIntl();
  22. const [value, setValue] = useState(data.content);
  23. const [saving, setSaving] = useState<boolean>(false);
  24. const save = () => {
  25. setSaving(true);
  26. put<ICommentRequest, ICommentResponse>(`/v2/discussion/${data.id}`, {
  27. content: value,
  28. })
  29. .then((json) => {
  30. console.log(json);
  31. setSaving(false);
  32. if (json.ok) {
  33. message.success(intl.formatMessage({ id: "flashes.success" }));
  34. } else {
  35. message.error(json.message);
  36. }
  37. })
  38. .catch((e) => {
  39. setSaving(false);
  40. console.error("catch", e);
  41. message.error(e.message);
  42. });
  43. };
  44. const formItemLayout = {
  45. labelCol: { span: 4 },
  46. wrapperCol: { span: 20 },
  47. };
  48. return (
  49. <div>
  50. <ProForm<IComment>
  51. {...formItemLayout}
  52. layout="horizontal"
  53. submitter={{
  54. render: (props, doms) => {
  55. return (
  56. <Row>
  57. <Col span={14} offset={4}>
  58. <Space>{doms}</Space>
  59. </Col>
  60. </Row>
  61. );
  62. },
  63. }}
  64. onFinish={async (values) => {
  65. if (typeof values.id === "undefined") {
  66. //新建
  67. post<ICommentRequest, ICommentResponse>(`/v2/discussion`, {
  68. res_id: data.resId,
  69. res_type: data.resType,
  70. title: values.title,
  71. content: values.content,
  72. })
  73. .then((json) => {
  74. console.log(json);
  75. if (json.ok) {
  76. message.success(
  77. intl.formatMessage({ id: "flashes.success" })
  78. );
  79. if (typeof onCreated !== "undefined") {
  80. onCreated(json.data);
  81. }
  82. } else {
  83. message.error(json.message);
  84. }
  85. })
  86. .catch((e) => {
  87. message.error(e.message);
  88. });
  89. } else {
  90. //修改
  91. }
  92. }}
  93. params={{}}
  94. request={async () => {
  95. return data;
  96. }}
  97. >
  98. <ProFormText
  99. name="title"
  100. label={intl.formatMessage({ id: "forms.fields.title.label" })}
  101. tooltip="最长为 24 位"
  102. placeholder={intl.formatMessage({
  103. id: "forms.message.title.required",
  104. })}
  105. />
  106. <ProFormTextArea
  107. name="content"
  108. label={intl.formatMessage({ id: "forms.fields.content.label" })}
  109. placeholder={intl.formatMessage({
  110. id: "forms.fields.content.placeholder",
  111. })}
  112. />
  113. </ProForm>
  114. <Card
  115. title={<span>{data.user.nickName}</span>}
  116. extra={
  117. <Button shape="circle" size="small">
  118. xxx
  119. </Button>
  120. }
  121. style={{ width: "auto" }}
  122. >
  123. <TextArea
  124. rows={4}
  125. showCount
  126. maxLength={2048}
  127. value={value}
  128. onChange={(e) => setValue(e.target.value)}
  129. />
  130. <div>
  131. <Button
  132. type="primary"
  133. icon={<SaveOutlined />}
  134. loading={saving}
  135. onClick={() => save()}
  136. >
  137. Save
  138. </Button>
  139. </div>
  140. </Card>
  141. </div>
  142. );
  143. };
  144. export default Widget;