@@ -11,6 +11,7 @@ import {
11
11
ResponsiveActions ,
12
12
SkeletonTableBody ,
13
13
SkeletonTableHead ,
14
+ WarningModal ,
14
15
} from '@patternfly/react-component-groups' ;
15
16
import {
16
17
DataView ,
@@ -22,15 +23,17 @@ import {
22
23
DataViewToolbar ,
23
24
DataViewTrTree ,
24
25
useDataViewSelection ,
26
+ DataViewTrObject ,
25
27
} from '@patternfly/react-data-view' ;
26
28
import { Workspace } from '../../redux/reducers/workspaces-reducer' ;
27
29
import { RBACStore } from '../../redux/store' ;
28
30
import AppLink from '../../presentational-components/shared/AppLink' ;
29
31
import pathnames from '../../utilities/pathnames' ;
30
32
import messages from '../../Messages' ;
31
33
import useAppNavigate from '../../hooks/useAppNavigate' ;
32
- import { EmptyState , EmptyStateHeader , EmptyStateIcon , EmptyStateBody } from '@patternfly/react-core' ;
34
+ import { EmptyState , EmptyStateHeader , EmptyStateIcon , EmptyStateBody , ButtonVariant } from '@patternfly/react-core' ;
33
35
import { SearchIcon } from '@patternfly/react-icons' ;
36
+ import { ActionsColumn } from '@patternfly/react-table' ;
34
37
35
38
interface WorkspaceFilters {
36
39
name : string ;
@@ -50,26 +53,6 @@ const mapWorkspacesToHierarchy = (workspaceData: Workspace[]): Workspace | undef
50
53
return root ;
51
54
} ;
52
55
53
- const buildRows = ( workspaces : Workspace [ ] ) : DataViewTrTree [ ] =>
54
- workspaces . map ( ( workspace ) => ( {
55
- row : [
56
- < AppLink
57
- to = { pathnames [ 'workspace-detail' ] . link . replace ( ':workspaceId' , workspace . id ) }
58
- key = { `${ workspace . id } -detail` }
59
- className = "rbac-m-hide-on-sm"
60
- >
61
- { workspace . name }
62
- </ AppLink > ,
63
- workspace . description ,
64
- ] ,
65
- id : workspace . id ,
66
- ...( workspace . children && workspace . children . length > 0
67
- ? {
68
- children : buildRows ( workspace . children ) ,
69
- }
70
- : { } ) ,
71
- } ) ) ;
72
-
73
56
const EmptyWorkspacesTable : React . FunctionComponent < { titleText : string } > = ( { titleText } ) => {
74
57
return (
75
58
< EmptyState >
@@ -116,6 +99,50 @@ const WorkspaceListTable = () => {
116
99
const dispatch = useDispatch ( ) ;
117
100
const navigate = useAppNavigate ( ) ;
118
101
const selection = useDataViewSelection ( { matchOption : ( a , b ) => a . id === b . id } ) ;
102
+ const [ isDeleteModalOpen , setIsDeleteModalOpen ] = useState ( false ) ;
103
+ const [ currentWorkspaces , setCurrentWorkspaces ] = useState < Workspace [ ] > ( [ ] ) ;
104
+
105
+ const handleModalToggle = ( workspaces : Workspace [ ] ) => {
106
+ setCurrentWorkspaces ( workspaces ) ;
107
+ setIsDeleteModalOpen ( ! isDeleteModalOpen ) ;
108
+ } ;
109
+
110
+ const buildRows = ( workspaces : Workspace [ ] ) : DataViewTrTree [ ] =>
111
+ workspaces . map ( ( workspace ) => ( {
112
+ row : Object . values ( {
113
+ name : (
114
+ < AppLink
115
+ to = { pathnames [ 'workspace-detail' ] . link . replace ( ':workspaceId' , workspace . id ) }
116
+ key = { `${ workspace . id } -detail` }
117
+ className = "rbac-m-hide-on-sm"
118
+ >
119
+ { workspace . name }
120
+ </ AppLink >
121
+ ) ,
122
+ description : workspace . description ,
123
+ rowActions : {
124
+ cell : (
125
+ < ActionsColumn
126
+ items = { [
127
+ {
128
+ title : 'Delete workspace' ,
129
+ onClick : ( ) => {
130
+ handleModalToggle ( [ workspace ] ) ;
131
+ } ,
132
+ } ,
133
+ ] }
134
+ />
135
+ ) ,
136
+ props : { isActionCell : true } ,
137
+ } ,
138
+ } ) ,
139
+ id : workspace . id ,
140
+ ...( workspace . children && workspace . children . length > 0
141
+ ? {
142
+ children : buildRows ( workspace . children ) ,
143
+ }
144
+ : { } ) ,
145
+ } ) ) ;
119
146
120
147
const { isLoading, workspaces, error } = useSelector ( ( state : RBACStore ) => ( {
121
148
workspaces : state . workspacesReducer . workspaces || [ ] ,
@@ -152,12 +179,55 @@ const WorkspaceListTable = () => {
152
179
selection . onSelect ( value === BulkSelectValue . all , value === BulkSelectValue . all ? workspaces : [ ] ) ;
153
180
} ;
154
181
182
+ const hasAssets = useMemo ( ( ) => {
183
+ return selection . selected . filter ( ( ws ) => ws . children && ws . children ?. length > 0 ) . length > 0 ? true : false ;
184
+ } , [ selection . selected , workspaces ] ) ;
185
+
155
186
if ( error ) {
156
187
return < ErrorState errorDescription = { error } /> ;
157
188
}
158
189
159
190
return (
160
191
< React . Fragment >
192
+ { isDeleteModalOpen && (
193
+ < WarningModal
194
+ ouiaId = { 'remove-workspaces-modal' }
195
+ isOpen = { isDeleteModalOpen }
196
+ title = { intl . formatMessage ( messages . deleteWorkspaceModalHeader ) }
197
+ confirmButtonLabel = { ! hasAssets ? intl . formatMessage ( messages . delete ) : intl . formatMessage ( messages . gotItButtonLabel ) }
198
+ confirmButtonVariant = { ! hasAssets ? ButtonVariant . danger : ButtonVariant . primary }
199
+ withCheckbox = { ! hasAssets }
200
+ checkboxLabel = { intl . formatMessage ( messages . understandActionIrreversible ) }
201
+ onClose = { ( ) => setIsDeleteModalOpen ( false ) }
202
+ onConfirm = { ( ) => {
203
+ ! hasAssets ? console . log ( 'deleting workspaces' ) : null ;
204
+ setIsDeleteModalOpen ( false ) ;
205
+ } }
206
+ cancelButtonLabel = { ! hasAssets ? 'Cancel' : '' }
207
+ >
208
+ { hasAssets ? (
209
+ < FormattedMessage
210
+ { ...messages . workspaceNotEmptyWarning }
211
+ values = { {
212
+ b : ( text ) => < b > { text } </ b > ,
213
+ count : currentWorkspaces . length ,
214
+ plural : currentWorkspaces . length > 1 ? intl . formatMessage ( messages . workspaces ) : intl . formatMessage ( messages . workspace ) ,
215
+ name : currentWorkspaces [ 0 ] ?. name ,
216
+ } }
217
+ />
218
+ ) : (
219
+ < FormattedMessage
220
+ { ...messages . deleteWorkspaceModalBody }
221
+ values = { {
222
+ b : ( text ) => < b > { text } </ b > ,
223
+ count : currentWorkspaces . length ,
224
+ plural : currentWorkspaces . length > 1 ? intl . formatMessage ( messages . workspaces ) : intl . formatMessage ( messages . workspace ) ,
225
+ name : currentWorkspaces [ 0 ] ?. name ,
226
+ } }
227
+ />
228
+ ) }
229
+ </ WarningModal >
230
+ ) }
161
231
< DataView selection = { selection } activeState = { activeState } >
162
232
< DataViewToolbar
163
233
bulkSelect = {
@@ -187,6 +257,17 @@ const WorkspaceListTable = () => {
187
257
< ResponsiveAction ouiaId = "create-workspace-button" isPinned onClick = { ( ) => navigate ( { pathname : pathnames [ 'create-workspace' ] . link } ) } >
188
258
{ intl . formatMessage ( messages . createWorkspace ) }
189
259
</ ResponsiveAction >
260
+ < ResponsiveAction
261
+ ouiaId = "delete-workspace-button"
262
+ isDisabled = { selection . selected . length === 0 }
263
+ onClick = { ( ) => {
264
+ handleModalToggle (
265
+ workspaces . filter ( ( workspace ) => selection . selected . some ( ( selectedRow : DataViewTrObject ) => selectedRow . id === workspace . id ) )
266
+ ) ;
267
+ } }
268
+ >
269
+ { intl . formatMessage ( messages . workspacesActionDeleteWorkspace ) }
270
+ </ ResponsiveAction >
190
271
</ ResponsiveActions >
191
272
}
192
273
/>
0 commit comments