feat: improve code

This commit is contained in:
chenos 2022-03-02 20:08:11 +08:00
parent afab15327e
commit 271111902f
7 changed files with 89 additions and 97 deletions

View File

@ -72,7 +72,9 @@ export const collectionFieldSchema: ISchema = {
action: 'list',
params: {
pageSize: 50,
filter: {},
filter: {
'interface.$not': null,
},
sort: ['sort'],
appends: ['uiSchema'],
},

View File

@ -14,18 +14,20 @@ interface ResourceActionProviderProps {
const ResourceContext = createContext<any>(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 (
<ResourceContext.Provider value={{ type: 'collection', resource, collection }}>
<ResourceActionContext.Provider value={service}>
@ -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 <InternalCollectionResourceActionProvider {...props} collection={collection} />;
};
const AssociationResourceActionProvider = (props) => {
let { collection, association, request, uid } = props;
const { get } = useCollectionManager();

View File

@ -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<Field>();
return (
<div>
{toArr(field.value).map((record, index) => {
return (
<RowContext.Provider key={index} value={{ record, field, props }}>
<RecursionField
schema={fieldSchema}
onlyRenderProperties
filterProperties={(s) => {
return s['x-component'] === 'RecordPicker.SelectedItem';
}}
/>
</RowContext.Provider>
);
})}
<Space size={0} split={<span style={{ marginRight: 4, color: '#aaa' }}>, </span>}>
{toArr(field.value).map((record, index) => {
return (
<RowContext.Provider key={index} value={{ record, field, props }}>
<RecursionField
schema={fieldSchema}
onlyRenderProperties
filterProperties={(s) => {
return s['x-component'] === 'RecordPicker.SelectedItem';
}}
/>
</RowContext.Provider>
);
})}
</Space>
</div>
);
};
@ -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 (
<ActionContext.Provider value={{ visible, setVisible }}>
<Tag style={{ cursor: 'pointer' }} onClick={() => setVisible(true)}>
{ctx.record?.name}
</Tag>
<a style={{ cursor: 'pointer' }} onClick={() => setVisible(true)}>
{ctx.record[fieldNames?.label || 'id']}
</a>
<RecursionField onlyRenderProperties schema={fieldSchema}></RecursionField>
</ActionContext.Provider>
);

View File

@ -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')}

View File

@ -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) => {

View File

@ -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")}}',

View File

@ -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();
}