CollaboratorAdd.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { message } from "antd";
  2. import { useIntl } from "react-intl";
  3. import {
  4. ProForm,
  5. ProFormDependency,
  6. ProFormInstance,
  7. ProFormSelect,
  8. } from "@ant-design/pro-components";
  9. import { post } from "../../request";
  10. import { TRole } from "../api/Auth";
  11. import { IShareRequest, IShareResponse } from "../api/Share";
  12. import { useRef } from "react";
  13. import { EResType } from "./Share";
  14. import UserSelect from "../template/UserSelect";
  15. import GroupSelect from "../template/GroupSelect";
  16. interface IWidget {
  17. resId: string;
  18. resType: EResType;
  19. onSuccess?: Function;
  20. }
  21. const Widget = ({ resId, resType, onSuccess }: IWidget) => {
  22. const roleList = ["manager", "editor", "reader"];
  23. const intl = useIntl();
  24. const formRef = useRef<ProFormInstance>();
  25. interface IFormData {
  26. userId: string[];
  27. groupId: string[];
  28. userType: string;
  29. role: TRole;
  30. }
  31. return (
  32. <ProForm<IFormData>
  33. formRef={formRef}
  34. onFinish={async (values: IFormData) => {
  35. // TODO
  36. console.log(values);
  37. if (typeof resId !== "undefined") {
  38. post<IShareRequest, IShareResponse>("/v2/share", {
  39. user_id:
  40. values.userType === "user" ? values.userId : values.groupId,
  41. user_type: values.userType,
  42. role: values.role,
  43. res_id: resId,
  44. res_type: resType,
  45. }).then((json) => {
  46. console.log("add member", json);
  47. if (json.ok) {
  48. if (typeof onSuccess !== "undefined") {
  49. onSuccess();
  50. }
  51. formRef.current?.resetFields(["userId"]);
  52. message.success(intl.formatMessage({ id: "flashes.success" }));
  53. }
  54. });
  55. }
  56. }}
  57. >
  58. <ProForm.Group>
  59. <ProFormSelect
  60. initialValue={"user"}
  61. name="userType"
  62. label={intl.formatMessage({ id: "forms.fields.role.label" })}
  63. allowClear={false}
  64. options={[
  65. {
  66. value: "user",
  67. label: intl.formatMessage({ id: "auth.type.user" }),
  68. },
  69. {
  70. value: "group",
  71. label: intl.formatMessage({ id: "auth.type.group" }),
  72. },
  73. ]}
  74. rules={[
  75. {
  76. required: true,
  77. message: intl.formatMessage({
  78. id: "forms.message.user.required",
  79. }),
  80. },
  81. ]}
  82. />
  83. </ProForm.Group>
  84. <ProForm.Group>
  85. <ProFormDependency name={["userType"]}>
  86. {({ userType }) => {
  87. if (userType === "user") {
  88. return <UserSelect name="userId" multiple={true} />;
  89. } else {
  90. return <GroupSelect name="groupId" multiple={true} />;
  91. }
  92. }}
  93. </ProFormDependency>
  94. <ProFormSelect
  95. name="role"
  96. initialValue={"reader"}
  97. label={intl.formatMessage({ id: "forms.fields.role.label" })}
  98. allowClear={false}
  99. options={roleList.map((item) => {
  100. return {
  101. value: item,
  102. label: intl.formatMessage({ id: "auth.role." + item }),
  103. };
  104. })}
  105. rules={[
  106. {
  107. required: true,
  108. message: intl.formatMessage({
  109. id: "forms.message.user.required",
  110. }),
  111. },
  112. ]}
  113. />
  114. </ProForm.Group>
  115. </ProForm>
  116. );
  117. };
  118. export default Widget;