1
|
import {Fragment, FunctionComponent, useEffect} from 'react'
|
2
|
import {useDispatch, useSelector} from 'react-redux'
|
3
|
import { useNavigate } from 'react-router-dom'
|
4
|
import { RootState } from '../redux/store'
|
5
|
import ChangePasswordDialog, {ChangePasswordDialogProps} from "./ChangePasswordDialog"
|
6
|
import {UserDto} from "../../swagger/data-contracts"
|
7
|
import {resetIsPasswordChanged, resetIsRegistered} from "../Auth/userSlice"
|
8
|
|
9
|
|
10
|
const ChangePassword = () => {
|
11
|
const userLoggedIn = useSelector(
|
12
|
(state: RootState) => state.user.isLoggedIn
|
13
|
)
|
14
|
|
15
|
const navigate = useNavigate()
|
16
|
|
17
|
const selectedUser: UserDto | undefined = useSelector((state: RootState) => state.usersDetail.selectedUser)
|
18
|
const isAdmin = useSelector(
|
19
|
(state: RootState) => state.user.roles.includes("ROLE_ADMIN")
|
20
|
)
|
21
|
|
22
|
const isPasswordChanged = useSelector(
|
23
|
(state: RootState) => state.user.isPasswordChanged
|
24
|
)
|
25
|
|
26
|
const dispatch = useDispatch()
|
27
|
|
28
|
useEffect(() => {
|
29
|
if (isPasswordChanged) {
|
30
|
dispatch(resetIsPasswordChanged())
|
31
|
navigate('/administration')
|
32
|
}
|
33
|
}, [isPasswordChanged, navigate])
|
34
|
|
35
|
// Redirect to home if the user is not logged in
|
36
|
useEffect(() => {
|
37
|
if (!userLoggedIn) {
|
38
|
navigate('/')
|
39
|
}
|
40
|
}, [userLoggedIn, navigate])
|
41
|
|
42
|
return (
|
43
|
<Fragment>
|
44
|
<ChangePasswordDialog username={selectedUser?.email} isAdmin={isAdmin} />
|
45
|
</Fragment>
|
46
|
)
|
47
|
}
|
48
|
|
49
|
export default ChangePassword
|