Edit.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { useIntl } from "react-intl";
  2. import {
  3. ProForm,
  4. ProFormText,
  5. ProFormTextArea,
  6. } from "@ant-design/pro-components";
  7. import { Alert, message } from "antd";
  8. import type {
  9. IApiResponseChannel,
  10. IApiResponseChannelData,
  11. } from "../../api/Channel";
  12. import { get, put } from "../../request";
  13. import ChannelTypeSelect from "./ChannelTypeSelect";
  14. import LangSelect from "../general/LangSelect";
  15. import PublicitySelect from "../studio/PublicitySelect";
  16. import { useState } from "react";
  17. import { useAppSelector } from "../../hooks";
  18. import { currentUser } from "../../reducers/current-user";
  19. interface IFormData {
  20. name: string;
  21. type: string;
  22. lang: string;
  23. summary: string;
  24. status: number;
  25. studio: string;
  26. isSystem: boolean;
  27. }
  28. interface IWidget {
  29. studioName?: string;
  30. channelId?: string;
  31. onLoad?: Function;
  32. }
  33. const EditWidget = ({ studioName, channelId, onLoad }: IWidget) => {
  34. const intl = useIntl();
  35. const [isSystem, setIsSystem] = useState<boolean>();
  36. const [data, setData] = useState<IApiResponseChannelData>();
  37. const user = useAppSelector(currentUser);
  38. return (
  39. <>
  40. {isSystem ? (
  41. <Alert type="warning" message={"此版本为系统建立。不能修改,删除。"} />
  42. ) : (
  43. <></>
  44. )}
  45. <ProForm<IFormData>
  46. submitter={{
  47. resetButtonProps: { disabled: isSystem ? true : false },
  48. submitButtonProps: { disabled: isSystem ? true : false },
  49. }}
  50. onFinish={async (values: IFormData) => {
  51. console.log(values);
  52. const res = await put(`/v2/channel/${channelId}`, values);
  53. console.log(res);
  54. message.success(intl.formatMessage({ id: "flashes.success" }));
  55. }}
  56. formKey="channel_edit"
  57. request={async () => {
  58. const res = await get<IApiResponseChannel>(
  59. `/v2/channel/${channelId}`
  60. );
  61. if (res.ok === false) {
  62. return {
  63. name: "",
  64. type: "",
  65. lang: "",
  66. summary: "",
  67. status: 0,
  68. studio: "",
  69. isSystem: true,
  70. };
  71. }
  72. setData(res.data);
  73. if (typeof onLoad !== "undefined") {
  74. onLoad(res.data);
  75. }
  76. setIsSystem(res.data.is_system);
  77. return {
  78. name: res.data.name,
  79. type: res.data.type,
  80. lang: res.data.lang,
  81. summary: res.data.summary,
  82. status: res.data.status,
  83. studio: studioName ? studioName : "",
  84. isSystem: res.data.is_system,
  85. };
  86. }}
  87. >
  88. <ProForm.Group>
  89. <ProFormText
  90. width="md"
  91. name="name"
  92. readonly={isSystem ? true : false}
  93. required
  94. label={intl.formatMessage({ id: "channel.name" })}
  95. rules={[
  96. {
  97. required: true,
  98. },
  99. ]}
  100. />
  101. </ProForm.Group>
  102. <ProForm.Group>
  103. <ChannelTypeSelect readonly={isSystem ? true : false} />
  104. <LangSelect readonly={isSystem ? true : false} />
  105. </ProForm.Group>
  106. <ProForm.Group>
  107. <PublicitySelect
  108. readonly={
  109. isSystem || user?.roles?.includes("basic") || data?.status === 5
  110. ? true
  111. : false
  112. }
  113. disable={["public_no_list"]}
  114. />
  115. </ProForm.Group>
  116. <ProForm.Group>
  117. <ProFormTextArea
  118. readonly={isSystem ? true : false}
  119. width="md"
  120. name="summary"
  121. label="简介"
  122. />
  123. </ProForm.Group>
  124. </ProForm>
  125. </>
  126. );
  127. };
  128. export default EditWidget;