SelectChannel.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { ModalForm, ProForm, ProFormSelect } from "@ant-design/pro-components";
  2. import { Alert, Button, message } from "antd";
  3. import { GlobalOutlined } from "@ant-design/icons";
  4. import { useAppSelector } from "../../hooks";
  5. import { currentUser as _currentUser } from "../../reducers/current-user";
  6. import { get, put } from "../../request";
  7. import type { IApiResponseChannelList } from "../../api/Channel";
  8. import { LockIcon } from "../../assets/icon";
  9. import type {
  10. ICourseMemberData,
  11. ICourseMemberResponse,
  12. } from "../../api/Course";
  13. import { useIntl } from "react-intl";
  14. interface IWidget {
  15. courseId?: string | null;
  16. exerciseId?: string;
  17. channel?: string;
  18. open?: boolean;
  19. onSelected?: () => void;
  20. onOpenChange?: (visible: boolean) => void;
  21. }
  22. const SelectChannelWidget = ({
  23. courseId,
  24. onSelected,
  25. onOpenChange,
  26. }: IWidget) => {
  27. const user = useAppSelector(_currentUser);
  28. const intl = useIntl();
  29. return (
  30. <Alert
  31. message={`请选择作业的存放位置`}
  32. type="warning"
  33. action={
  34. <ModalForm<{
  35. channel: string;
  36. }>
  37. title="请选择作业的存放位置"
  38. trigger={
  39. <Button type="primary">
  40. {intl.formatMessage({
  41. id: "buttons.select",
  42. })}
  43. </Button>
  44. }
  45. autoFocusFirstInput
  46. modalProps={{
  47. destroyOnClose: true,
  48. onCancel: () => console.log("run"),
  49. }}
  50. submitTimeout={20000}
  51. onFinish={async (values) => {
  52. if (user && courseId) {
  53. const url = `/v2/course-member_set-channel`;
  54. const data = {
  55. user_id: user.id,
  56. course_id: courseId,
  57. channel_id: values.channel,
  58. };
  59. console.info("course select channel api request", url, data);
  60. const json = await put<ICourseMemberData, ICourseMemberResponse>(
  61. url,
  62. data
  63. );
  64. console.info("course select channel api response", json);
  65. if (json.ok) {
  66. message.success("提交成功");
  67. if (typeof onSelected !== "undefined") {
  68. onSelected();
  69. }
  70. } else {
  71. message.error(json.message);
  72. return false;
  73. }
  74. } else {
  75. console.log("select channel error:", user, courseId);
  76. return false;
  77. }
  78. return true;
  79. }}
  80. onOpenChange={onOpenChange}
  81. >
  82. <div>
  83. 您还没有选择版本。您将用一个版本保存自己的作业。这个版本里面的内容将会被老师,助理老师看到。
  84. </div>
  85. <ProForm.Group>
  86. <ProFormSelect
  87. rules={[
  88. {
  89. required: true,
  90. },
  91. ]}
  92. request={async () => {
  93. const channelData = await get<IApiResponseChannelList>(
  94. `/v2/channel?view=studio&name=${user?.realName}`
  95. );
  96. const channel = channelData.data.rows.map((item) => {
  97. const icon =
  98. item.status === 30 ? <GlobalOutlined /> : <LockIcon />;
  99. return {
  100. value: item.uid,
  101. label: (
  102. <>
  103. {icon} {item.name}
  104. </>
  105. ),
  106. };
  107. });
  108. return channel;
  109. }}
  110. width="md"
  111. name="channel"
  112. label="版本风格"
  113. />
  114. </ProForm.Group>
  115. </ModalForm>
  116. }
  117. />
  118. );
  119. };
  120. export default SelectChannelWidget;