Добавлены route на страницы

This commit is contained in:
Никита Потапов 2024-01-11 16:11:44 +04:00
parent bd4bcf5e9e
commit 30fb4d3e6a
8 changed files with 91 additions and 1 deletions

View File

@ -11,7 +11,7 @@ const Header = ({ routes }) => {
const indexPageLink = routes.filter((route) => route.index === false).shift(); const indexPageLink = routes.filter((route) => route.index === false).shift();
const currentPage = routes.filter((route) => route.path === location.pathname)[0]; const currentPage = routes.filter((route) => route.path === location.pathname)[0];
if (currentUser) { if (currentUser) {
routes = routes.filter((route) => !route.onlyNoAuthorized); routes = routes.filter((route) => !route.onlyNoAuthorized && route.onlyTeachers == null || route.onlyTeachers == currentUser.isTeacher);
} }
else { else {
routes = routes.filter((route) => !route.onlyAuthorized); routes = routes.filter((route) => !route.onlyAuthorized);

View File

View File

@ -1,5 +1,28 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import UsersApiService from "../services/UsersApiService"; import UsersApiService from "../services/UsersApiService";
import { useNavigate } from "react-router-dom";
import { useContext } from "react";
import { CurrentUserContext } from "../contexts/CurrentUserContext";
export const useOnlyAuthorized = () => {
const navigate = useNavigate();
const { currentUser } = useContext(CurrentUserContext);
useEffect(() => {
if (!currentUser) {
navigate("/auth");
}
});
};
export const useOnlyTeachers = () => {
const navigate = useNavigate();
const { currentUser } = useContext(CurrentUserContext);
useEffect(() => {
if (!currentUser || !currentUser.isTeacher) {
navigate("/auth");
}
});
};
export const useUserByUsername = (username) => { export const useUserByUsername = (username) => {
const [usersRefresh, setUsersRefresh] = useState(false); const [usersRefresh, setUsersRefresh] = useState(false);

View File

@ -10,6 +10,9 @@ import MainPage from './pages/MainPage/MainPage.jsx'
import AuthPage from './pages/AuthPage/AuthPage.jsx' import AuthPage from './pages/AuthPage/AuthPage.jsx'
import RegisterPage from './pages/RegisterPage/RegisterPage.jsx' import RegisterPage from './pages/RegisterPage/RegisterPage.jsx'
import StatementsPage from './pages/StatementsPage/StatementsPage.jsx' import StatementsPage from './pages/StatementsPage/StatementsPage.jsx'
import SubjectsPage from './pages/SubjectsPage/SubjectsPage.jsx'
import GroupsPage from './pages/GroupsPage/GroupsPage.jsx'
import StudentsPage from './pages/StudentsPage/StudentsPage.jsx'
const routes = [ const routes = [
{ {
@ -25,6 +28,27 @@ const routes = [
onlyAuthorized: true, onlyAuthorized: true,
onlyTeachers: true onlyTeachers: true
}, },
{
path: '/groups',
element: <GroupsPage />,
title: 'Группы',
onlyAuthorized: true,
onlyTeachers: true
},
{
path: '/students',
element: <StudentsPage />,
title: 'Студенты',
onlyAuthorized: true,
onlyTeachers: true
},
{
path: '/subjects',
element: <SubjectsPage />,
title: 'Предметы',
onlyAuthorized: true,
onlyTeachers: true
},
{ {
path: '/auth', path: '/auth',
element: <AuthPage />, element: <AuthPage />,

View File

@ -0,0 +1,13 @@
import { useOnlyTeachers } from "../../hooks/UserHooks";
const GroupsPage = () => {
useOnlyTeachers();
return (
<>
<h5>GroupsPage</h5>
</>
);
};
export default GroupsPage

View File

@ -1,4 +1,8 @@
import { useOnlyTeachers } from "../../hooks/UserHooks";
const StatementsPage = () => { const StatementsPage = () => {
useOnlyTeachers();
return ( return (
<> <>
<h5>StatementsPage</h5> <h5>StatementsPage</h5>

View File

@ -0,0 +1,13 @@
import { useOnlyTeachers } from "../../hooks/UserHooks";
const StudentsPage = () => {
useOnlyTeachers();
return (
<>
<h5>StudentsPage</h5>
</>
);
};
export default StudentsPage;

View File

@ -0,0 +1,13 @@
import { useOnlyTeachers } from "../../hooks/UserHooks";
const SubjectsPage = () => {
useOnlyTeachers();
return (
<>
<h5>SubjectsPage</h5>
</>
);
};
export default SubjectsPage