CollaboratorAdd.tsx 3.6 KB

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