Skip to content

feat: Delete workspace #1822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ComponentMeta, Story } from "@storybook/react"
import React from "react"
import { DeleteWorkspaceDialog, DeleteWorkspaceDialogProps } from "./DeleteWorkspaceDialog"

export default {
title: "Components/DeleteWorkspaceDialog",
component: DeleteWorkspaceDialog,
argTypes: {
onClose: {
action: "onClose",
},
onConfirm: {
action: "onConfirm",
},
open: {
control: "boolean",
defaultValue: true,
},
title: {
defaultValue: "Confirm Dialog",
},
},
} as ComponentMeta<typeof DeleteWorkspaceDialog>

const Template: Story<DeleteWorkspaceDialogProps> = (args) => <DeleteWorkspaceDialog {...args} />

export const Example = Template.bind({})
Example.args = {
isOpen: true,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from "react"
import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog"

const Language = {
deleteDialogTitle: "Delete workspace?",
deleteDialogMessage: "Deleting your workspace is irreversible. Are you sure?",
}

export interface DeleteWorkspaceDialogProps {
isOpen: boolean
handleConfirm: () => void
handleCancel: () => void
}

export const DeleteWorkspaceDialog: React.FC<DeleteWorkspaceDialogProps> = ({
isOpen,
handleCancel,
handleConfirm,
}) => (
<ConfirmDialog
type="delete"
hideCancel={false}
open={isOpen}
title={Language.deleteDialogTitle}
onConfirm={handleConfirm}
onClose={handleCancel}
description={Language.deleteDialogMessage}
/>
)
3 changes: 3 additions & 0 deletions site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { WorkspaceStats } from "../WorkspaceStats/WorkspaceStats"
export interface WorkspaceProps {
handleStart: () => void
handleStop: () => void
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
workspace: TypesGen.Workspace
Expand All @@ -28,6 +29,7 @@ export interface WorkspaceProps {
export const Workspace: React.FC<WorkspaceProps> = ({
handleStart,
handleStop,
handleDelete,
handleUpdate,
handleCancel,
workspace,
Expand Down Expand Up @@ -55,6 +57,7 @@ export const Workspace: React.FC<WorkspaceProps> = ({
workspace={workspace}
handleStart={handleStart}
handleStop={handleStop}
handleDelete={handleDelete}
handleUpdate={handleUpdate}
handleCancel={handleCancel}
/>
Expand Down
16 changes: 16 additions & 0 deletions site/src/components/WorkspaceActions/WorkspaceActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Button from "@material-ui/core/Button"
import { makeStyles } from "@material-ui/core/styles"
import CancelIcon from "@material-ui/icons/Cancel"
import CloudDownloadIcon from "@material-ui/icons/CloudDownload"
import DeleteIcon from "@material-ui/icons/Delete"
import PlayArrowRoundedIcon from "@material-ui/icons/PlayArrowRounded"
import StopIcon from "@material-ui/icons/Stop"
import React from "react"
Expand All @@ -15,6 +16,8 @@ export const Language = {
stopping: "Stopping workspace",
start: "Start workspace",
starting: "Starting workspace",
delete: "Delete workspace",
deleting: "Deleting workspace",
cancel: "Cancel action",
update: "Update workspace",
}
Expand All @@ -38,10 +41,14 @@ const canStart = (workspaceStatus: WorkspaceStatus) => ["stopped", "canceled", "

const canStop = (workspaceStatus: WorkspaceStatus) => ["started", "canceled", "error"].includes(workspaceStatus)

const canDelete = (workspaceStatus: WorkspaceStatus) =>
["started", "stopped", "canceled", "error"].includes(workspaceStatus)

Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

Praise: Nice 😎

export interface WorkspaceActionsProps {
workspace: Workspace
handleStart: () => void
handleStop: () => void
handleDelete: () => void
handleUpdate: () => void
handleCancel: () => void
}
Expand All @@ -50,6 +57,7 @@ export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
workspace,
handleStart,
handleStop,
handleDelete,
handleUpdate,
handleCancel,
}) => {
Expand All @@ -74,6 +82,14 @@ export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
label={Language.stop}
/>
)}
{canDelete(workspaceStatus) && (
<WorkspaceActionButton
className={styles.actionButton}
icon={<DeleteIcon />}
onClick={handleDelete}
label={Language.delete}
/>
)}
{canCancelJobs(workspaceStatus) && (
<WorkspaceActionButton
className={styles.actionButton}
Expand Down
12 changes: 11 additions & 1 deletion site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fireEvent, screen, waitFor } from "@testing-library/react"
import { fireEvent, screen, waitFor, within } from "@testing-library/react"
import { rest } from "msw"
import React from "react"
import * as api from "../../api/api"
Expand Down Expand Up @@ -76,6 +76,16 @@ describe("Workspace Page", () => {
const stopWorkspaceMock = jest.spyOn(api, "stopWorkspace").mockResolvedValueOnce(MockWorkspaceBuild)
await testButton(Language.stop, stopWorkspaceMock)
})
it("requests a delete job when the user presses Delete and confirms", async () => {
const deleteWorkspaceMock = jest.spyOn(api, "deleteWorkspace").mockResolvedValueOnce(MockWorkspaceBuild)
await renderWorkspacePage()
const button = await screen.findByText(Language.delete)
await waitFor(() => fireEvent.click(button))
const confirmDialog = await screen.findByRole("dialog")
const confirmButton = within(confirmDialog).getByText("Delete")
await waitFor(() => fireEvent.click(confirmButton))
expect(deleteWorkspaceMock).toBeCalled()
})
it("requests a start job when the user presses Start", async () => {
server.use(
rest.get(`/api/v2/workspaces/${MockWorkspace.id}`, (req, res, ctx) => {
Expand Down
35 changes: 24 additions & 11 deletions site/src/pages/WorkspacePage/WorkspacePage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMachine } from "@xstate/react"
import React, { useEffect } from "react"
import { useParams } from "react-router-dom"
import { useNavigate, useParams } from "react-router-dom"
import { DeleteWorkspaceDialog } from "../../components/DeleteWorkspaceDialog/DeleteWorkspaceDialog"
import { ErrorSummary } from "../../components/ErrorSummary/ErrorSummary"
import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
import { Margins } from "../../components/Margins/Margins"
Expand All @@ -11,6 +12,7 @@ import { workspaceMachine } from "../../xServices/workspace/workspaceXService"

export const WorkspacePage: React.FC = () => {
const { workspace: workspaceQueryParam } = useParams()
const navigate = useNavigate()
const workspaceId = firstOrItem(workspaceQueryParam, null)

const [workspaceState, workspaceSend] = useMachine(workspaceMachine)
Expand All @@ -32,16 +34,27 @@ export const WorkspacePage: React.FC = () => {
return (
<Margins>
<Stack spacing={4}>
<Workspace
workspace={workspace}
handleStart={() => workspaceSend("START")}
handleStop={() => workspaceSend("STOP")}
handleUpdate={() => workspaceSend("UPDATE")}
handleCancel={() => workspaceSend("CANCEL")}
resources={resources}
getResourcesError={getResourcesError instanceof Error ? getResourcesError : undefined}
builds={builds}
/>
<>
<Workspace
workspace={workspace}
handleStart={() => workspaceSend("START")}
handleStop={() => workspaceSend("STOP")}
handleDelete={() => workspaceSend("ASK_DELETE")}
handleUpdate={() => workspaceSend("UPDATE")}
handleCancel={() => workspaceSend("CANCEL")}
resources={resources}
getResourcesError={getResourcesError instanceof Error ? getResourcesError : undefined}
builds={builds}
/>
<DeleteWorkspaceDialog
isOpen={workspaceState.matches({ ready: { build: "askingDelete" } })}
handleCancel={() => workspaceSend("CANCEL_DELETE")}
handleConfirm={() => {
workspaceSend("DELETE")
navigate("/workspaces")
}}
Comment on lines +52 to +55
Copy link
Contributor

@greyscaled greyscaled May 27, 2022

Choose a reason for hiding this comment

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

Question:

What happens if we send "DELETE" but the API request fails? It seems we would navigate away.

I ran into this problem in #1701 and used a submitSuccess state to indicate it was OK to navigate.

WDYT here?


Edit: LoC for reference

onSubmit={(values) => {
scheduleSend({
type: "SUBMIT_SCHEDULE",
autoStart: formValuesToAutoStartRequest(values),
ttl: formValuesToTTLRequest(values),
})
}}
/>
)
} else if (scheduleState.matches("submitSuccess")) {
navigate(`/workspaces/${workspaceId}`)
return <FullScreenLoader />
} else {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. I opened a decision ticket on this and we decided to stick with what I have for now, but change it in the future. #1837

/>
</>
</Stack>
</Margins>
)
Expand Down
32 changes: 32 additions & 0 deletions site/src/xServices/workspace/workspaceXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type WorkspaceEvent =
| { type: "GET_WORKSPACE"; workspaceId: string }
| { type: "START" }
| { type: "STOP" }
| { type: "ASK_DELETE" }
| { type: "DELETE" }
| { type: "CANCEL_DELETE" }
| { type: "UPDATE" }
| { type: "CANCEL" }
| { type: "LOAD_MORE_BUILDS" }
Expand Down Expand Up @@ -136,10 +139,17 @@ export const workspaceMachine = createMachine(
on: {
START: "requestingStart",
STOP: "requestingStop",
ASK_DELETE: "askingDelete",
UPDATE: "refreshingTemplate",
CANCEL: "requestingCancel",
},
},
askingDelete: {
on: {
DELETE: "requestingDelete",
CANCEL_DELETE: "idle",
},
},
requestingStart: {
entry: "clearBuildError",
invoke: {
Expand Down Expand Up @@ -170,6 +180,21 @@ export const workspaceMachine = createMachine(
},
},
},
requestingDelete: {
entry: "clearBuildError",
invoke: {
id: "deleteWorkspace",
src: "deleteWorkspace",
onDone: {
target: "idle",
actions: ["assignBuild", "refreshTimeline"],
},
onError: {
target: "idle",
actions: ["assignBuildError", "displayBuildError"],
Copy link
Member

Choose a reason for hiding this comment

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

Do these show an error toast or something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, and now that you mention it I should test for it. The component is called Snackbar. Oh but I think redirecting will stop it from happening :/

},
},
},
requestingCancel: {
entry: "clearCancellationMessage",
invoke: {
Expand Down Expand Up @@ -428,6 +453,13 @@ export const workspaceMachine = createMachine(
throw Error("Cannot stop workspace without workspace id")
}
},
deleteWorkspace: async (context) => {
if (context.workspace) {
return await API.deleteWorkspace(context.workspace.id)
} else {
throw Error("Cannot delete workspace without workspace id")
}
},
cancelWorkspace: async (context) => {
if (context.workspace) {
return await API.cancelWorkspaceBuild(context.workspace.latest_build.id)
Expand Down