Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ajs12_shop
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
Egor Kremnev
ajs12_shop
Commits
ba5af6c4
Commit
ba5af6c4
authored
May 24, 2023
by
Egor Kremnev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add edit user
parent
2584765b
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
153 additions
and
10 deletions
+153
-10
config.js
api_client/config.js
+1
-1
users.js
api_client/routes/users.js
+27
-4
Routes.jsx
front_client/src/Routes.jsx
+10
-1
UserMenu.jsx
.../src/components/UI/AppToolbar/Menus/UserMenu/UserMenu.jsx
+3
-3
UserForm.jsx
front_client/src/components/User/UserForm/UserForm.jsx
+66
-0
config.js
front_client/src/constants/config.js
+1
-1
routes.js
front_client/src/constants/routes.js
+1
-0
User.jsx
front_client/src/containers/User/User.jsx
+29
-0
usersActions.js
front_client/src/store/actions/usersActions.js
+15
-0
No files found.
api_client/config.js
View file @
ba5af6c4
...
...
@@ -3,7 +3,7 @@ const rootPath = __dirname;
module
.
exports
=
{
rootPath
,
port
:
80
01
,
port
:
80
10
,
uploadPath
:
path
.
join
(
rootPath
,
'public'
,
'uploads'
),
db
:
{
host
:
'mongodb://127.0.0.1'
,
...
...
api_client/routes/users.js
View file @
ba5af6c4
...
...
@@ -18,6 +18,32 @@ router.post('/', async (req, res) => {
}
});
router
.
put
(
'/'
,
auth
,
async
(
req
,
res
)
=>
{
const
user
=
req
.
user
;
if
(
req
.
body
.
oldPassword
.
trim
()
&&
!
await
user
.
checkPassword
(
req
.
body
.
oldPassword
.
toString
())
)
{
return
res
.
status
(
400
)
.
send
({
error
:
'Old password incorrect'
});
}
try
{
user
.
password
=
req
.
body
.
password
||
user
.
password
;
user
.
username
=
req
.
body
.
username
||
user
.
username
;
await
user
.
save
();
res
.
send
(
user
);
}
catch
(
e
)
{
res
.
status
(
400
)
.
send
({
error
:
e
.
message
});
}
});
router
.
post
(
'/login'
,
async
(
req
,
res
)
=>
{
const
user
=
await
User
.
findOne
({
username
:
req
.
body
.
username
});
...
...
@@ -42,10 +68,7 @@ router.post('/login', async (req, res) => {
});
router
.
get
(
'/profile'
,
auth
,
async
(
req
,
res
)
=>
{
res
.
send
({
message
:
"Большой большой сикрет"
,
username
:
req
.
user
.
username
});
res
.
send
(
req
.
user
);
});
router
.
delete
(
'/logout'
,
auth
,
async
(
req
,
res
)
=>
{
...
...
front_client/src/Routes.jsx
View file @
ba5af6c4
import
{
Navigate
,
Outlet
,
Route
,
Routes
as
RoutesSwitch
}
from
"react-router-dom"
;
import
{
LOGIN
,
MAIN
,
PRODUCT_ADD
,
PRODUCT_LIST
,
PRODUCT_VIEW
,
REGISTER
}
from
"./constants/routes"
;
import
{
LOGIN
,
MAIN
,
PRODUCT_ADD
,
PRODUCT_LIST
,
PRODUCT_VIEW
,
REGISTER
,
USER_PROFILE
}
from
"./constants/routes"
;
import
AddProduct
from
"./containers/AddProduct/AddProduct"
;
import
Layout
from
"./components/Layout/Layout"
;
import
Products
from
"./containers/Products/Products"
;
import
Register
from
"./containers/Auth/Register/Register"
;
import
Login
from
"./containers/Auth/Login/Login"
;
import
User
from
"./containers/User/User"
;
const
ProtectedRoute
=
({
isAllowed
,
redirectPath
,
children
})
=>
{
if
(
!
isAllowed
)
{
...
...
@@ -22,6 +23,13 @@ const Routes = ({user}) => {
<
AddProduct
/>
</
ProtectedRoute
>;
const
userProfile
=
<
ProtectedRoute
isAllowed=
{
!!
user
}
redirectPath=
{
LOGIN
}
>
<
User
/>
</
ProtectedRoute
>;
return
<
RoutesSwitch
>
<
Route
element=
{
<
Layout
/>
}
>
<
Route
index
element=
{
<
Products
/>
}
/>
...
...
@@ -29,6 +37,7 @@ const Routes = ({user}) => {
<
Route
path=
{
LOGIN
}
element=
{
<
Login
/>
}
/>
<
Route
path=
{
PRODUCT_LIST
}
element=
{
<
Products
/>
}
/>
<
Route
path=
{
PRODUCT_ADD
}
element=
{
productAdd
}
/>
<
Route
path=
{
USER_PROFILE
}
element=
{
userProfile
}
/>
<
Route
path=
{
PRODUCT_VIEW
}
element=
{
<
h1
>
Show product
</
h1
>
}
/>
</
Route
>
</
RoutesSwitch
>;
...
...
front_client/src/components/UI/AppToolbar/Menus/UserMenu/UserMenu.jsx
View file @
ba5af6c4
...
...
@@ -2,8 +2,8 @@ import {useState} from "react";
import
{
Button
,
Menu
,
MenuItem
}
from
'@mui/material'
;
import
{
useDispatch
}
from
"react-redux"
;
import
{
logoutUser
}
from
"../../../../../store/actions/usersActions"
;
import
{
useNavigate
}
from
"react-router-dom"
;
import
{
MAIN
}
from
"../../../../../constants/routes"
;
import
{
NavLink
,
useNavigate
}
from
"react-router-dom"
;
import
{
MAIN
,
USER_PROFILE
}
from
"../../../../../constants/routes"
;
const
UserMenu
=
({
user
})
=>
{
const
navigate
=
useNavigate
();
...
...
@@ -33,7 +33,7 @@ const UserMenu = ({user}) => {
onClose=
{
handleClose
}
keepMounted
>
<
MenuItem
>
Profile
</
MenuItem
>
<
MenuItem
component=
{
NavLink
}
to=
{
USER_PROFILE
}
>
Profile
</
MenuItem
>
<
MenuItem
>
My account
</
MenuItem
>
<
MenuItem
onClick=
{
()
=>
dispatch
(
logoutUser
({
callback
:
()
=>
navigate
(
MAIN
)}))
}
...
...
front_client/src/components/User/UserForm/UserForm.jsx
0 → 100644
View file @
ba5af6c4
import
{
useState
}
from
"react"
;
import
{
Button
,
Grid
}
from
"@mui/material"
;
import
FormElement
from
"../../UI/Form/FormElement/FormElement"
;
const
UserForm
=
({
onFormSubmit
,
user
})
=>
{
const
[
state
,
setState
]
=
useState
({
oldPassword
:
""
,
password
:
""
,
username
:
user
.
username
});
const
submitFormHandler
=
e
=>
{
e
.
preventDefault
();
onFormSubmit
(
state
);
};
const
inputChangeHandler
=
e
=>
{
const
name
=
e
.
target
.
name
;
const
value
=
e
.
target
.
value
;
setState
(
prevState
=>
{
return
{...
prevState
,
[
name
]:
value
};
});
};
return
(
<
form
autoComplete=
"off"
onSubmit=
{
submitFormHandler
}
>
<
Grid
container
direction=
"column"
spacing=
{
2
}
>
<
FormElement
id=
"oldPassword"
label=
"Old Password"
required=
{
!!
state
.
password
.
trim
()
}
value=
{
state
.
oldPassword
}
onChange=
{
inputChangeHandler
}
name=
"oldPassword"
/>
<
FormElement
id=
"password"
label=
"New password"
required=
{
!!
state
.
oldPassword
.
trim
()
}
value=
{
state
.
password
}
onChange=
{
inputChangeHandler
}
name=
"password"
/>
<
FormElement
id=
"username"
label=
"Username"
value=
{
state
.
username
}
onChange=
{
inputChangeHandler
}
name=
"username"
/>
<
Grid
item
xs
>
<
Button
type=
"submit"
color=
"primary"
variant=
"contained"
>
Save
</
Button
>
</
Grid
>
</
Grid
>
</
form
>
);
};
export
default
UserForm
;
front_client/src/constants/config.js
View file @
ba5af6c4
export
const
apiUrl
=
"http://localhost:80
01
"
;
export
const
apiUrl
=
"http://localhost:80
10
"
;
export
const
uploadUrl
=
apiUrl
+
'/uploads'
;
front_client/src/constants/routes.js
View file @
ba5af6c4
...
...
@@ -4,3 +4,4 @@ export const PRODUCT_LIST = '/products/';
export
const
PRODUCT_VIEW
=
'/products/:id'
;
export
const
REGISTER
=
'/register'
;
export
const
LOGIN
=
'/login'
;
export
const
USER_PROFILE
=
'/user/profile'
;
front_client/src/containers/User/User.jsx
0 → 100644
View file @
ba5af6c4
import
{
useDispatch
,
useSelector
}
from
"react-redux"
;
import
{
Typography
}
from
"@mui/material"
;
import
UserForm
from
"../../components/User/UserForm/UserForm"
;
import
{
useNavigate
}
from
"react-router-dom"
;
import
{
updateUser
}
from
"../../store/actions/usersActions"
;
const
User
=
()
=>
{
const
navigate
=
useNavigate
();
const
dispatch
=
useDispatch
();
const
user
=
useSelector
(({
usersState
})
=>
usersState
.
user
);
const
onFormSubmit
=
async
data
=>
{
await
dispatch
(
updateUser
({
data
,
callback
:
()
=>
navigate
(
'/'
)}));
};
return
(
<>
<
Typography
variant=
'h4'
>
Edit
{
user
.
username
}
</
Typography
>
<
UserForm
onFormSubmit=
{
onFormSubmit
}
user=
{
user
}
/>
</>
);
};
export
default
User
;
front_client/src/store/actions/usersActions.js
View file @
ba5af6c4
...
...
@@ -44,3 +44,18 @@ export const logoutUser = createAsyncThunk(
throw
e
;
})
);
export
const
updateUser
=
createAsyncThunk
(
'users/update'
,
async
({
data
,
callback
},
{
dispatch
})
=>
await
axiosApi
.
put
(
'/users'
,
data
)
.
then
(
res
=>
{
dispatch
(
setUser
(
res
.
data
));
callback
();
})
.
catch
(
e
=>
{
if
(
e
?.
response
?.
data
)
dispatch
(
setLoginError
(
e
.
response
.
data
));
else
dispatch
(
setLoginError
(
e
));
throw
e
;
})
);
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