MSP-3074 add roles
This commit is contained in:
1 parent
5a29bfc7f6
commit
9c1b859971
36 files changed
+1063
-100
No files matched your search
@@ -0,0 +1,14 @@
|
||||
import {PropsWithChildren} from 'react';
|
||||
import useRole from "../../hooks/useRole.tsx";
|
||||
import {PermissionKeys} from "../../types/roles.type.ts";
|
||||
|
||||
function AdminPermission({children}: PropsWithChildren) {
|
||||
const {hasPermission} = useRole();
|
||||
|
||||
if (hasPermission(PermissionKeys.admin_view)) {
|
||||
return <>{children}</>;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export default AdminPermission;
|
||||
+9
-20
@@ -1,35 +1,24 @@
|
||||
import { useState } from 'react';
|
||||
import { Route, Routes } from 'react-router-dom';
|
||||
import {App} from 'antd';
|
||||
import { ThemeAppearance, ThemeProvider } from 'antd-style';
|
||||
import Layout from "../pages/Layout/Layout.tsx";
|
||||
import HomePages from '../pages/HomePage/HomePages.tsx';
|
||||
import {useState} from 'react';
|
||||
import {ThemeAppearance, ThemeProvider} from 'antd-style';
|
||||
import {Context} from "../store";
|
||||
import Routers from "./Routers/Routers.tsx";
|
||||
import './app.scss'
|
||||
|
||||
|
||||
|
||||
|
||||
function AppDefault() {
|
||||
const [appearance, setTheme] = useState<ThemeAppearance>('light');
|
||||
function App() {
|
||||
const [theme, setTheme] = useState<ThemeAppearance>('light');
|
||||
|
||||
return (
|
||||
<div className="app">
|
||||
<Context.Provider value={{setTheme}}>
|
||||
<ThemeProvider appearance={appearance} >
|
||||
<App>
|
||||
<Routes>
|
||||
<Route path="/" element={<Layout />}>
|
||||
<Route index element={<HomePages/>}/>
|
||||
</Route>
|
||||
</Routes>
|
||||
</App>
|
||||
</ThemeProvider>
|
||||
<ThemeProvider appearance={theme}>
|
||||
<Routers/>
|
||||
</ThemeProvider>
|
||||
</Context.Provider>
|
||||
</div>
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
export default AppDefault
|
||||
export default App
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import {useContext} from "react";
|
||||
import {ThemeAppearance, useTheme} from "antd-style";
|
||||
import {Segmented} from "antd";
|
||||
import {Context} from "../../store";
|
||||
|
||||
|
||||
const options = [
|
||||
{ label: 'Light', value: 'light' },
|
||||
{ label: 'Dark', value: 'dark' },
|
||||
];
|
||||
function Header() {
|
||||
const theme = useTheme();
|
||||
const { setTheme } = useContext(Context)
|
||||
return <header style={{
|
||||
background: theme.colorBgLayout,
|
||||
width: '100%',
|
||||
border: 'none',
|
||||
display: 'flex',
|
||||
justifyContent: 'end'
|
||||
}}><Segmented onChange={(v) => setTheme(v as ThemeAppearance)} options={options} /></header>;
|
||||
}
|
||||
|
||||
export default Header;
|
||||
@@ -0,0 +1,6 @@
|
||||
.header {
|
||||
width: 100%;
|
||||
border: none;
|
||||
display: flex;
|
||||
justify-content: end
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import {useContext} from "react";
|
||||
import {ThemeAppearance, useTheme} from "antd-style";
|
||||
import {Segmented} from "antd";
|
||||
import {Context} from "../../store";
|
||||
import './header.scss'
|
||||
|
||||
|
||||
const options = [
|
||||
{label: 'Light', value: 'light'},
|
||||
{label: 'Dark', value: 'dark'},
|
||||
];
|
||||
|
||||
function Header() {
|
||||
const theme = useTheme();
|
||||
const {setTheme} = useContext(Context)
|
||||
|
||||
return <header className='header' style={{
|
||||
background: theme.colorBgLayout
|
||||
}}><Segmented onChange={(v) => setTheme(v as ThemeAppearance)} options={options}/></header>;
|
||||
}
|
||||
|
||||
export default Header;
|
||||
@@ -1,25 +0,0 @@
|
||||
import {Button} from 'antd';
|
||||
import {useTheme} from 'antd-style';
|
||||
|
||||
|
||||
|
||||
const Main= () => {
|
||||
const theme = useTheme();
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
background: theme.colorBgLayout,
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
justifyContent: 'center'
|
||||
}}>
|
||||
|
||||
<Button ghost type={'primary'}>
|
||||
Border
|
||||
</Button>
|
||||
<div>Проверка шрифтов</div>
|
||||
<Button type={'primary'}>Primary</Button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default Main;
|
||||
@@ -0,0 +1,27 @@
|
||||
import {Button} from 'antd';
|
||||
import {useTheme} from 'antd-style';
|
||||
import {PermissionKeys} from "../../types/roles.type.ts";
|
||||
import useRole from "../../hooks/useRole.tsx";
|
||||
import './main.scss'
|
||||
|
||||
|
||||
const Main = () => {
|
||||
const theme = useTheme();
|
||||
const {hasPermission} = useRole();
|
||||
|
||||
const isPageAvailable = hasPermission(PermissionKeys.user_view);
|
||||
|
||||
return (
|
||||
<div className='main' style={{
|
||||
background: theme.colorBgLayout
|
||||
}}>
|
||||
|
||||
{isPageAvailable && <Button ghost type={'primary'}>
|
||||
Border
|
||||
</Button>}
|
||||
<div>Проверка шрифтов</div>
|
||||
<Button type={'primary'}>Primary</Button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default Main;
|
||||
@@ -0,0 +1,5 @@
|
||||
.main {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
justify-content: center
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import {Navigate, Outlet, useLocation} from 'react-router-dom';
|
||||
import {LocalStorage} from "../../utils/localStorageUtils.ts";
|
||||
import {localStorageKeys} from "../../constants/localStorageKeys.ts";
|
||||
import {routePaths} from "../../constants/routes.ts";
|
||||
|
||||
function PrivateRoute() {
|
||||
const location = useLocation();
|
||||
|
||||
const userToken = LocalStorage.get(localStorageKeys.USER_TOKEN);
|
||||
|
||||
if (!userToken) {
|
||||
LocalStorage.set('prevUrl', location.pathname)
|
||||
return (
|
||||
<Navigate
|
||||
to={routePaths.error}
|
||||
// state={{ from: location.pathname }}
|
||||
replace
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
return <Outlet/>;
|
||||
}
|
||||
|
||||
export default PrivateRoute;
|
||||
@@ -0,0 +1,27 @@
|
||||
import {Route, Routes} from 'react-router-dom';
|
||||
import {App} from 'antd';
|
||||
import Layout from "../../pages/Layout";
|
||||
import HomePages from "../../pages/HomePage";
|
||||
import Test from "../Test/Test.tsx";
|
||||
import PrivateRoute from "../PrivateRoute";
|
||||
|
||||
|
||||
function Routers() {
|
||||
|
||||
return (
|
||||
<App>
|
||||
<Routes>
|
||||
<Route element={<PrivateRoute/>}>
|
||||
<Route path="/" element={<Layout/>}>
|
||||
<Route index element={<HomePages/>}/>
|
||||
<Route path="/sessions" element={<Test/>}/>
|
||||
<Route path="/error" element={<Test/>}/>
|
||||
</Route>
|
||||
</Route>
|
||||
</Routes>
|
||||
</App>
|
||||
)
|
||||
}
|
||||
|
||||
export default Routers
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import {useTheme} from 'antd-style';
|
||||
import {useEffect} from "react";
|
||||
import {routePaths} from "../../constants/routes.ts";
|
||||
import useRole from "../../hooks/useRole.tsx";
|
||||
import {PermissionKeys} from "../../types/roles.type.ts";
|
||||
import {useNavigate} from "react-router-dom";
|
||||
|
||||
|
||||
const Test = () => {
|
||||
const theme = useTheme();
|
||||
const {role, hasPermission} = useRole();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const isPageAvailable = hasPermission(PermissionKeys.admin_view);
|
||||
|
||||
useEffect(() => {
|
||||
if (!role) {
|
||||
navigate(routePaths.home);
|
||||
}
|
||||
}, [role]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!isPageAvailable) {
|
||||
navigate(routePaths.home);
|
||||
}
|
||||
}, [isPageAvailable]);
|
||||
return (
|
||||
<div style={{
|
||||
background: theme.colorBgLayout,
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
justifyContent: 'center'
|
||||
}}>
|
||||
|
||||
<div>fgdegjhjflefkegjvhecdhejqhdkefke</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
export default Test;
|
||||
Reference in new issue
Block a user