diff --git a/src/components/Private/PrivateContent.jsx b/src/components/Private/PrivateContent.jsx deleted file mode 100644 index 878644f..0000000 --- a/src/components/Private/PrivateContent.jsx +++ /dev/null @@ -1,24 +0,0 @@ -const admins = ['администратор', 'админ', 'admin'] - -export const isInRole = (roles) => { - if(!roles?.length) - return true - const role = localStorage['roleName']?.toLowerCase() - if(admins.indexOf(role) > -1) - return true - for(const r of roles) - if(r.toLowerCase() === role) - return true - return false -} - -export const privateContent = (roles, children) => { - if(isInRole(roles)) - return children - else - return null -} - -export const PrivateContent = ({roles, children}) => { - return privateContent(roles, children) -} \ No newline at end of file diff --git a/src/components/Private/PrivateContent.tsx b/src/components/Private/PrivateContent.tsx new file mode 100644 index 0000000..dd195d4 --- /dev/null +++ b/src/components/Private/PrivateContent.tsx @@ -0,0 +1,28 @@ +export type Role = string + +const admins: Role[] = ['администратор', 'админ', 'admin'] + +export const isInRole = (roles?: Role[]): boolean => { + // if (localStorage['login'] === 'dev') + // return true + if(!roles?.length) + return true + const role: Role = localStorage['roleName']?.toLowerCase() + if(admins.indexOf(role) > -1) + return true + for(const r of roles) + if(r.toLowerCase() === role) + return true + return false +} + +export const privateContent = (roles?: Role[], children?: T): T | null | undefined => { + return isInRole(roles) ? children : null +} + +type PrivateContentProps = { + roles?: Role[] + children?: any +} + +export const PrivateContent: React.FC = ({ roles, children }) => privateContent(roles, children) diff --git a/src/components/Private/PrivateMenuItem.jsx b/src/components/Private/PrivateMenuItem.jsx deleted file mode 100644 index 070866f..0000000 --- a/src/components/Private/PrivateMenuItem.jsx +++ /dev/null @@ -1,11 +0,0 @@ -import { Menu } from "antd"; -import { isInRole } from './PrivateContent' - -export const PrivateMenuItem = ({ children, roles, ...other }) => { - if(!isInRole(roles)) - return null - - return - {children} - ; -} diff --git a/src/components/Private/PrivateMenuItem.tsx b/src/components/Private/PrivateMenuItem.tsx new file mode 100644 index 0000000..6de159f --- /dev/null +++ b/src/components/Private/PrivateMenuItem.tsx @@ -0,0 +1,12 @@ +import React from 'react' +import { Menu } from 'antd' +import { isInRole, Role } from './PrivateContent' + +type PrivateMenuItemProps = { + roles: Role[] + [props: string]: any +} + +export const PrivateMenuItem: React.FC = ({ roles, ...props }) => { + return isInRole(roles) ? : null +} diff --git a/src/components/Private/PrivateRoute.jsx b/src/components/Private/PrivateRoute.jsx deleted file mode 100644 index 94a9725..0000000 --- a/src/components/Private/PrivateRoute.jsx +++ /dev/null @@ -1,16 +0,0 @@ -import React /*, { useContext, createContext, useState }*/ from "react"; -import { - Route, - Redirect -} from "react-router-dom"; - -export function PrivateRoute({ children, ...rest }) { - let token = localStorage['token']; - return ( - token - ? (children) - : ()} /> - ); -} diff --git a/src/components/Private/PrivateRoute.tsx b/src/components/Private/PrivateRoute.tsx new file mode 100644 index 0000000..49be595 --- /dev/null +++ b/src/components/Private/PrivateRoute.tsx @@ -0,0 +1,21 @@ +import React from 'react' +import { Route, Redirect } from 'react-router-dom' +import { isInRole, Role } from './PrivateContent' + +type PrivateRouteProps = { + children: any + roles: Role[] + [other: string]: any +} + +export const PrivateRoute: React.FC = ({ children, roles, ...other }) => { + const token = localStorage['token'] + return ( + token && isInRole(roles) ? children : ( + ) + } + /> + ) +} diff --git a/src/components/Private/index.ts b/src/components/Private/index.ts index c813da3..ab1f42a 100644 --- a/src/components/Private/index.ts +++ b/src/components/Private/index.ts @@ -1,3 +1,3 @@ -export {PrivateRoute} from './PrivateRoute' -export {privateContent, PrivateContent} from './PrivateContent' -export {PrivateMenuItem} from './PrivateMenuItem' \ No newline at end of file +export { PrivateRoute } from './PrivateRoute' +export { privateContent, PrivateContent } from './PrivateContent' +export { PrivateMenuItem } from './PrivateMenuItem'