Skip to content

Commit

Permalink
feat(core): 调整router和权限
Browse files Browse the repository at this point in the history
  • Loading branch information
stbui committed Jan 2, 2020
1 parent e464a2d commit 5f09c6f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/auth/WithPermissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ const WithPermissions: FunctionComponent<WithPermissionsProps> = ({
return children({ permissions, ...props });
};

export default WithPermissions as any;
export default WithPermissions;
2 changes: 1 addition & 1 deletion packages/core/src/auth/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const useAuthState = (params = {}) => {
.catch(() =>
setState({ loading: false, loaded: true, authenticated: false })
);
}, [checkAuth, params, setState]);
}, [checkAuth, JSON.stringify(params), setState]);

return state;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/auth/usePermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const usePermissions = (params = {}) => {
error,
});
});
}, [getPermissions, params, setState]);
}, [getPermissions, JSON.stringify(params), setState]);

return state;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/CoreAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* https://github.com/stbui/prophet
*/

import React, { FunctionComponent, ComponentType, Children } from 'react';
import React, { FunctionComponent, ComponentType } from 'react';
import CoreContext from './CoreContext';
import CoreUI from './CoreUI';

Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/core/CoreRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React, {
useState,
useEffect,
} from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import { Switch, Route } from 'react-router-dom';
import CoreRoutesWithLayout from './CoreRoutesWithLayout';
import { useGetPermissions, useAuthState } from '../auth';

Expand Down Expand Up @@ -96,11 +96,11 @@ const CoreRouter: FunctionComponent<CoreRouterProps> = ({
typeof children === 'function' ? computedChildren : children;

return (
<div>
<React.Fragment>
{Children.map(childrenToRender, (child: any) =>
cloneElement(child, {
key: child.props.name,
context: 'registration',
intent: 'registration',
})
)}
<Switch>
Expand All @@ -124,6 +124,7 @@ const CoreRouter: FunctionComponent<CoreRouterProps> = ({
dashboard,
menu,
title,
brand,
},
<CoreRoutesWithLayout
catchAll={catchAll}
Expand All @@ -144,7 +145,7 @@ const CoreRouter: FunctionComponent<CoreRouterProps> = ({
}
/>
</Switch>
</div>
</React.Fragment>
);
};

Expand Down
60 changes: 41 additions & 19 deletions packages/core/src/core/Resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
* https://github.com/stbui
*/

import React, { useEffect, useMemo } from 'react';
import React, { useEffect, useMemo, FunctionComponent } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Switch, Route } from 'react-router-dom';
import { WithPermissions } from '../auth';
import { registerResource, unregisterResource } from '../actions';

const ResourceRegister: any = ({
export interface ResourceProps {
name: string;
label?: any;
match?: any;
list?: any;
create?: any;
edit?: any;
show?: any;
options?: any;
icon?: any;
intent?: 'route' | 'registration';
}

const ResourceRegister: FunctionComponent<ResourceProps> = ({
label,
name,
list,
Expand Down Expand Up @@ -44,7 +57,7 @@ const ResourceRegister: any = ({
return null;
};

const ResourceRoutes: any = ({
const ResourceRoutes: FunctionComponent<ResourceProps> = ({
label,
name,
match,
Expand Down Expand Up @@ -80,55 +93,59 @@ const ResourceRoutes: any = ({
{create && (
<Route
path={`${basePath}/create`}
render={props => (
render={routeProps => (
<WithPermissions
component={create}
basePath={basePath}
{...props}
{...routeProps}
{...resource}
></WithPermissions>
/>
)}
/>
)}
{show && (
<Route
path={`${basePath}/:id/show`}
render={props => (
render={routeProps => (
<WithPermissions
component={show}
basePath={basePath}
id={decodeURIComponent(props.match.params.id)}
{...props}
id={decodeURIComponent(
routeProps.match.params.id
)}
{...routeProps}
{...resource}
></WithPermissions>
/>
)}
/>
)}
{edit && (
<Route
path={`${basePath}/:id`}
render={props => (
render={routeProps => (
<WithPermissions
component={edit}
basePath={basePath}
id={decodeURIComponent(props.match.params.id)}
{...props}
id={decodeURIComponent(
routeProps.match.params.id
)}
{...routeProps}
{...resource}
></WithPermissions>
/>
)}
/>
)}

{list && (
<Route
path={basePath}
render={props => (
render={routeProps => (
<WithPermissions
component={list}
basePath={basePath}
{...props}
{...routeProps}
{...resource}
></WithPermissions>
/>
)}
/>
)}
Expand All @@ -147,10 +164,15 @@ const ResourceRoutes: any = ({
]);
};

export default ({ context = 'route', ...props }) => {
return context === 'registration' ? (
const Resource: FunctionComponent<ResourceProps> = ({
intent = 'route',
...props
}) => {
return intent === 'registration' ? (
<ResourceRegister {...props} />
) : (
<ResourceRoutes {...props} />
);
};

export default Resource;

0 comments on commit 5f09c6f

Please sign in to comment.