Description.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { useEffect, useState } from "react";
  2. import { Button, message, Space, Typography } from "antd";
  3. import { EditOutlined, SaveOutlined } from "@ant-design/icons";
  4. import type {
  5. ITaskData,
  6. ITaskResponse,
  7. ITaskUpdateRequest,
  8. } from "../../api/task";
  9. import MdView from "../template/MdView";
  10. import MDEditor from "@uiw/react-md-editor";
  11. import "../article/article.css";
  12. import { patch } from "../../request";
  13. import _DiscussionDrawer from "../discussion/DiscussionDrawer";
  14. import { useIntl } from "react-intl";
  15. const { Text } = Typography;
  16. interface IWidget {
  17. task?: ITaskData;
  18. onChange?: (data: ITaskData[]) => void;
  19. onDiscussion?: () => void;
  20. }
  21. const Description = ({ task, onChange, onDiscussion }: IWidget) => {
  22. const intl = useIntl();
  23. const [mode, setMode] = useState<"read" | "edit">("read");
  24. const [content, setContent] = useState(task?.description);
  25. const [loading, setLoading] = useState(false);
  26. useEffect(() => setContent(task?.description), [task]);
  27. return (
  28. <div>
  29. <div
  30. style={{
  31. display: "flex",
  32. justifyContent: "space-between",
  33. padding: 8,
  34. }}
  35. >
  36. <span>
  37. <Text strong>任务描述</Text>
  38. </span>
  39. <span>
  40. {mode === "read" ? (
  41. <Space>
  42. <Button key={1} onClick={onDiscussion}>
  43. {intl.formatMessage({ id: "buttons.discussion" })}
  44. </Button>
  45. <Button
  46. key={2}
  47. ghost
  48. type="primary"
  49. icon={<EditOutlined />}
  50. onClick={() => setMode("edit")}
  51. >
  52. {intl.formatMessage({ id: "buttons.edit" })}
  53. </Button>
  54. </Space>
  55. ) : (
  56. <Button
  57. ghost
  58. type="primary"
  59. icon={<SaveOutlined />}
  60. loading={loading}
  61. onClick={() => {
  62. if (!task) {
  63. return;
  64. }
  65. const setting: ITaskUpdateRequest = {
  66. id: task.id,
  67. studio_name: "",
  68. description: content,
  69. };
  70. const url = `/v2/task/${setting.id}`;
  71. console.info("api request", url, setting);
  72. setLoading(true);
  73. patch<ITaskUpdateRequest, ITaskResponse>(url, setting)
  74. .then((json) => {
  75. console.info("api response", json);
  76. if (json.ok) {
  77. message.success("Success");
  78. setMode("read");
  79. onChange && onChange([json.data]);
  80. } else {
  81. message.error(json.message);
  82. }
  83. })
  84. .finally(() => setLoading(false));
  85. }}
  86. >
  87. {intl.formatMessage({ id: "buttons.save" })}
  88. </Button>
  89. )}
  90. </span>
  91. </div>
  92. {mode === "read" ? (
  93. <MdView html={task?.html} />
  94. ) : (
  95. <MDEditor
  96. className="pcd_md_editor"
  97. value={content ?? undefined}
  98. onChange={(value: unknown) => setContent(value)}
  99. height={450}
  100. minHeight={200}
  101. style={{ width: "100%" }}
  102. />
  103. )}
  104. </div>
  105. );
  106. };
  107. export default Description;