feat: improve code

This commit is contained in:
chenos 2022-02-13 11:23:40 +08:00
parent 8a791f37aa
commit 01e5e1cbac
12 changed files with 70 additions and 30 deletions

View File

@ -63,7 +63,7 @@ const plugins = [
'@nocobase/plugin-file-manager',
'@nocobase/plugin-system-settings',
'@nocobase/plugin-users',
'@nocobase/plugin-acl',
// '@nocobase/plugin-acl',
'@nocobase/plugin-china-region',
];

View File

@ -1,10 +1,9 @@
import { APIClient } from '@nocobase/client';
import mock from './mock';
const apiClient = new APIClient({
baseURL: `${location.protocol}//${location.host}/api/`,
baseURL: `http://localhost:3000/api/`,
});
mock(apiClient);
// mock(apiClient);
export default apiClient;

View File

@ -47,7 +47,7 @@ const providers = [
const App = compose(...providers)(() => {
const { data, loading } = useRequest({
url: 'routes:getAccessible',
url: 'uiRoutes:getAccessible',
});
if (loading) {
return <Spin />;

View File

@ -9,7 +9,7 @@ export default (apiClient: APIClient) => {
data: { lang: 'en-US' },
});
mock.onGet('/system_settings:get').reply(200, {
mock.onGet('/systemSettings:get/1').reply(200, {
data: {
title: 'NocoBase',
},

View File

@ -9,7 +9,7 @@ export default (apiClient: APIClient) => {
data: { lang: 'en-US' },
});
mock.onGet('/system_settings:get').reply(200, {
mock.onGet('/systemSettings:get/1').reply(200, {
data: {
title: 'NocoBase',
},
@ -135,6 +135,33 @@ export default (apiClient: APIClient) => {
return [200, response];
});
mock.onGet(/\/ui_schemas\:getProperties\/(\w+)/).reply(function (config) {
// const name = config.url.split('/').pop();
// console.log(name);
// if (jsonSchema[name]) {
// return [200, { data: jsonSchema[name] }];
// }
const response = {
data: {
type: 'void',
name: uid(),
'x-uid': uid(),
'x-component': 'Page',
properties: {
[uid()]: {
type: 'void',
name: 'grid1',
'x-component': 'Grid',
'x-item-initializer': 'Grid.AddBlockItem',
'x-uid': uid(),
properties: {},
},
},
},
};
return [200, response];
});
mock.onGet('/routes:getAccessible').reply(200, {
data: [
{

View File

@ -75,7 +75,7 @@ export function AdminLayout(props: any) {
<Layout>
<Layout.Sider style={{ display: 'none' }} theme={'light'} ref={sideMenuRef}></Layout.Sider>
<Layout.Content>
<RemoteSchemaComponent uid={match.params.name} />
<RemoteSchemaComponent onlyRenderProperties uid={match.params.name} />
</Layout.Content>
</Layout>
</Layout>

View File

@ -20,7 +20,7 @@ const useSideMenuRef = () => {
return;
}
return scope[scopeKey];
}
};
export const Menu: ComposedMenu = observer((props) => {
let {
@ -32,7 +32,6 @@ export const Menu: ComposedMenu = observer((props) => {
defaultOpenKeys: dOpenKeys,
...others
} = props;
console.log('defaultSelectedUid', defaultSelectedUid)
const schema = useFieldSchema();
const sideMenuRef = useSideMenuRef();
const [defaultSelectedKeys, setDefaultSelectedKeys] = useState(() => {
@ -52,8 +51,9 @@ export const Menu: ComposedMenu = observer((props) => {
return dOpenKeys;
});
const [sideMenuSchema, setSideMenuSchema] = useState<Schema>(() => {
if (mode === 'mix' && defaultSelectedKeys[0]) {
const s = schema.properties?.[defaultSelectedKeys[0]];
const key = defaultSelectedKeys?.[0] || null;
if (mode === 'mix' && key) {
const s = schema.properties?.[key];
if (s['x-component'] === 'Menu.SubMenu') {
return s;
}

View File

@ -12,25 +12,24 @@ export interface RemoteSchemaComponentProps {
schemaTransform?: (schema: Schema) => Schema;
render?: any;
hidden?: any;
onlyRenderProperties?: boolean;
}
const defaultTransform = (s: Schema) => s;
const RequestSchemaComponent: React.FC<RemoteSchemaComponentProps> = (props) => {
const { hidden, scope, uid, onSuccess, schemaTransform = defaultTransform } = props;
const { onlyRenderProperties, hidden, scope, uid, onSuccess, schemaTransform = defaultTransform } = props;
const { reset } = useSchemaComponentContext();
const { data, loading } = useRequest(
{
url: `/ui_schemas:getJsonSchema/${uid}`,
const conf = {
url: `/ui_schemas:${onlyRenderProperties ? 'getProperties' : 'getJsonSchema'}/${uid}`,
};
const { data, loading } = useRequest(conf, {
refreshDeps: [uid],
onSuccess(data) {
onSuccess && onSuccess(data);
reset && reset();
},
{
refreshDeps: [uid],
onSuccess(data) {
onSuccess && onSuccess(data);
reset && reset();
},
},
);
});
if (loading) {
return <Spin />;

View File

@ -11,11 +11,7 @@ export const useSystemSettings = () => {
export const SystemSettingsProvider: React.FC = (props) => {
const result = useRequest({
resource: 'system_settings',
action: 'get',
params: {
filterByTk: 1,
},
url: 'systemSettings:get/1',
});
if (result.loading) {
return <Spin />;

View File

@ -16,7 +16,7 @@ const apiClient = new APIClient();
const mock = new MockAdapter(apiClient.axios);
mock.onGet('/system_settings:get').reply(200, {
mock.onGet('/systemSettings:get/1').reply(200, {
data: {
title: 'NocoBase',
},

View File

@ -45,6 +45,21 @@ export class UiRoutesStoragePlugin extends Plugin {
title: 'Menu Item u7',
'x-component': 'Menu.Item',
'x-component-props': {},
properties: {
page1: {
type: 'void',
'x-component': 'Page',
'x-async': true,
properties: {
grid1: {
type: 'void',
'x-component': 'Grid',
'x-item-initializer': 'Grid.AddBlockItem',
properties: {},
},
},
}
}
},
item8: {
type: 'void',

View File

@ -1,5 +1,6 @@
import { MagicAttributeModel } from '@nocobase/database';
import { Plugin } from '@nocobase/server';
import { uid } from '@nocobase/utils';
import path from 'path';
import { uiSchemaActions } from './actions/ui-schema-action';
import { UiSchemaModel } from './model';
@ -27,6 +28,9 @@ export class UiSchemaStoragePlugin extends Plugin {
db.on('ui_schemas.beforeCreate', function setUid(model) {
model.set('uid', model.get('x-uid'));
if (!model.get('name')) {
model.set('name', uid());
}
});
db.on('ui_schemas.afterCreate', async function insertSchema(model, options) {