Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve delete
  • Loading branch information
BrunoQuaresma committed Aug 10, 2023
commit 18965e0fa876fcfcb76a3eb8f886d82c96ebc879
51 changes: 43 additions & 8 deletions site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import { useTemplateFilterMenu, useStatusFilterMenu } from "./filter/menus"
import { useSearchParams } from "react-router-dom"
import { useFilter } from "components/Filter/filter"
import { useUserFilterMenu } from "components/Filter/UserFilter"
import { getWorkspaces } from "api/api"
import { deleteWorkspace, getWorkspaces } from "api/api"
import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
import Box from "@mui/material/Box"
import { MONOSPACE_FONT_FAMILY } from "theme/constants"
import TextField from "@mui/material/TextField"
import { displayError } from "components/GlobalSnackbar/utils"
import { getErrorMessage } from "api/errors"

const WorkspacesPage: FC = () => {
const [lockedWorkspaces, setLockedWorkspaces] = useState<Workspace[]>([])
Expand All @@ -25,7 +27,7 @@ const WorkspacesPage: FC = () => {
const searchParamsResult = useSearchParams()
const pagination = usePagination({ searchParamsResult })
const filterProps = useWorkspacesFilter({ searchParamsResult, pagination })
const { data, error, queryKey } = useWorkspacesData({
const { data, error, queryKey, refetch } = useWorkspacesData({
...pagination,
query: filterProps.filter.query,
})
Expand Down Expand Up @@ -101,6 +103,10 @@ const WorkspacesPage: FC = () => {
onClose={() => {
setIsDeletingAll(false)
}}
onDelete={async () => {
await refetch()
setCheckedWorkspaces([])
}}
/>
</>
)
Expand Down Expand Up @@ -159,38 +165,69 @@ const BatchDeleteConfirmation = ({
checkedWorkspaces,
open,
onClose,
onDelete,
}: {
checkedWorkspaces: Workspace[]
open: boolean
onClose: () => void
onDelete: () => void
}) => {
const [confirmValue, setConfirmValue] = useState("")
const [confirmError, setConfirmError] = useState(false)
const [isDeleting, setIsDeleting] = useState(false)

const close = () => {
if (isDeleting) {
return
}

onClose()
setConfirmValue("")
setConfirmError(false)
setIsDeleting(false)
}

const confirmDeletion = async () => {
setConfirmError(false)

const confirmDeletion = () => {
if (confirmValue.toLowerCase() !== "delete") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could consider only accepting the value as stated (i.e. must be upper-case DELETE) to confirm. But there may be differing opinions on this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can test it as it is now on dev.coder.com and see if we get any feedback.

setConfirmError(true)
return
}

try {
setIsDeleting(true)
await Promise.all(checkedWorkspaces.map((w) => deleteWorkspace(w.id)))
} catch (e) {
displayError(
"Error on deleting workspaces",
getErrorMessage(e, "An error occurred while deleting the workspaces"),
)
} finally {
close()
onDelete()
}
}

return (
<ConfirmDialog
type="delete"
open={open}
confirmLoading={isDeleting}
onConfirm={confirmDeletion}
onClose={() => {
onClose()
setConfirmValue("")
setConfirmError(false)
}}
type="delete"
title={`Delete ${checkedWorkspaces?.length} ${
checkedWorkspaces.length === 1 ? "workspace" : "workspaces"
}`}
description={
<form
onSubmit={(e) => {
onSubmit={async (e) => {
e.preventDefault()
confirmDeletion()
await confirmDeletion()
}}
>
<Box>
Expand Down Expand Up @@ -223,8 +260,6 @@ const BatchDeleteConfirmation = ({
/>
</form>
}
confirmLoading={false}
onConfirm={confirmDeletion}
/>
)
}