优化代码

pull/47/head
smallbun 2023-09-10 22:22:22 +08:00
parent 6caeab68e6
commit 337f3ecbc7
7 changed files with 163 additions and 15 deletions

View File

@ -18,7 +18,7 @@
import { getUserGroupList } from '@/services/account';
import type { SelectProps } from 'antd';
import { Select, Spin } from 'antd';
import { ReactText, useState } from 'react';
import { useState } from 'react';
import { useAsyncEffect } from 'ahooks';
import { SortOrder } from 'antd/es/table/interface';
import { RequestData } from '@ant-design/pro-components';
@ -35,9 +35,9 @@ export type UserGroupSelectProps<ValueType = UserData> = Omit<
>;
async function getAllUserGroupList(
params?: Record<string, any>,
sort?: Record<string, SortOrder>,
filter?: Record<string, ReactText[] | null>,
params: Record<string, any>,
sort: Record<string, SortOrder>,
filter: Record<string, (string | number)[] | null>,
): Promise<RequestData<AccountAPI.ListUserGroup>> {
let pageSize = 100,
current = 1;
@ -79,7 +79,7 @@ const UserGroupSelect = (props: UserGroupSelectProps) => {
useAsyncEffect(async () => {
setFetching(true);
const { success, data } = await getAllUserGroupList().finally(() => {
const { success, data } = await getAllUserGroupList({}, {}, {}).finally(() => {
setFetching(false);
});
if (success && data) {

View File

@ -78,9 +78,9 @@ const UpdateModel = (props: UpdateFormProps<AccountAPI.UpdateOrganization>) => {
open={visible}
onFinish={async (values: AccountAPI.UpdateOrganization) => {
setUpdateLoading(true);
const result = await onFinish(values);
setUpdateLoading(false);
return !!result;
await onFinish(values).finally(() => {
setUpdateLoading(false);
});
}}
>
<Skeleton loading={loading} active={true}>

View File

@ -61,7 +61,7 @@ export default (props: UpdateOrganizationFormProps<AccountAPI.UpdateOrganization
*/
const cancel = async () => {
if (onCancel) {
await onCancel();
onCancel();
}
form.resetFields();
};

View File

@ -0,0 +1,149 @@
/*
* eiam-console - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { getPermissionRole, permissionRoleParamCheck } from '../../service';
import type { ProFormInstance } from '@ant-design/pro-components';
import { ModalForm, ProFormText, ProFormTextArea } from '@ant-design/pro-components';
import { Spin } from 'antd';
import React, { useRef, useState } from 'react';
import { useAsyncEffect } from 'ahooks';
import Paragraph from 'antd/es/typography/Paragraph';
type UpdateFormProps = {
/**
* ID
*/
id: string | undefined;
/**
*
*/
open: boolean;
/**
*
*/
onCancel: () => void;
/**
*
*/
onFinish?: (formData: Record<string, string>) => Promise<boolean | void>;
};
const UpdateRole: React.FC<UpdateFormProps> = (props) => {
const { open, onCancel, onFinish, id } = props;
const [loading, setLoading] = useState<boolean>(false);
const formRef = useRef<ProFormInstance>();
useAsyncEffect(async () => {
if (open && id) {
setLoading(true);
const { success, result } = await getPermissionRole(id);
if (success && result) {
setLoading(false);
return;
}
}
}, [id, open]);
return (
<ModalForm
title="修改角色"
width={600}
modalProps={{
onCancel: onCancel,
destroyOnClose: true,
}}
layout={'horizontal'}
labelCol={{ span: 4 }}
wrapperCol={{ span: 19 }}
labelAlign={'right'}
open={open}
key={'update'}
onFinish={async (values) => {
setLoading(true);
const result = await onFinish?.(values);
setLoading(false);
return result;
}}
>
<Spin spinning={loading}>
<ProFormText name="id" hidden />
<ProFormText name="appId" hidden />
<ProFormText
name="name"
label="名称"
rules={[
{
required: true,
message: '请输入角色名称',
},
{
validator: async (rule, value) => {
if (!value) {
return Promise.resolve();
}
setLoading(true);
const { success, result } = await permissionRoleParamCheck(
formRef.current?.getFieldValue('appId'),
'NAME',
value,
id,
);
setLoading(false);
if (!success) {
return Promise.reject<any>();
}
if (!result) {
return Promise.reject<any>(new Error('手机号已存在'));
}
},
validateTrigger: ['onBlur'],
},
]}
placeholder="请输入角色名称"
/>
<ProFormText
name="code"
label="标识"
placeholder="请输入角色标识"
readonly
proFieldProps={{
render: (value: string) => {
return (
value && (
<Paragraph copyable={{ text: value }} style={{ marginBottom: '0' }}>
<span
dangerouslySetInnerHTML={{
__html: `<span>${value}</span>`,
}}
/>
</Paragraph>
)
);
},
}}
extra="角色编码在当前应用中的唯一标识,不能重复,仅支持英文、数字、下划线,创建后不可修改。"
/>
<ProFormTextArea
name="remark"
fieldProps={{ rows: 2, maxLength: 20, showCount: false }}
label="描述"
placeholder="请输入角色描述"
/>
</Spin>
</ModalForm>
);
};
export default UpdateRole;

View File

@ -74,9 +74,9 @@ export default (props: CreateDrawerProps) => {
scrollToFirstError
onFinish={async (values: Record<string, string>) => {
setUpdateLoading(true);
const result = await onFinish(values);
setUpdateLoading(false);
return !!result;
await onFinish(values).finally(() => {
setUpdateLoading(false);
});
}}
open={visible}
>

View File

@ -18,7 +18,6 @@
import type { SortOrder } from 'antd/es/table/interface';
import yaml from 'js-yaml';
import { parse } from 'querystring';
import type { ReactText } from 'react';
import { history, matchPath } from '@umijs/max';
import YAML from 'yaml';
import { PhoneNumber } from 'google-libphonenumber';
@ -106,7 +105,7 @@ export const sortParamConverter = (value: Record<string, SortOrder> | undefined)
*
* @param value
*/
export const filterParamConverter = (value: Record<string, ReactText[] | null> | undefined) => {
export const filterParamConverter = (value: Record<string, (string | number)[] | null>) => {
const param: Record<string, any> = {};
if (value)
Object.entries(value).forEach(([key], index) => {

View File

@ -94,7 +94,7 @@ export const sortParamConverter = (value: Record<string, SortOrder> | undefined)
*
* @param value
*/
export const filterParamConverter = (value: Record<string, ReactText[] | null> | undefined) => {
export const filterParamConverter = (value: Record<string, Record<string, string | number[] | null>[] | null> | undefined) => {
const param: Record<string, any> = {};
if (value)
Object.entries(value).forEach(([key], index) => {