| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- import { message } from "antd";
- import { useIntl } from "react-intl";
- import {
- ProForm,
- ProFormDependency,
- type ProFormInstance,
- ProFormSelect,
- } from "@ant-design/pro-components";
- import { post } from "../../request";
- import type { TRole } from "../../api/Auth";
- import type { IShareRequest, IShareResponse } from "../../api/Share";
- import { useRef } from "react";
- import { EResType } from "./Share";
- import UserSelect from "../template/UserSelect";
- import GroupSelect from "../template/GroupSelect";
- interface IWidget {
- resId: string;
- resType: EResType;
- onSuccess?: Function;
- }
- const CollaboratorAddWidget = ({ resId, resType, onSuccess }: IWidget) => {
- const roleList = ["editor", "reader"];
- const intl = useIntl();
- const formRef = useRef<ProFormInstance | undefined>(undefined);
- interface IFormData {
- userId: string[];
- groupId: string[];
- userType: string;
- role: TRole;
- }
- return (
- <ProForm<IFormData>
- formRef={formRef}
- onFinish={async (values: IFormData) => {
- if (typeof resId !== "undefined") {
- const postData: IShareRequest = {
- user_id:
- values.userType === "user" ? values.userId : values.groupId,
- user_type: values.userType,
- role: values.role,
- res_id: resId,
- res_type: resType,
- };
- const url = "/v2/share";
- console.info("share api request", url, postData);
- post<IShareRequest, IShareResponse>(url, postData).then((json) => {
- console.debug("share api response", json);
- if (json.ok) {
- if (typeof onSuccess !== "undefined") {
- onSuccess();
- }
- formRef.current?.resetFields(["userId"]);
- message.success(intl.formatMessage({ id: "flashes.success" }));
- }
- });
- }
- }}
- >
- <ProForm.Group>
- <ProFormSelect
- initialValue={"user"}
- name="userType"
- label={intl.formatMessage({ id: "forms.fields.type.label" })}
- allowClear={false}
- options={[
- {
- value: "user",
- label: intl.formatMessage({ id: "auth.type.user" }),
- },
- {
- value: "group",
- label: intl.formatMessage({ id: "auth.type.group" }),
- },
- ]}
- rules={[
- {
- required: true,
- message: intl.formatMessage({
- id: "forms.message.user.required",
- }),
- },
- ]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormDependency name={["userType"]}>
- {({ userType }) => {
- if (userType === "user") {
- return <UserSelect name="userId" multiple={true} />;
- } else {
- return <GroupSelect name="groupId" multiple={true} />;
- }
- }}
- </ProFormDependency>
- <ProFormSelect
- name="role"
- initialValue={"reader"}
- label={intl.formatMessage({ id: "forms.fields.role.label" })}
- allowClear={false}
- options={roleList.map((item) => {
- return {
- value: item,
- label: intl.formatMessage({ id: "auth.role." + item }),
- };
- })}
- rules={[
- {
- required: true,
- message: intl.formatMessage({
- id: "forms.message.user.required",
- }),
- },
- ]}
- />
- </ProForm.Group>
- </ProForm>
- );
- };
- export default CollaboratorAddWidget;
|