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', action: 'list',
params: { params: {
pageSize: 50, pageSize: 50,
filter: {}, filter: {
'interface.$not': null,
},
sort: ['sort'], sort: ['sort'],
appends: ['uiSchema'], appends: ['uiSchema'],
}, },

View File

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

View File

@ -13,7 +13,7 @@ import {
useForm useForm
} from '@formily/react'; } from '@formily/react';
import { toArr } from '@formily/shared'; 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 React, { createContext, useContext, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useAttach } from '../../hooks/useAttach'; import { useAttach } from '../../hooks/useAttach';
@ -113,19 +113,21 @@ const ReadPrettyRecordPicker: React.FC = (props) => {
const field = useField<Field>(); const field = useField<Field>();
return ( return (
<div> <div>
{toArr(field.value).map((record, index) => { <Space size={0} split={<span style={{ marginRight: 4, color: '#aaa' }}>, </span>}>
return ( {toArr(field.value).map((record, index) => {
<RowContext.Provider key={index} value={{ record, field, props }}> return (
<RecursionField <RowContext.Provider key={index} value={{ record, field, props }}>
schema={fieldSchema} <RecursionField
onlyRenderProperties schema={fieldSchema}
filterProperties={(s) => { onlyRenderProperties
return s['x-component'] === 'RecordPicker.SelectedItem'; filterProperties={(s) => {
}} return s['x-component'] === 'RecordPicker.SelectedItem';
/> }}
</RowContext.Provider> />
); </RowContext.Provider>
})} );
})}
</Space>
</div> </div>
); );
}; };
@ -185,11 +187,12 @@ RecordPicker.SelectedItem = () => {
const ctx = useContext(RowContext); const ctx = useContext(RowContext);
const fieldSchema = useFieldSchema(); const fieldSchema = useFieldSchema();
const [visible, setVisible] = useState(false); const [visible, setVisible] = useState(false);
const fieldNames = ctx.field.componentProps.fieldNames;
return ( return (
<ActionContext.Provider value={{ visible, setVisible }}> <ActionContext.Provider value={{ visible, setVisible }}>
<Tag style={{ cursor: 'pointer' }} onClick={() => setVisible(true)}> <a style={{ cursor: 'pointer' }} onClick={() => setVisible(true)}>
{ctx.record?.name} {ctx.record[fieldNames?.label || 'id']}
</Tag> </a>
<RecursionField onlyRenderProperties schema={fieldSchema}></RecursionField> <RecursionField onlyRenderProperties schema={fieldSchema}></RecursionField>
</ActionContext.Provider> </ActionContext.Provider>
); );

View File

@ -1,4 +1,3 @@
import flat from 'flat';
import React from 'react'; import React from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { SchemaInitializer } from '../SchemaInitializer'; 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')} {t('Configure fields')}

View File

@ -59,7 +59,7 @@ const findTableColumn = (schema: Schema, key: string, action: string, deepth: nu
export const useTableColumnInitializerFields = () => { export const useTableColumnInitializerFields = () => {
const { name, fields } = useCollection(); const { name, fields } = useCollection();
return fields return fields
.filter((field) => field?.interface !== 'subTable') .filter((field) => field?.interface && field?.interface !== 'subTable')
.map((field) => { .map((field) => {
if (field.interface === 'linkTo') { if (field.interface === 'linkTo') {
return { return {
@ -93,14 +93,47 @@ export const useTableColumnInitializerFields = () => {
export const useFormItemInitializerFields = () => { export const useFormItemInitializerFields = () => {
const { name, fields } = useCollection(); const { name, fields } = useCollection();
return fields?.map((field) => { return fields
if (field.interface === 'linkTo') { ?.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 { return {
type: 'item', type: 'item',
title: field?.uiSchema?.title || field.name, title: field?.uiSchema?.title || field.name,
component: 'LinkToFieldInitializer', component: 'CollectionFieldInitializer',
remove: removeGridFormItem, remove: removeGridFormItem,
field,
schema: { schema: {
name: field.name, name: field.name,
'x-designer': 'FormItem.Designer', 'x-designer': 'FormItem.Designer',
@ -109,38 +142,7 @@ export const useFormItemInitializerFields = () => {
'x-collection-field': `${name}.${field.name}`, 'x-collection-field': `${name}.${field.name}`,
}, },
} as SchemaInitializerItemOptions; } 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) => { const findSchema = (schema: Schema, key: string, action: string) => {

View File

@ -11,6 +11,7 @@ export default {
name: 'name', name: 'name',
prefix: 'r_', prefix: 'r_',
primaryKey: true, primaryKey: true,
interface: 'input',
uiSchema: { uiSchema: {
type: 'string', type: 'string',
title: '{{t("Role UID")}}', title: '{{t("Role UID")}}',
@ -21,6 +22,7 @@ export default {
type: 'string', type: 'string',
name: 'title', name: 'title',
unique: true, unique: true,
interface: 'input',
uiSchema: { uiSchema: {
type: 'string', type: 'string',
title: '{{t("Role name")}}', 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) { export async function signup(ctx: Context, next: Next) {
const User = ctx.db.getRepository('users'); const User = ctx.db.getRepository('users');
const { values } = ctx.action.params; const { values } = ctx.action.params;
try { const user = await User.create({ values });
const user = await User.create({ values }); ctx.body = user;
ctx.body = user;
} catch (error) {
if (error.errors) {
ctx.throw(401, error.errors.map((data) => data.message).join(', '));
} else {
ctx.throw(401, '注册失败');
}
}
await next(); await next();
} }