CourseInvite.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { PlusOutlined } from "@ant-design/icons";
  2. import {
  3. ProFormInstance,
  4. ProFormSelect,
  5. StepsForm,
  6. } from "@ant-design/pro-components";
  7. import { Alert, Button, Modal, Result, message } from "antd";
  8. import { useRef, useState } from "react";
  9. import { useIntl } from "react-intl";
  10. import { get, post } from "../../request";
  11. import { ICourseMemberData, ICourseMemberResponse } from "../api/Course";
  12. import { IUserListResponse } from "../api/Auth";
  13. interface IFormData {
  14. userId: string;
  15. role: string;
  16. }
  17. interface IWidget {
  18. courseId?: string;
  19. onCreated?: Function;
  20. }
  21. const CourseInviteWidget = ({ courseId, onCreated }: IWidget) => {
  22. const intl = useIntl();
  23. const [visible, setVisible] = useState(false);
  24. const [curr, setCurr] = useState<ICourseMemberData>();
  25. const [userId, setUserId] = useState<string>();
  26. const formRef = useRef<ProFormInstance>();
  27. return (
  28. <>
  29. <Button type="primary" onClick={() => setVisible(true)}>
  30. <PlusOutlined />
  31. 邀请
  32. </Button>
  33. <Modal
  34. title="邀请"
  35. width={600}
  36. onCancel={() => setVisible(false)}
  37. open={visible}
  38. footer={false}
  39. destroyOnClose
  40. >
  41. <StepsForm<IFormData>
  42. formRef={formRef}
  43. onFinish={async (values) => {
  44. console.log(values);
  45. setVisible(false);
  46. message.success("提交成功");
  47. }}
  48. formProps={{
  49. validateMessages: {
  50. required: "此项为必填项",
  51. },
  52. }}
  53. >
  54. <StepsForm.StepForm
  55. name="base"
  56. title="选择用户"
  57. onFinish={async (values) => {
  58. setUserId(values.userId);
  59. const url = `/v2/course-member/${courseId}?user_uid=${values.userId}`;
  60. console.info("api request", url, values);
  61. const json = await get<ICourseMemberResponse>(url);
  62. if (json.ok) {
  63. setCurr(json.data);
  64. } else {
  65. setCurr(undefined);
  66. }
  67. return true;
  68. }}
  69. >
  70. <ProFormSelect
  71. width="sm"
  72. name="userId"
  73. label={intl.formatMessage({ id: "forms.fields.user.label" })}
  74. showSearch
  75. debounceTime={300}
  76. request={async ({ keyWords }) => {
  77. console.log("keyWord", keyWords);
  78. const json = await get<IUserListResponse>(
  79. `/v2/user?view=key&key=${keyWords}`
  80. );
  81. const userList = json.data.rows.map((item) => {
  82. return {
  83. value: item.id,
  84. label: `${item.userName}-${item.nickName}`,
  85. };
  86. });
  87. console.log("json", userList);
  88. return userList;
  89. }}
  90. placeholder={intl.formatMessage({
  91. id: "forms.message.user.required",
  92. })}
  93. rules={[
  94. {
  95. required: true,
  96. message: intl.formatMessage({
  97. id: "forms.message.user.required",
  98. }),
  99. },
  100. ]}
  101. />
  102. </StepsForm.StepForm>
  103. <StepsForm.StepForm
  104. name="checkbox"
  105. title="选择身份"
  106. onFinish={async (values) => {
  107. if (typeof courseId !== "undefined" && userId) {
  108. const url = "/v2/course-member";
  109. const data: ICourseMemberData = {
  110. user_id: userId,
  111. role: values.role,
  112. course_id: courseId,
  113. status: "invited",
  114. };
  115. console.info("api request", url, data);
  116. const json = await post<
  117. ICourseMemberData,
  118. ICourseMemberResponse
  119. >(url, data);
  120. console.info("add member api response", json);
  121. if (json.ok) {
  122. if (typeof onCreated !== "undefined") {
  123. onCreated();
  124. }
  125. } else {
  126. console.error(json.message);
  127. return false;
  128. }
  129. } else {
  130. return false;
  131. }
  132. return true;
  133. }}
  134. >
  135. {curr ? (
  136. <Alert
  137. message={`用户 ${curr?.user?.nickName} 身份 ${curr?.role} 状态 ${curr?.status} `}
  138. />
  139. ) : (
  140. <></>
  141. )}
  142. <ProFormSelect
  143. width="sm"
  144. name="role"
  145. label={intl.formatMessage({ id: "forms.fields.type.label" })}
  146. valueEnum={{
  147. student: intl.formatMessage({
  148. id: "forms.fields.student.label",
  149. }),
  150. assistant: intl.formatMessage({
  151. id: "forms.fields.assistant.label",
  152. }),
  153. manager: intl.formatMessage({
  154. id: "auth.role.manager",
  155. }),
  156. }}
  157. />
  158. </StepsForm.StepForm>
  159. <StepsForm.StepForm name="time" title="完成">
  160. <Result status="success" title="已经成功邀请" />
  161. </StepsForm.StepForm>
  162. </StepsForm>
  163. </Modal>
  164. </>
  165. );
  166. };
  167. export default CourseInviteWidget;