@@ -6,6 +6,9 @@ import ConfirmDialog from "../control/ConfirmDialog";
6
6
import TrashIcon from "./icons/tabler/TrashIcon" ;
7
7
import { joinClasses } from "@/utils/common" ;
8
8
import UserCogIcon from "./icons/tabler/UserCogIcon" ;
9
+ import * as UserService from "@/apis/user" ;
10
+ import { useSelector } from "react-redux" ;
11
+ import { userInfoSelector } from "@/store/selectors" ;
9
12
10
13
export interface UserTableProps {
11
14
data : UserServiceModel . UserInfo [ ] ;
@@ -15,6 +18,9 @@ export interface UserTableProps {
15
18
16
19
const UserTable : React . FC < UserTableProps > = ( props ) => {
17
20
const { t } = useTranslation ( ) ;
21
+ const [ deletingAccount , setDeletingAccount ] = React . useState < string | null > (
22
+ null ,
23
+ ) ;
18
24
19
25
return (
20
26
< >
@@ -47,7 +53,12 @@ const UserTable: React.FC<UserTableProps> = (props) => {
47
53
</ td >
48
54
{ props . showActions && (
49
55
< td className = "p-2" >
50
- < UserActions userInfo = { userInfo } onClickDelete = { ( ) => { } } />
56
+ < UserActions
57
+ userInfo = { userInfo }
58
+ onClickDelete = { ( ) => {
59
+ setDeletingAccount ( userInfo . account ) ;
60
+ } }
61
+ />
51
62
</ td >
52
63
) }
53
64
</ tr >
@@ -58,9 +69,17 @@ const UserTable: React.FC<UserTableProps> = (props) => {
58
69
< ConfirmDialog
59
70
id = "user_delete_confirm_modal"
60
71
title = "Confirm"
61
- message = "Are you sure to delete this user?"
72
+ message = { t ( "Are you sure to delete this user?" ) }
62
73
onClickConfirm = { ( ) => {
63
- window . location . reload ( ) ;
74
+ if ( deletingAccount ) {
75
+ UserService . deleteUser ( deletingAccount )
76
+ . then ( ( ) => {
77
+ window . location . reload ( ) ;
78
+ } )
79
+ . catch ( ( err ) => {
80
+ console . error ( err ) ;
81
+ } ) ;
82
+ }
64
83
} }
65
84
/>
66
85
</ >
@@ -92,24 +111,50 @@ interface ActionsProps {
92
111
}
93
112
94
113
const UserActions : React . FC < ActionsProps > = ( props ) => {
114
+ const userInfo = useSelector ( userInfoSelector ) ;
115
+
95
116
const onClickDelete = ( e : React . MouseEvent ) => {
96
117
e . preventDefault ( ) ;
97
118
e . stopPropagation ( ) ;
98
119
99
120
props . onClickDelete ( ) ;
100
121
const modal = document . getElementById (
101
- "delete_confirm_modal " ,
122
+ "user_delete_confirm_modal " ,
102
123
) as HTMLDialogElement ;
103
124
modal ?. showModal ( ) ;
104
125
} ;
105
126
106
127
return (
107
128
< div className = "flex space-x-1" >
108
- < button className = "btn btn-square btn-ghost btn-sm rounded" >
129
+ < button
130
+ className = { joinClasses (
131
+ "btn btn-square btn-ghost btn-sm rounded" ,
132
+ ( props . userInfo . roles ?. includes ( "super" ) ||
133
+ props . userInfo . account === userInfo ?. account ) &&
134
+ "btn-disabled" ,
135
+ ) }
136
+ onClick = { ( ) => {
137
+ if ( props . userInfo . roles ?. includes ( "admin" ) ) {
138
+ UserService . revokeUserRole ( props . userInfo . account , "admin" ) . catch (
139
+ ( err ) => {
140
+ console . error ( err ) ;
141
+ } ,
142
+ ) ;
143
+ } else if ( ! props . userInfo . roles ?. includes ( "super" ) ) {
144
+ UserService . grantUserRole ( props . userInfo . account , "admin" ) . catch (
145
+ ( err ) => {
146
+ console . error ( err ) ;
147
+ } ,
148
+ ) ;
149
+ }
150
+ window . location . reload ( ) ;
151
+ } }
152
+ >
109
153
< UserCogIcon
110
154
className = { joinClasses (
111
155
"h-5 w-5" ,
112
- props . userInfo . roles ?. includes ( "admin" )
156
+ props . userInfo . roles ?. includes ( "admin" ) ||
157
+ props . userInfo . roles ?. includes ( "super" )
113
158
? "text-success"
114
159
: "text-base-content" ,
115
160
) }
0 commit comments