import { Button, Card, Input, message, Modal } from 'antd';
import { ProTable } from '@ant-design/pro-table';
import { ProForm, ProFormDateTimePicker } from '@ant-design/pro-form';
import type { ProColumns } from '@ant-design/pro-table';
import { FormattedMessage, useIntl } from 'umi';
import { bindUsers, validateUsersRequest } from '../server';
import { memo, useState } from 'react';
import { useGetAppId } from '../utils/hooks';
import { reloadReactNode } from '../utils/jsxformat';
import { PlusOutlined } from '@ant-design/icons';
import { formatUserRender } from '../utils';
import moment from 'moment';
/**
* @description 这个组件用来添加用户的组件
*/
interface AddUserProps {
/**
* @description 选择类型,目前只支持四种类型的数据添加用户
*/
type: 'feature' | 'option' | 'policy' | 'role'; // 选择类型
/**
* @description 选择什么type,就传入对应type的id。比如选了feature添加用户,那么就要传入featureId进来。
*/
id: number; // 默认参数
/**
* @description type选了option,就需要传入flagId。
*/
flagId?: number;
/**
* @description 在添加用户成功后回调的函数,可不传。
*/
reload?: () => void;
/**
* @description 默认是新增按钮。
* @default ()
*/
children?: React.ReactNode;
}
const AddUser = ({ id, flagId, type, reload, children }: AddUserProps) => {
const { formatMessage } = useIntl();
const [selectedRowKeys, setSelectedRowKeys] = useState([]);
const [open, setOpen] = useState(false);
const appId = useGetAppId();
const [form] = ProForm.useForm();
const columns: ProColumns[] = [
{
title: ,
dataIndex: 'accounts',
valueType: 'textarea',
hideInTable: true,
fieldProps: {
placeholder: formatMessage({ id: 'component.user.add.user.id.placeholder' }),
rows: 4,
},
colSize: 2,
renderFormItem: () => ,
},
{
title: ,
dataIndex: 'id',
ellipsis: true,
search: false,
hideInSearch: false,
render: (text, record) => formatUserRender(record),
},
];
const tableExtraRender = () => {
return (
null }}>
{
return current && current < moment().startOf('day');
},
}}
label={
}
placeholder={formatMessage({ id: 'component.user.add.user.expires.placeholder' })}
/>
);
};
const triggerRender = () => {
return reloadReactNode(
,
setOpen,
children,
);
};
return (
<>
}
width="900px"
open={open}
destroyOnClose={true}
onCancel={() => {
setOpen(false);
}}
onOk={async () => {
const { expires } = form.getFieldsValue();
if (selectedRowKeys?.length < 1) {
return;
}
try {
await bindUsers(
type,
selectedRowKeys?.map((userId) => ({
appId,
userId,
expiredAt: expires ? Math.floor(moment(expires).valueOf() / 1000) : expires,
[`${type}Id`]: id,
flagId,
})),
);
reload?.();
setOpen(false);
message.success(formatMessage({ id: 'component.user.add.user.success' }));
} catch (error) {}
}}
>
{
const req = await validateUsersRequest({
accounts: data.accounts?.trim()?.split(',') || [],
appId,
[`${type}Id`]: id,
flagId,
});
setSelectedRowKeys(req?.map((d) => d.id) || []);
return { data: req };
}}
/>
{triggerRender()}
>
);
};
export default memo(AddUser);