chenos dcdb21d398
发布核心框架 (#6)
* api/ui 改名为 server/client

* 微调

* 继续完善 pages

* Fix env file and file mode. (#1)

* Fix: ignore .env file and environment variable names.

* Fix: correct file mode.

* fix: put environment variables together

* fix: separate data and ui resourcer

* feat: collection loader

* feat: redirectTo

* feat: fields & actions & views

* feat: fields & actions

* feat: app & pages & collections...

* feat: collections & pages & permissions...

* Doc: add readme (#2)

* Doc: add README.md.

* Util: add .editorconfig.

* Fix: use glob ignore option instead of additional checking. (#3)

* Fix: typo. (#4)

* feat: permissions

* feat: getCollection & getView actions

* refactor: code cleanup

Co-authored-by: Junyi <mytharcher@users.noreply.github.com>
2020-11-11 15:23:39 +08:00

53 lines
1.4 KiB
TypeScript

import React from 'react';
import get from 'lodash/get';
import { Spin, Redirect } from '@nocobase/client';
function getRoutes(path: string, pages: any) {
const keys = path.split('/');
const routes: Array<any> = [];
while(keys.length) {
const uri = keys.join('/');
if (pages[uri]) {
routes.push({...pages[uri], path: uri});
}
keys.pop();
}
if (path && pages['/']) {
routes.push({...pages['/'], path: '/'});
}
return routes;
}
export function TemplateLoader(props: any) {
const { loading, pathname, pages } = props;
if (loading) {
return <Spin size={'large'} className={'spinning--absolute'}></Spin>;
}
const redirect = get(pages, [pathname, 'redirect']);
if (redirect) {
return <Redirect to={redirect}/>
}
const routes = getRoutes(pathname, pages);
let component: any;
console.log(...routes);
const len = routes.length;
const componentProps = {...props};
while(routes.length) {
const route = routes.shift();
console.log(route.template);
const Component = require(route.template).default;
if (route.type === 'collection') {
componentProps.match.params['collection'] = route.collection;
}
if (len === routes.length+1) {
componentProps['lastPage'] = route;
}
componentProps['page'] = route;
component = <Component key={`page-${route.id}`} {...componentProps}>{component}</Component>;
if (route.inherit === false) {
break;
}
}
return component;
}