Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
products_lesson_front
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavel Mishakov
products_lesson_front
Commits
8e77dc13
Commit
8e77dc13
authored
Apr 07, 2023
by
Pavel Mishakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
94 done FRONT
parent
f6961248
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
84 additions
and
11 deletions
+84
-11
App.tsx
src/App.tsx
+8
-2
Header.tsx
src/components/Header/Header.tsx
+3
-0
ProductItem.tsx
src/components/ProductItem/ProductItem.tsx
+13
-3
Button.tsx
src/components/UI/Button/Button.tsx
+15
-0
IButtonProps.ts
src/components/UI/Button/IButtonProps.ts
+8
-0
ERoles.ts
src/enums/ERoles.ts
+5
-0
IUser.ts
src/interfaces/IUser.ts
+3
-1
PermissionRoute.tsx
src/utils/PermissionRoute.tsx
+23
-0
PrivateRoute.tsx
src/utils/PrivateRoute.tsx
+6
-5
No files found.
src/App.tsx
View file @
8e77dc13
...
...
@@ -8,6 +8,8 @@ import PrivateRoute from "./utils/PrivateRoute";
import
UserForm
from
"./components/UserForm/UserForm"
;
import
ForgotPasswordForm
from
"./components/ForgotPasswordForm/ForgotPasswordForm"
;
import
ResetPasswordForm
from
"./components/ResetPasswordForm/ResetPasswordForm"
;
import
PermissionRoute
from
"./utils/PermissionRoute"
;
import
{
ERoles
}
from
"./enums/ERoles"
;
const
App
=
()
=>
{
return
(
...
...
@@ -16,8 +18,12 @@ const App = () => {
<
Route
element=
{
<
Layout
/>
}
>
<
Route
path=
{
"/"
}
element=
{
<
h1
>
HOME
</
h1
>
}
/>
<
Route
path=
{
"/products"
}
element=
{
<
ProductsPage
/>
}
/>
<
Route
path=
{
"/add-product"
}
element=
{
<
ProductFormPage
/>
}
/>
<
Route
path=
{
"/edit-user"
}
element=
{
<
UserForm
/>
}
/>
<
Route
element=
{
<
PermissionRoute
roles=
{
[
ERoles
.
SUPER_ADMIN
]
}
/>
}
>
<
Route
path=
{
"/add-product"
}
element=
{
<
ProductFormPage
/>
}
/>
</
Route
>
<
Route
element=
{
<
PermissionRoute
roles=
{
[
ERoles
.
ADMIN
]
}
/>
}
>
<
Route
path=
{
"/edit-user"
}
element=
{
<
UserForm
/>
}
/>
</
Route
>
</
Route
>
</
Route
>
<
Route
path=
{
"/login"
}
element=
{
<
Login
/>
}
/>
...
...
src/components/Header/Header.tsx
View file @
8e77dc13
...
...
@@ -19,12 +19,15 @@ const Header: FunctionComponent = (): ReactElement => {
<
NavLink
className=
{
styles
.
Header__link
}
to=
{
"/add-product"
}
>
Add product
</
NavLink
>
<
NavLink
className=
{
styles
.
Header__link
}
to=
{
"/products"
}
>
Products
</
NavLink
>
<
NavLink
className=
{
styles
.
Header__link
}
to=
{
"/edit-user"
}
>
Edit user
</
NavLink
>
<
div
>
{
user
?.
username
}
</
div
>
...
...
src/components/ProductItem/ProductItem.tsx
View file @
8e77dc13
...
...
@@ -5,6 +5,8 @@ import defaultImage from '../../assets/images/default.jpg'
import
{
AppDispatch
}
from
"../../store/store"
;
import
{
useDispatch
}
from
"react-redux"
;
import
{
deleteProductById
}
from
"../../store/products/products.slice"
;
import
Button
from
"../UI/Button/Button"
;
import
{
ERoles
}
from
"../../enums/ERoles"
;
const
ProductItem
:
FunctionComponent
<
IProductItemProps
>
=
(
props
):
ReactElement
=>
{
const
dispatch
:
AppDispatch
=
useDispatch
()
...
...
@@ -58,9 +60,17 @@ const ProductItem: FunctionComponent<IProductItemProps> = (props): ReactElement
<
p
>
{
props
.
product
.
price
}
</
p
>
<
div
className=
{
styles
.
ProductItem__btnBlock
}
>
<
button
>
Details
</
button
>
<
button
>
Edit
</
button
>
<
button
onClick=
{
deleteHandler
}
>
Delete
</
button
>
<
Button
title=
{
'Details'
}
/>
<
Button
title=
{
'Edit'
}
/>
<
Button
title=
{
'Delete'
}
click=
{
deleteHandler
}
roles=
{
[
ERoles
.
SUPER_ADMIN
]
}
/>
</
div
>
</
div
>
)
...
...
src/components/UI/Button/Button.tsx
0 → 100644
View file @
8e77dc13
import
React
,
{
useContext
}
from
'react'
import
IButtonProps
from
'./IButtonProps'
import
{
UserContext
}
from
'../../../utils/PrivateRoute'
import
{
ERoles
}
from
'../../../enums/ERoles'
const
Button
:
React
.
FunctionComponent
<
IButtonProps
>
=
(
props
):
React
.
ReactElement
=>
{
const
role
=
useContext
(
UserContext
)
return
(
!
props
.
roles
||
role
&&
props
.
roles
.
includes
(
role
)
?
<
button
onClick=
{
props
.
click
}
>
{
props
.
title
}
</
button
>
:
<></>
)
}
export
default
Button
\ No newline at end of file
src/components/UI/Button/IButtonProps.ts
0 → 100644
View file @
8e77dc13
import
{
MouseEventHandler
}
from
"react"
import
{
ERoles
}
from
"../../../enums/ERoles"
export
default
interface
IButtonProps
{
title
:
string
click
?:
MouseEventHandler
<
HTMLButtonElement
>
roles
?:
ERoles
[]
}
\ No newline at end of file
src/enums/ERoles.ts
0 → 100644
View file @
8e77dc13
export
enum
ERoles
{
ADMIN
=
'ADMIN'
,
USER
=
'USER'
,
SUPER_ADMIN
=
'SUPER_ADMIN'
}
\ No newline at end of file
src/interfaces/IUser.ts
View file @
8e77dc13
import
{
ERoles
}
from
"../enums/ERoles"
export
default
interface
IUser
{
_id
:
string
username
:
string
password
:
string
active
:
boolean
token
:
string
role
:
string
role
:
ERoles
}
\ No newline at end of file
src/utils/PermissionRoute.tsx
0 → 100644
View file @
8e77dc13
import
React
,
{
useEffect
}
from
'react'
import
{
shallowEqual
,
useDispatch
,
useSelector
}
from
'react-redux'
import
{
Navigate
,
Outlet
}
from
'react-router-dom'
import
{
AppDispatch
,
AppState
}
from
'../store/store'
import
{
checkToken
}
from
'../store/users/users.slice'
const
PermissionRoute
:
React
.
FunctionComponent
<
{
roles
:
string
[]}
>
=
({
roles
}):
React
.
ReactElement
=>
{
const
{
isAuth
,
user
}
=
useSelector
((
state
:
AppState
)
=>
state
.
users
,
shallowEqual
)
const
dispatch
:
AppDispatch
=
useDispatch
()
useEffect
(()
=>
{
dispatch
(
checkToken
())
},
[])
return
(
isAuth
&&
user
?.
role
&&
roles
.
includes
(
user
?.
role
)
?
<
Outlet
/>
:
<
Navigate
to=
'/'
replace
/>
)
}
export
default
PermissionRoute
\ No newline at end of file
src/utils/PrivateRoute.tsx
View file @
8e77dc13
import
React
,
{
useEffect
}
from
'react'
import
React
,
{
createContext
,
useEffect
}
from
'react'
import
{
shallowEqual
,
useDispatch
,
useSelector
}
from
'react-redux'
import
{
Navigate
,
Outlet
,
useLocation
}
from
'react-router-dom'
import
{
AppDispatch
,
AppState
}
from
'../store/store'
import
{
checkToken
}
from
'../store/users/users.slice'
import
{
ERoles
}
from
'../enums/ERoles'
export
const
UserContext
=
createContext
<
ERoles
|
undefined
>
(
undefined
)
const
PrivateRoute
:
React
.
FunctionComponent
=
():
React
.
ReactElement
=>
{
const
{
isAuth
}
=
useSelector
((
state
:
AppState
)
=>
state
.
users
,
shallowEqual
)
const
{
isAuth
,
user
}
=
useSelector
((
state
:
AppState
)
=>
state
.
users
,
shallowEqual
)
const
location
=
useLocation
()
const
dispatch
:
AppDispatch
=
useDispatch
()
useEffect
(()
=>
{
dispatch
(
checkToken
())
},
[])
return
(
isAuth
isAuth
?
<
Outlet
/
>
<
UserContext
.
Provider
value=
{
user
?.
role
}
><
Outlet
/></
UserContext
.
Provider
>
:
<
Navigate
to=
'/login'
replace
state=
{
{
from
:
location
}
}
/>
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment