@@ -11,11 +11,13 @@ import { useTemplateFilterMenu, useStatusFilterMenu } from "./filter/menus"
11
11
import { useSearchParams } from "react-router-dom"
12
12
import { useFilter } from "components/Filter/filter"
13
13
import { useUserFilterMenu } from "components/Filter/UserFilter"
14
- import { getWorkspaces } from "api/api"
14
+ import { deleteWorkspace , getWorkspaces } from "api/api"
15
15
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
16
16
import Box from "@mui/material/Box"
17
17
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
18
18
import TextField from "@mui/material/TextField"
19
+ import { displayError } from "components/GlobalSnackbar/utils"
20
+ import { getErrorMessage } from "api/errors"
19
21
20
22
const WorkspacesPage : FC = ( ) => {
21
23
const [ lockedWorkspaces , setLockedWorkspaces ] = useState < Workspace [ ] > ( [ ] )
@@ -25,7 +27,7 @@ const WorkspacesPage: FC = () => {
25
27
const searchParamsResult = useSearchParams ( )
26
28
const pagination = usePagination ( { searchParamsResult } )
27
29
const filterProps = useWorkspacesFilter ( { searchParamsResult, pagination } )
28
- const { data, error, queryKey } = useWorkspacesData ( {
30
+ const { data, error, queryKey, refetch } = useWorkspacesData ( {
29
31
...pagination ,
30
32
query : filterProps . filter . query ,
31
33
} )
@@ -101,6 +103,10 @@ const WorkspacesPage: FC = () => {
101
103
onClose = { ( ) => {
102
104
setIsDeletingAll ( false )
103
105
} }
106
+ onDelete = { async ( ) => {
107
+ await refetch ( )
108
+ setCheckedWorkspaces ( [ ] )
109
+ } }
104
110
/>
105
111
</ >
106
112
)
@@ -159,38 +165,69 @@ const BatchDeleteConfirmation = ({
159
165
checkedWorkspaces,
160
166
open,
161
167
onClose,
168
+ onDelete,
162
169
} : {
163
170
checkedWorkspaces : Workspace [ ]
164
171
open : boolean
165
172
onClose : ( ) => void
173
+ onDelete : ( ) => void
166
174
} ) => {
167
175
const [ confirmValue , setConfirmValue ] = useState ( "" )
168
176
const [ confirmError , setConfirmError ] = useState ( false )
177
+ const [ isDeleting , setIsDeleting ] = useState ( false )
178
+
179
+ const close = ( ) => {
180
+ if ( isDeleting ) {
181
+ return
182
+ }
183
+
184
+ onClose ( )
185
+ setConfirmValue ( "" )
186
+ setConfirmError ( false )
187
+ setIsDeleting ( false )
188
+ }
189
+
190
+ const confirmDeletion = async ( ) => {
191
+ setConfirmError ( false )
169
192
170
- const confirmDeletion = ( ) => {
171
193
if ( confirmValue . toLowerCase ( ) !== "delete" ) {
172
194
setConfirmError ( true )
173
195
return
174
196
}
197
+
198
+ try {
199
+ setIsDeleting ( true )
200
+ await Promise . all ( checkedWorkspaces . map ( ( w ) => deleteWorkspace ( w . id ) ) )
201
+ } catch ( e ) {
202
+ displayError (
203
+ "Error on deleting workspaces" ,
204
+ getErrorMessage ( e , "An error occurred while deleting the workspaces" ) ,
205
+ )
206
+ } finally {
207
+ close ( )
208
+ onDelete ( )
209
+ }
175
210
}
176
211
177
212
return (
178
213
< ConfirmDialog
214
+ type = "delete"
179
215
open = { open }
216
+ confirmLoading = { isDeleting }
217
+ onConfirm = { confirmDeletion }
180
218
onClose = { ( ) => {
181
219
onClose ( )
182
220
setConfirmValue ( "" )
183
221
setConfirmError ( false )
184
222
} }
185
- type = "delete"
186
223
title = { `Delete ${ checkedWorkspaces ?. length } ${
187
224
checkedWorkspaces . length === 1 ? "workspace" : "workspaces"
188
225
} `}
189
226
description = {
190
227
< form
191
- onSubmit = { ( e ) => {
228
+ onSubmit = { async ( e ) => {
192
229
e . preventDefault ( )
193
- confirmDeletion ( )
230
+ await confirmDeletion ( )
194
231
} }
195
232
>
196
233
< Box >
@@ -223,8 +260,6 @@ const BatchDeleteConfirmation = ({
223
260
/>
224
261
</ form >
225
262
}
226
- confirmLoading = { false }
227
- onConfirm = { confirmDeletion }
228
263
/>
229
264
)
230
265
}
0 commit comments