From 271111902f19c44518aac99c9bcb3a9148653ad2 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 2 Mar 2022 20:08:11 +0800 Subject: [PATCH] feat: improve code --- .../Configuration/schemas/collectionFields.ts | 4 +- .../ResourceActionProvider.tsx | 32 +++++--- .../antd/record-picker/RecordPicker.tsx | 37 ++++----- .../Initializers/GridFormItemInitializers.tsx | 23 ------ .../schema-initializer/Initializers/utils.ts | 76 ++++++++++--------- packages/plugin-acl/src/collections/roles.ts | 2 + packages/plugin-users/src/actions/users.ts | 12 +-- 7 files changed, 89 insertions(+), 97 deletions(-) diff --git a/packages/client/src/collection-manager/Configuration/schemas/collectionFields.ts b/packages/client/src/collection-manager/Configuration/schemas/collectionFields.ts index f394bcda7..6e07e620f 100644 --- a/packages/client/src/collection-manager/Configuration/schemas/collectionFields.ts +++ b/packages/client/src/collection-manager/Configuration/schemas/collectionFields.ts @@ -72,7 +72,9 @@ export const collectionFieldSchema: ISchema = { action: 'list', params: { pageSize: 50, - filter: {}, + filter: { + 'interface.$not': null, + }, sort: ['sort'], appends: ['uiSchema'], }, diff --git a/packages/client/src/collection-manager/ResourceActionProvider.tsx b/packages/client/src/collection-manager/ResourceActionProvider.tsx index 8dbe04876..31e187076 100644 --- a/packages/client/src/collection-manager/ResourceActionProvider.tsx +++ b/packages/client/src/collection-manager/ResourceActionProvider.tsx @@ -14,18 +14,20 @@ interface ResourceActionProviderProps { const ResourceContext = createContext(null); -const CollectionResourceActionProvider = (props) => { +const InternalCollectionResourceActionProvider = (props) => { let { collection, request, uid } = props; - const { get } = useCollectionManager(); const api = useAPIClient(); - const service = useRequest(request, { uid }); + const service = useRequest( + { + ...request, + params: { + ...request?.params, + appends: collection.fields.filter(field => field.target).map((field) => field.name), + }, + }, + { uid }, + ); const resource = api.resource(request.resource); - if (typeof collection === 'string') { - collection = get(collection); - } - if (!collection) { - return null; - } return ( @@ -35,6 +37,18 @@ const CollectionResourceActionProvider = (props) => { ); }; +const CollectionResourceActionProvider = (props) => { + let { collection, request, uid } = props; + const { getCollection } = useCollectionManager(); + if (typeof collection === 'string') { + collection = getCollection(collection); + } + if (!collection) { + return null; + } + return ; +}; + const AssociationResourceActionProvider = (props) => { let { collection, association, request, uid } = props; const { get } = useCollectionManager(); diff --git a/packages/client/src/schema-component/antd/record-picker/RecordPicker.tsx b/packages/client/src/schema-component/antd/record-picker/RecordPicker.tsx index 878cc2660..68f23fd67 100644 --- a/packages/client/src/schema-component/antd/record-picker/RecordPicker.tsx +++ b/packages/client/src/schema-component/antd/record-picker/RecordPicker.tsx @@ -13,7 +13,7 @@ import { useForm } from '@formily/react'; import { toArr } from '@formily/shared'; -import { Button, Drawer, Select, Space, Tag } from 'antd'; +import { Button, Drawer, Select, Space } from 'antd'; import React, { createContext, useContext, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useAttach } from '../../hooks/useAttach'; @@ -113,19 +113,21 @@ const ReadPrettyRecordPicker: React.FC = (props) => { const field = useField(); return (
- {toArr(field.value).map((record, index) => { - return ( - - { - return s['x-component'] === 'RecordPicker.SelectedItem'; - }} - /> - - ); - })} + , }> + {toArr(field.value).map((record, index) => { + return ( + + { + return s['x-component'] === 'RecordPicker.SelectedItem'; + }} + /> + + ); + })} +
); }; @@ -185,11 +187,12 @@ RecordPicker.SelectedItem = () => { const ctx = useContext(RowContext); const fieldSchema = useFieldSchema(); const [visible, setVisible] = useState(false); + const fieldNames = ctx.field.componentProps.fieldNames; return ( - setVisible(true)}> - {ctx.record?.name} - + setVisible(true)}> + {ctx.record[fieldNames?.label || 'id']} + ); diff --git a/packages/client/src/schema-initializer/Initializers/GridFormItemInitializers.tsx b/packages/client/src/schema-initializer/Initializers/GridFormItemInitializers.tsx index a42b5281a..048f42d23 100644 --- a/packages/client/src/schema-initializer/Initializers/GridFormItemInitializers.tsx +++ b/packages/client/src/schema-initializer/Initializers/GridFormItemInitializers.tsx @@ -1,4 +1,3 @@ -import flat from 'flat'; import React from 'react'; import { useTranslation } from 'react-i18next'; import { SchemaInitializer } from '../SchemaInitializer'; @@ -34,28 +33,6 @@ export const GridFormItemInitializers = (props: any) => { }, }, }, - { - type: 'item', - title: t('Add Filter'), - component: 'GeneralInitializer', - schema: { - name: 'filter', - type: 'object', - default: flat.unflatten({ - $or: [ - { 'aa.$eq': 'b' }, - { 'bb.field.$eq': ['aabb', 'aaa'] }, - { - 'bb.field': { - $eq: ['aabb', 'aaa'], - }, - }, - ], - }), - 'x-component': 'Filter', - 'x-component-props': {}, - }, - }, ]} > {t('Configure fields')} diff --git a/packages/client/src/schema-initializer/Initializers/utils.ts b/packages/client/src/schema-initializer/Initializers/utils.ts index 8e01fcca2..bc1d032be 100644 --- a/packages/client/src/schema-initializer/Initializers/utils.ts +++ b/packages/client/src/schema-initializer/Initializers/utils.ts @@ -59,7 +59,7 @@ const findTableColumn = (schema: Schema, key: string, action: string, deepth: nu export const useTableColumnInitializerFields = () => { const { name, fields } = useCollection(); return fields - .filter((field) => field?.interface !== 'subTable') + .filter((field) => field?.interface && field?.interface !== 'subTable') .map((field) => { if (field.interface === 'linkTo') { return { @@ -93,14 +93,47 @@ export const useTableColumnInitializerFields = () => { export const useFormItemInitializerFields = () => { const { name, fields } = useCollection(); - return fields?.map((field) => { - if (field.interface === 'linkTo') { + return fields + ?.filter((field) => field?.interface) + ?.map((field) => { + if (field.interface === 'linkTo') { + return { + type: 'item', + title: field?.uiSchema?.title || field.name, + component: 'LinkToFieldInitializer', + remove: removeGridFormItem, + field, + schema: { + name: field.name, + 'x-designer': 'FormItem.Designer', + 'x-component': 'CollectionField', + 'x-decorator': 'FormItem', + 'x-collection-field': `${name}.${field.name}`, + }, + } as SchemaInitializerItemOptions; + } + if (field.interface === 'subTable') { + return { + type: 'item', + title: field?.uiSchema?.title || field.name, + component: 'SubTableFieldInitializer', + remove: removeGridFormItem, + field, + schema: { + type: 'void', + name: field.name, + 'x-designer': 'FormItem.Designer', + 'x-component': 'div', + 'x-decorator': 'FormItem', + 'x-collection-field': `${name}.${field.name}`, + }, + } as SchemaInitializerItemOptions; + } return { type: 'item', title: field?.uiSchema?.title || field.name, - component: 'LinkToFieldInitializer', + component: 'CollectionFieldInitializer', remove: removeGridFormItem, - field, schema: { name: field.name, 'x-designer': 'FormItem.Designer', @@ -109,38 +142,7 @@ export const useFormItemInitializerFields = () => { 'x-collection-field': `${name}.${field.name}`, }, } as SchemaInitializerItemOptions; - } - if (field.interface === 'subTable') { - return { - type: 'item', - title: field?.uiSchema?.title || field.name, - component: 'SubTableFieldInitializer', - remove: removeGridFormItem, - field, - schema: { - type: 'void', - name: field.name, - 'x-designer': 'FormItem.Designer', - 'x-component': 'div', - 'x-decorator': 'FormItem', - 'x-collection-field': `${name}.${field.name}`, - }, - } as SchemaInitializerItemOptions; - } - return { - type: 'item', - title: field?.uiSchema?.title || field.name, - component: 'CollectionFieldInitializer', - remove: removeGridFormItem, - schema: { - name: field.name, - 'x-designer': 'FormItem.Designer', - 'x-component': 'CollectionField', - 'x-decorator': 'FormItem', - 'x-collection-field': `${name}.${field.name}`, - }, - } as SchemaInitializerItemOptions; - }); + }); }; const findSchema = (schema: Schema, key: string, action: string) => { diff --git a/packages/plugin-acl/src/collections/roles.ts b/packages/plugin-acl/src/collections/roles.ts index d4f2c73f6..5adf61ce3 100644 --- a/packages/plugin-acl/src/collections/roles.ts +++ b/packages/plugin-acl/src/collections/roles.ts @@ -11,6 +11,7 @@ export default { name: 'name', prefix: 'r_', primaryKey: true, + interface: 'input', uiSchema: { type: 'string', title: '{{t("Role UID")}}', @@ -21,6 +22,7 @@ export default { type: 'string', name: 'title', unique: true, + interface: 'input', uiSchema: { type: 'string', title: '{{t("Role name")}}', diff --git a/packages/plugin-users/src/actions/users.ts b/packages/plugin-users/src/actions/users.ts index 53cb765f4..8b077e1c1 100644 --- a/packages/plugin-users/src/actions/users.ts +++ b/packages/plugin-users/src/actions/users.ts @@ -51,16 +51,8 @@ export async function signout(ctx: Context, next: Next) { export async function signup(ctx: Context, next: Next) { const User = ctx.db.getRepository('users'); const { values } = ctx.action.params; - try { - const user = await User.create({ values }); - ctx.body = user; - } catch (error) { - if (error.errors) { - ctx.throw(401, error.errors.map((data) => data.message).join(', ')); - } else { - ctx.throw(401, '注册失败'); - } - } + const user = await User.create({ values }); + ctx.body = user; await next(); }