ArticleCreate.tsx 3.7 KB

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