ArticleCreate.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { useIntl } from "react-intl";
  2. import {
  3. ProForm,
  4. ProFormInstance,
  5. ProFormSelect,
  6. ProFormText,
  7. } from "@ant-design/pro-components";
  8. import { Alert, Space, message } from "antd";
  9. import { get, post } from "../../request";
  10. import {
  11. IAnthologyListResponse,
  12. IArticleCreateRequest,
  13. IArticleDataResponse,
  14. IArticleResponse,
  15. } from "../api/Article";
  16. import LangSelect from "../general/LangSelect";
  17. import { useEffect, useRef, useState } from "react";
  18. interface IFormData {
  19. title: string;
  20. lang: string;
  21. studio: string;
  22. anthologyId?: string;
  23. parentId?: string;
  24. }
  25. interface IWidget {
  26. studio?: string;
  27. anthologyId?: string;
  28. parentId?: string | null;
  29. compact?: boolean;
  30. onSuccess?: Function;
  31. }
  32. const ArticleCreateWidget = ({
  33. studio,
  34. anthologyId,
  35. parentId,
  36. compact = true,
  37. onSuccess,
  38. }: IWidget) => {
  39. const intl = useIntl();
  40. const formRef = useRef<ProFormInstance>();
  41. const [parent, setParent] = useState<IArticleDataResponse>();
  42. console.log("parentId", parentId);
  43. useEffect(() => {
  44. if (parentId) {
  45. get<IArticleResponse>(`/v2/article/${parentId}`).then((json) => {
  46. console.log("article", json);
  47. if (json.ok) {
  48. setParent(json.data);
  49. }
  50. });
  51. }
  52. }, []);
  53. return (
  54. <Space direction="vertical">
  55. {parentId ? (
  56. <Alert
  57. message={`从文章 ${parent?.title} 创建子文章`}
  58. type="info"
  59. closable
  60. />
  61. ) : undefined}
  62. <ProForm<IFormData>
  63. formRef={formRef}
  64. onFinish={async (values: IFormData) => {
  65. console.log(values);
  66. if (typeof studio === "undefined") {
  67. return;
  68. }
  69. values.studio = studio;
  70. values.parentId = parentId ? parentId : undefined;
  71. const res = await post<IArticleCreateRequest, IArticleResponse>(
  72. `/v2/article`,
  73. values
  74. );
  75. console.log(res);
  76. if (res.ok) {
  77. message.success(intl.formatMessage({ id: "flashes.success" }));
  78. if (typeof onSuccess !== "undefined") {
  79. onSuccess(res.data);
  80. formRef.current?.resetFields(["title"]);
  81. }
  82. } else {
  83. message.error(res.message);
  84. }
  85. }}
  86. >
  87. <ProForm.Group>
  88. <ProFormText
  89. width="md"
  90. name="title"
  91. required
  92. label={intl.formatMessage({ id: "channel.name" })}
  93. rules={[
  94. {
  95. required: true,
  96. message: intl.formatMessage({
  97. id: "channel.create.message.noname",
  98. }),
  99. },
  100. ]}
  101. />
  102. </ProForm.Group>
  103. <ProForm.Group>
  104. <LangSelect />
  105. </ProForm.Group>
  106. <ProForm.Group>
  107. <ProFormSelect
  108. name={"anthologyId"}
  109. label={"加入文集"}
  110. hidden={compact}
  111. width={"md"}
  112. showSearch
  113. debounceTime={300}
  114. request={async ({ keyWords }) => {
  115. console.log("keyWord", keyWords);
  116. let url = `/v2/anthology?view=studio&view2=my&name=${studio}`;
  117. url += keyWords ? "&search=" + keyWords : "";
  118. const res = await get<IAnthologyListResponse>(url);
  119. const result = res.data.rows.map((item) => {
  120. return {
  121. value: item.uid,
  122. label: item.title,
  123. };
  124. });
  125. console.log("json", result);
  126. return result;
  127. }}
  128. />
  129. </ProForm.Group>
  130. </ProForm>
  131. </Space>
  132. );
  133. };
  134. export default ArticleCreateWidget;