Commit 7520d64e authored by Ibadullina Inabat's avatar Ibadullina Inabat

реализовано слияние кода с основным

parent f6ba2795
import {Routes, Route, Outlet, Navigate, BrowserRouter} from "react-router-dom"; import {Routes, Route, Outlet, Navigate, BrowserRouter} from "react-router-dom";
import {Container} from "@mui/material"; import {Container} from "@mui/material";
import {useSelector} from "react-redux"; import {useSelector} from "react-redux";
import AppToolbar from "./components/UI/AppToolBar/AppToolBar"; import AppToolbar from "./components/UI/AppToolbar/AppToolBar";
import Login from "./containers/Login/Login";
import Register from "./containers/Register/Register";
const ProtectedRoute = ({isAllowed, roles, redirectUrl, children}) => { const ProtectedRoute = ({isAllowed, roles, redirectUrl, children}) => {
const user = useSelector(state => state.users?.user); const user = useSelector(state => state.users?.user);
console.log(user)
if (!isAllowed && !roles?.includes(user?.role)) { if (!isAllowed && !roles?.includes(user?.role)) {
return <Navigate to={redirectUrl} /> return <Navigate to={redirectUrl} />
} }
...@@ -13,6 +16,7 @@ const ProtectedRoute = ({isAllowed, roles, redirectUrl, children}) => { ...@@ -13,6 +16,7 @@ const ProtectedRoute = ({isAllowed, roles, redirectUrl, children}) => {
const App = () => { const App = () => {
const user = useSelector(state => state.users?.user); const user = useSelector(state => state.users?.user);
console.log(user)
return ( return (
<BrowserRouter> <BrowserRouter>
<Routes> <Routes>
...@@ -30,7 +34,7 @@ const App = () => { ...@@ -30,7 +34,7 @@ const App = () => {
<Route path={"/"} element={ <Route path={"/"} element={
<ProtectedRoute <ProtectedRoute
isAllowed={user} isAllowed={user}
redirectUrl={"/log-in"} redirectUrl={"/sign-in"}
> >
<h1>week page</h1> <h1>week page</h1>
</ProtectedRoute> </ProtectedRoute>
...@@ -39,7 +43,7 @@ const App = () => { ...@@ -39,7 +43,7 @@ const App = () => {
<Route path={"/week"} element={ <Route path={"/week"} element={
<ProtectedRoute <ProtectedRoute
isAllowed={user} isAllowed={user}
redirectUrl={"/log-in"} redirectUrl={"/sign-in"}
> >
<h1>week page</h1> <h1>week page</h1>
</ProtectedRoute> </ProtectedRoute>
...@@ -48,7 +52,7 @@ const App = () => { ...@@ -48,7 +52,7 @@ const App = () => {
<Route path={"/month"} element={ <Route path={"/month"} element={
<ProtectedRoute <ProtectedRoute
isAllowed={user} isAllowed={user}
redirectUrl={"/log-in"} redirectUrl={"/sign-in"}
> >
<h1>month page</h1> <h1>month page</h1>
</ProtectedRoute> </ProtectedRoute>
...@@ -57,7 +61,7 @@ const App = () => { ...@@ -57,7 +61,7 @@ const App = () => {
<Route path={"/my-tasks"} element={ <Route path={"/my-tasks"} element={
<ProtectedRoute <ProtectedRoute
isAllowed={user} isAllowed={user}
redirectUrl={"/log-in"} redirectUrl={"/sign-in"}
> >
<h1>my tasks page</h1> <h1>my tasks page</h1>
</ProtectedRoute> </ProtectedRoute>
...@@ -66,7 +70,7 @@ const App = () => { ...@@ -66,7 +70,7 @@ const App = () => {
<Route path={"/profile/:id"} element={ <Route path={"/profile/:id"} element={
<ProtectedRoute <ProtectedRoute
isAllowed={user} isAllowed={user}
redirectUrl={"/log-in"} redirectUrl={"/sign-in"}
> >
<h1>profile page</h1> <h1>profile page</h1>
</ProtectedRoute> </ProtectedRoute>
...@@ -86,11 +90,11 @@ const App = () => { ...@@ -86,11 +90,11 @@ const App = () => {
roles={["superuser"]} roles={["superuser"]}
redirectUrl={"/"} redirectUrl={"/"}
> >
<h1>sign-up page</h1> <Register/>
</ProtectedRoute> </ProtectedRoute>
}/> }/>
<Route path={"/log-in"} element={<h1>log-in page</h1>}/> <Route path={"/sign-in"} element={<Login/>}/>
<Route path='*' element={<h1>404</h1>}/> <Route path='*' element={<h1>404</h1>}/>
</Route> </Route>
</Routes> </Routes>
......
...@@ -6,7 +6,7 @@ const AnonymousMenu = () => { ...@@ -6,7 +6,7 @@ const AnonymousMenu = () => {
<Button <Button
component={NavLink} component={NavLink}
color="inherit" color="inherit"
to="/log-in" to="/sign-in"
> >
Вход Вход
</Button> </Button>
......
import {AppBar, Box, Toolbar, IconButton, Typography} from "@mui/material"; import {AppBar, Box, Toolbar, Typography} from "@mui/material";
import {NavLink} from "react-router-dom"; import {NavLink} from "react-router-dom";
import MenuIcon from '@mui/icons-material/Menu'; import {useSelector} from "react-redux";
import HasAccess from "../HasAccess/HasAccess"; import HasAccess from "../HasAccess/HasAccess";
import { useSelector } from "react-redux";
import AnonymousMenu from "../../Menus/AnonymousMenu/AnonymousMenu"; import AnonymousMenu from "../../Menus/AnonymousMenu/AnonymousMenu";
import UserMenu from "../../Menus/UserMenu/UserMenu"; import WorkerMenu from "../../Menus/WorkerMenu/WorkerMenu";
import AdminMenu from "../../Menus/AdminMenu/AdminMenu";
const AppToolbar = () => { const AppToolbar = () => {
const user = useSelector(state => state.users.user); const user = useSelector(state => state.users.user);
return <Box sx={{ flexGrow: 1, mb: "40px" }}> return <Box sx={{ flexGrow: 1, mb: "40px" }}>
<AppBar position="static"> <AppBar position="static">
<Toolbar> <Toolbar>
<IconButton <Typography
size="large" variant="h4"
edge="start" component="div"
color="inherit" sx={{ flexGrow: 1 }}>
aria-label="menu"
sx={{ mr: 2 }} <NavLink to='/' style={{textDecoration: 'none', color: 'inherit'}}>Task Manager</NavLink>
component={NavLink} </Typography>
to="/"
>
<MenuIcon />
</IconButton>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
Planner
</Typography>
<HasAccess allowed={!!user}>
<UserMenu user={user}/>
</HasAccess>
<HasAccess allowed={!user}> <HasAccess allowed={!user}>
<AnonymousMenu/> <AnonymousMenu/>
</HasAccess>
<HasAccess roles={['user']}>
<WorkerMenu/>
</HasAccess>
<HasAccess roles={['superuser']}>
<AdminMenu/>
</HasAccess> </HasAccess>
</Toolbar> </Toolbar>
</AppBar> </AppBar>
......
import ReactDOM from 'react-dom'; import React from 'react';
import { configureStore } from "@reduxjs/toolkit"; import ReactDOM from 'react-dom/client';
import {Provider} from 'react-redux'; import './index.css';
import {BrowserRouter} from 'react-router-dom';
import "./index.css";
import App from './App'; import App from './App';
import usersReduser from './store/reducers/usersReducer'; import { configureStore } from '@reduxjs/toolkit';
import { Provider } from 'react-redux';
import usersReducer from './store/reducers/usersReducer';
const localstorageMiddleware = ({getState}) => (next) => (action) => { const localStorageMiddleware = ({getState}) => (next) => (action) => {
const result = next(action); const result = next(action);
localStorage.setItem("user", JSON.stringify(getState().users.user)); localStorage.setItem("user", JSON.stringify(getState().users.user));
return result; return result;
} };
const loadFromLocalStorage = () => { const loadFromLocalStorage = () => {
if (localStorage.getItem("user") !== null) { if (localStorage.getItem("user") !== null) {
return {users: {user: JSON.parse(localStorage.getItem("user"))}} return {users: {user: JSON.parse(localStorage.getItem("user"))}}
} }
return undefined; return undefined;
} };
const store = configureStore({ const store = configureStore({
reducer: { reducer: {
users: usersReduser users: usersReducer,
}, },
preloadedState: loadFromLocalStorage(), preloadedState: loadFromLocalStorage(),
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localstorageMiddleware) middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware)
}); })
const app = ( const root = ReactDOM.createRoot(document.getElementById('root'));
<BrowserRouter> root.render(
<Provider store={store}> <Provider store={store}>
<App/> <App />
</Provider> </Provider>
</BrowserRouter>
); );
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(app);
\ No newline at end of file
import axios from "../../axiosPlanner"; import axios from "../../axiosPlanner";
import { LOGIN_USER_FAILURE, LOGIN_USER_SUCCESS, LOGOUT_USER_FAILURE, LOGOUT_USER_SUCCESS, REGISTER_USER_FAILURE, REGISTER_USER_REQUEST, REGISTER_USER_SUCCESS } from "../actionTypes" import { LOGIN_USER_FAILURE, LOGIN_USER_SUCCESS, LOGOUT_USER_FAILURE, LOGOUT_USER_SUCCESS, REGISTER_USER_FAILURE, REGISTER_USER_REQUEST, REGISTER_USER_SUCCESS } from "../actionTypes/actionTypes"
import { showNotification } from "./commonActions"; import { showNotification } from "./commonActions";
const registerUserRequest = () => { const registerUserRequest = () => {
......
import { REGISTER_USER_REQUEST, REGISTER_USER_SUCCESS, REGISTER_USER_FAILURE, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER_SUCCESS } from "../actionTypes"; import { REGISTER_USER_REQUEST, REGISTER_USER_SUCCESS, REGISTER_USER_FAILURE, LOGIN_USER_SUCCESS, LOGIN_USER_FAILURE, LOGOUT_USER_SUCCESS } from "../actionTypes/actionTypes";
const initialState = { const initialState = {
user: null, user: {
name: 'Ivan',
surname: 'Petrov',
email: 'test@gmail.com',
role: 'superuser'
},
registerError: null, registerError: null,
loginError: null, loginError: null,
loading: false loading: false
...@@ -17,6 +22,7 @@ const usersReduser = (state = initialState, action) => { ...@@ -17,6 +22,7 @@ const usersReduser = (state = initialState, action) => {
return {...state, loading: false, registerError: action.error}; return {...state, loading: false, registerError: action.error};
case LOGIN_USER_SUCCESS: case LOGIN_USER_SUCCESS:
return {...state, user: action.user}; return {...state, user: action.user};
case LOGIN_USER_FAILURE: case LOGIN_USER_FAILURE:
return {...state, loginError: action.error}; return {...state, loginError: action.error};
case LOGOUT_USER_SUCCESS: case LOGOUT_USER_SUCCESS:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment