AnthologyInfoEdit.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { Form, message } from "antd";
  2. import { useIntl } from "react-intl";
  3. import { ProForm, ProFormText } from "@ant-design/pro-components";
  4. import MDEditor from "@uiw/react-md-editor";
  5. import { get, put } from "../../request";
  6. import { IAnthologyDataRequest, IAnthologyResponse } from "../api/Article";
  7. import LangSelect from "../general/LangSelect";
  8. import PublicitySelect from "../studio/PublicitySelect";
  9. interface IFormData {
  10. title: string;
  11. subtitle: string;
  12. summary?: string;
  13. lang: string;
  14. status: number;
  15. }
  16. interface IWidget {
  17. anthologyId?: string;
  18. onLoad?: Function;
  19. }
  20. const AnthologyInfoEditWidget = ({ anthologyId, onLoad }: IWidget) => {
  21. const intl = useIntl();
  22. return anthologyId ? (
  23. <ProForm<IFormData>
  24. onFinish={async (values: IFormData) => {
  25. console.log(values);
  26. const res = await put<IAnthologyDataRequest, IAnthologyResponse>(
  27. `/v2/anthology/${anthologyId}`,
  28. {
  29. title: values.title,
  30. subtitle: values.subtitle,
  31. summary: values.summary,
  32. status: values.status,
  33. lang: values.lang,
  34. }
  35. );
  36. console.log(res);
  37. if (res.ok) {
  38. if (typeof onLoad !== "undefined") {
  39. onLoad(res.data);
  40. }
  41. message.success(
  42. intl.formatMessage({
  43. id: "flashes.success",
  44. })
  45. );
  46. } else {
  47. message.error(res.message);
  48. }
  49. }}
  50. request={async () => {
  51. const url = `/v2/anthology/${anthologyId}`;
  52. console.log("url", url);
  53. const res = await get<IAnthologyResponse>(url);
  54. console.log("文集get", res);
  55. if (res.ok) {
  56. if (typeof onLoad !== "undefined") {
  57. onLoad(res.data);
  58. }
  59. return {
  60. title: res.data.title,
  61. subtitle: res.data.subtitle,
  62. summary: res.data.summary ? res.data.summary : undefined,
  63. lang: res.data.lang,
  64. status: res.data.status,
  65. };
  66. } else {
  67. return {
  68. title: "",
  69. subtitle: "",
  70. summary: "",
  71. lang: "",
  72. status: 0,
  73. };
  74. }
  75. }}
  76. >
  77. <ProForm.Group>
  78. <ProFormText
  79. width="md"
  80. name="title"
  81. required
  82. label={intl.formatMessage({
  83. id: "forms.fields.title.label",
  84. })}
  85. rules={[
  86. {
  87. required: true,
  88. message: intl.formatMessage({
  89. id: "forms.message.title.required",
  90. }),
  91. },
  92. ]}
  93. />
  94. <ProFormText
  95. width="md"
  96. name="subtitle"
  97. label={intl.formatMessage({
  98. id: "forms.fields.subtitle.label",
  99. })}
  100. />
  101. </ProForm.Group>
  102. <ProForm.Group>
  103. <LangSelect width="md" />
  104. <PublicitySelect width="md" />
  105. </ProForm.Group>
  106. <ProForm.Group>
  107. <Form.Item
  108. name="summary"
  109. label={intl.formatMessage({ id: "forms.fields.summary.label" })}
  110. >
  111. <MDEditor />
  112. </Form.Item>
  113. </ProForm.Group>
  114. </ProForm>
  115. ) : (
  116. <></>
  117. );
  118. };
  119. export default AnthologyInfoEditWidget;