AnthologyInfoEdit.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { Form, message } from "antd";
  2. import { useIntl } from "react-intl";
  3. import {
  4. ProForm,
  5. ProFormSelect,
  6. ProFormText,
  7. type RequestOptionsType,
  8. } from "@ant-design/pro-components";
  9. import MDEditor from "@uiw/react-md-editor";
  10. import { get, put } from "../../request";
  11. import type {
  12. IAnthologyDataRequest,
  13. IAnthologyDataResponse,
  14. IAnthologyResponse,
  15. } from "../../api/Article";
  16. import LangSelect from "../general/LangSelect";
  17. import PublicitySelect from "../studio/PublicitySelect";
  18. import { useState } from "react";
  19. import type { DefaultOptionType } from "antd/lib/select";
  20. import type { IApiResponseChannelList } from "../../api/Channel";
  21. import { useAppSelector } from "../../hooks";
  22. import { currentUser } from "../../reducers/current-user";
  23. interface IFormData {
  24. title: string;
  25. subtitle: string;
  26. summary?: string;
  27. lang: string;
  28. status: number;
  29. defaultChannel?: string;
  30. }
  31. interface IWidget {
  32. anthologyId?: string;
  33. studioName?: string;
  34. onLoad?: Function;
  35. }
  36. const AnthologyInfoEditWidget = ({
  37. studioName,
  38. anthologyId,
  39. onLoad,
  40. }: IWidget) => {
  41. const intl = useIntl();
  42. const [channelOption, setChannelOption] = useState<DefaultOptionType[]>([]);
  43. const [currChannel, setCurrChannel] = useState<RequestOptionsType>();
  44. const [data, setData] = useState<IAnthologyDataResponse>();
  45. const user = useAppSelector(currentUser);
  46. return anthologyId ? (
  47. <ProForm<IFormData>
  48. onFinish={async (values: IFormData) => {
  49. const url = `/v2/anthology/${anthologyId}`;
  50. console.log("url", url);
  51. console.log("values", values);
  52. const res = await put<IAnthologyDataRequest, IAnthologyResponse>(url, {
  53. title: values.title,
  54. subtitle: values.subtitle,
  55. summary: values.summary,
  56. status: values.status,
  57. lang: values.lang,
  58. default_channel: values.defaultChannel,
  59. });
  60. console.log(res);
  61. if (res.ok) {
  62. if (typeof onLoad !== "undefined") {
  63. onLoad(res.data);
  64. }
  65. message.success(
  66. intl.formatMessage({
  67. id: "flashes.success",
  68. })
  69. );
  70. } else {
  71. message.error(res.message);
  72. }
  73. }}
  74. request={async () => {
  75. const url = `/v2/anthology/${anthologyId}`;
  76. console.log("url", url);
  77. const res = await get<IAnthologyResponse>(url);
  78. console.log("文集get", res);
  79. if (res.ok) {
  80. setData(res.data);
  81. if (typeof onLoad !== "undefined") {
  82. onLoad(res.data);
  83. }
  84. if (res.data.default_channel) {
  85. const channel = {
  86. value: res.data.default_channel.id,
  87. label: res.data.default_channel.name,
  88. };
  89. setCurrChannel(channel);
  90. setChannelOption([channel]);
  91. }
  92. return {
  93. title: res.data.title,
  94. subtitle: res.data.subtitle,
  95. summary: res.data.summary ? res.data.summary : undefined,
  96. lang: res.data.lang,
  97. status: res.data.status,
  98. defaultChannel: res.data.default_channel?.id,
  99. };
  100. } else {
  101. return {
  102. title: "",
  103. subtitle: "",
  104. summary: "",
  105. lang: "",
  106. status: 0,
  107. defaultChannel: "",
  108. };
  109. }
  110. }}
  111. >
  112. <ProForm.Group>
  113. <ProFormText
  114. width="md"
  115. name="title"
  116. required
  117. label={intl.formatMessage({
  118. id: "forms.fields.title.label",
  119. })}
  120. rules={[
  121. {
  122. required: true,
  123. message: intl.formatMessage({
  124. id: "forms.message.title.required",
  125. }),
  126. },
  127. ]}
  128. />
  129. <ProFormText
  130. width="md"
  131. name="subtitle"
  132. label={intl.formatMessage({
  133. id: "forms.fields.subtitle.label",
  134. })}
  135. />
  136. </ProForm.Group>
  137. <ProForm.Group>
  138. <LangSelect width="md" />
  139. <PublicitySelect
  140. width="md"
  141. disable={["public_no_list"]}
  142. readonly={
  143. user?.roles?.includes("basic") ||
  144. data?.studio.roles?.includes("basic")
  145. ? true
  146. : false
  147. }
  148. />
  149. </ProForm.Group>
  150. <ProForm.Group>
  151. <ProFormSelect
  152. options={channelOption}
  153. width="md"
  154. name="defaultChannel"
  155. label={"默认版本"}
  156. showSearch
  157. debounceTime={300}
  158. request={async ({ keyWords }) => {
  159. console.log("keyWord", keyWords);
  160. if (typeof keyWords === "undefined") {
  161. return currChannel ? [currChannel] : [];
  162. }
  163. const url = `/v2/channel?view=studio&name=${studioName}`;
  164. console.log("url", url);
  165. const json = await get<IApiResponseChannelList>(url);
  166. const textbookList = json.data.rows.map((item) => {
  167. return {
  168. value: item.uid,
  169. label: `${item.studio.nickName}/${item.name}`,
  170. };
  171. });
  172. console.log("json", textbookList);
  173. return textbookList;
  174. }}
  175. />
  176. </ProForm.Group>
  177. <ProForm.Group>
  178. <Form.Item
  179. name="summary"
  180. label={intl.formatMessage({ id: "forms.fields.summary.label" })}
  181. >
  182. <MDEditor />
  183. </Form.Item>
  184. </ProForm.Group>
  185. </ProForm>
  186. ) : (
  187. <></>
  188. );
  189. };
  190. export default AnthologyInfoEditWidget;