14
14
// You should have received a copy of the GNU Affero General Public License
15
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
17
- import React from "react" ;
18
- import { useDispatch } from "react-redux" ;
17
+ import React , { useEffect , useState , Fragment } from "react" ;
18
+ import { connect } from "react-redux" ;
19
19
import { DialogContentText } from "@mui/material" ;
20
+ import { setErrorSnackMessage } from "../../../systemSlice" ;
20
21
import useApi from "../Common/Hooks/useApi" ;
21
22
import ConfirmDialog from "../Common/ModalWrapper/ConfirmDialog" ;
22
23
import { ErrorResponseHandler } from "../../../common/types" ;
23
24
import { ConfirmDeleteIcon } from "../../../icons" ;
24
25
import { encodeURLString } from "../../../common/utils" ;
25
- import { setErrorSnackMessage } from "../../../systemSlice" ;
26
-
26
+ import WarningMessage from "../Common/WarningMessage/WarningMessage" ;
27
+ import TableWrapper from "../Common/TableWrapper/TableWrapper" ;
28
+ import api from "../../../common/api" ;
29
+ import { IAM_PAGES } from "../../../common/SecureComponent/permissions" ;
30
+ import Loader from "../Common/Loader/Loader" ;
27
31
interface IDeleteUserProps {
28
32
closeDeleteModalAndRefresh : ( refresh : boolean ) => void ;
29
33
deleteOpen : boolean ;
30
34
selectedUsers : string [ ] | null ;
35
+ setErrorSnackMessage : typeof setErrorSnackMessage ;
36
+ history : any ;
31
37
}
32
38
33
39
const DeleteUser = ( {
34
40
closeDeleteModalAndRefresh,
35
41
deleteOpen,
36
42
selectedUsers,
43
+ setErrorSnackMessage,
44
+ history,
37
45
} : IDeleteUserProps ) => {
38
- const dispatch = useDispatch ( ) ;
39
46
const onDelSuccess = ( ) => closeDeleteModalAndRefresh ( true ) ;
40
- const onDelError = ( err : ErrorResponseHandler ) =>
41
- dispatch ( setErrorSnackMessage ( err ) ) ;
47
+ const onDelError = ( err : ErrorResponseHandler ) => setErrorSnackMessage ( err ) ;
42
48
const onClose = ( ) => closeDeleteModalAndRefresh ( false ) ;
43
49
44
50
const [ deleteLoading , invokeDeleteApi ] = useApi ( onDelSuccess , onDelError ) ;
51
+ const [ loadingSA , setLoadingSA ] = useState < boolean > ( true ) ;
52
+ const [ hasSA , setHasSA ] = useState < boolean > ( false ) ;
53
+ const [ userSAList , setUserSAList ] = useState < userSACount [ ] > ( [ ] ) ;
45
54
46
55
const userLoggedIn = localStorage . getItem ( "userLoggedIn" ) || "" ;
56
+
57
+ useEffect ( ( ) => {
58
+
59
+ if ( selectedUsers ) {
60
+ api
61
+ . invoke ( "POST" , `/api/v1/users/service-accounts` , selectedUsers )
62
+ . then ( ( res ) => {
63
+ setUserSAList ( res . userServiceAccountList ) ;
64
+ if ( res . hasSA ) {
65
+ setHasSA ( true )
66
+ }
67
+ setLoadingSA ( false ) ;
68
+ } )
69
+ . catch ( ( err : ErrorResponseHandler ) => {
70
+ setErrorSnackMessage ( err ) ;
71
+ setLoadingSA ( false ) ;
72
+ } ) ;
73
+ }
74
+ } , [ selectedUsers , setErrorSnackMessage ] ) ;
47
75
48
76
if ( ! selectedUsers ) {
49
77
return null ;
@@ -52,7 +80,18 @@ const DeleteUser = ({
52
80
< div key = { user } >
53
81
< b > { user } </ b >
54
82
</ div >
55
- ) ) ;
83
+ ) ) ;
84
+ const viewAction = ( selectionElement : any ) : void => {
85
+ history . push (
86
+ `${ IAM_PAGES . USERS } /${ encodeURLString ( selectionElement . userName ) } `
87
+ ) ;
88
+ } ;
89
+ const tableActions = [
90
+ {
91
+ type : "view" ,
92
+ onClick : viewAction ,
93
+ } ,
94
+ ] ;
56
95
57
96
const onConfirmDelete = ( ) => {
58
97
for ( let user of selectedUsers ) {
@@ -68,7 +107,18 @@ const DeleteUser = ({
68
107
}
69
108
} ;
70
109
110
+ interface userSACount {
111
+ userName : string ;
112
+ numSAs : number ;
113
+ }
114
+
115
+ const noSAtext = "Are you sure you want to delete the following " + selectedUsers . length + " " +
116
+ "user" + ( selectedUsers . length > 1 ? "s?" : "?" )
117
+
71
118
return (
119
+ loadingSA ?
120
+ < Loader />
121
+ :
72
122
< ConfirmDialog
73
123
title = { `Delete User${ selectedUsers . length > 1 ? "s" : "" } ` }
74
124
confirmText = { "Delete" }
@@ -78,14 +128,42 @@ const DeleteUser = ({
78
128
onConfirm = { onConfirmDelete }
79
129
onClose = { onClose }
80
130
confirmationContent = {
81
- < DialogContentText >
82
- Are you sure you want to delete the following { selectedUsers . length } { " " }
83
- user{ selectedUsers . length > 1 ? "s?" : "?" }
84
- < b > { renderUsers } </ b >
131
+ < DialogContentText >
132
+
133
+ { hasSA ?
134
+ < Fragment >
135
+ < WarningMessage
136
+ label = "Click on a user to view the full listing of asociated Service Accounts. All Service Accounts associated with a user will be deleted along with the user. Are you sure you want to continue?"
137
+ title = "Warning: One or more users selected has associated Service Accounts. "
138
+ />
139
+ < TableWrapper
140
+ itemActions = { tableActions }
141
+ columns = { [
142
+ { label : "Username" , elementKey : "userName" } ,
143
+ { label : "# Associated Service Accounts" , elementKey : "numSAs" } ,
144
+ ] }
145
+ isLoading = { loadingSA }
146
+ records = { userSAList }
147
+ entityName = "User Service Accounts"
148
+ idField = "userName"
149
+ customPaperHeight = "250"
150
+ />
151
+ </ Fragment >
152
+ : < Fragment >
153
+ { noSAtext }
154
+ { renderUsers }
155
+ </ Fragment >
156
+ }
85
157
</ DialogContentText >
86
158
}
87
159
/>
88
160
) ;
89
161
} ;
90
162
91
- export default DeleteUser ;
163
+ const mapDispatchToProps = {
164
+ setErrorSnackMessage,
165
+ } ;
166
+
167
+ const connector = connect ( null , mapDispatchToProps ) ;
168
+
169
+ export default connector ( DeleteUser ) ;
0 commit comments