Skip to content

chore: refactor frontend to use workspace status directly #4361

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 11 commits into from
Oct 5, 2022
33 changes: 12 additions & 21 deletions site/src/components/DropdownButton/ActionCtas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,63 @@ import DeleteOutlineIcon from "@material-ui/icons/DeleteOutline"
import PlayCircleOutlineIcon from "@material-ui/icons/PlayCircleOutline"
import { LoadingButton } from "components/LoadingButton/LoadingButton"
import { FC } from "react"
import { useTranslation } from "react-i18next"
import { combineClasses } from "util/combineClasses"
import { WorkspaceStateEnum } from "util/workspace"
import { WorkspaceActionButton } from "../WorkspaceActionButton/WorkspaceActionButton"

export const Language = {
start: "Start",
stop: "Stop",
delete: "Delete",
cancel: "Cancel",
update: "Update",
updating: "Updating",
// these labels are used in WorkspaceActions.tsx
starting: "Starting...",
stopping: "Stopping...",
deleting: "Deleting...",
}

interface WorkspaceAction {
handleAction: () => void
}

export const UpdateButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({ handleAction }) => {
const styles = useStyles()
const { t } = useTranslation("workspacePage")

return (
<Button className={styles.actionButton} startIcon={<CloudQueueIcon />} onClick={handleAction}>
{Language.update}
{t("actionButton.update")}
</Button>
)
}

export const StartButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({ handleAction }) => {
const styles = useStyles()
const { t } = useTranslation("workspacePage")

return (
<WorkspaceActionButton
className={styles.actionButton}
icon={<PlayCircleOutlineIcon />}
onClick={handleAction}
label={Language.start}
label={t("actionButton.start")}
/>
)
}

export const StopButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({ handleAction }) => {
const styles = useStyles()
const { t } = useTranslation("workspacePage")

return (
<WorkspaceActionButton
className={styles.actionButton}
icon={<CropSquareIcon />}
onClick={handleAction}
label={Language.stop}
label={t("actionButton.stop")}
/>
)
}

export const DeleteButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({ handleAction }) => {
const styles = useStyles()
const { t } = useTranslation("workspacePage")

return (
<WorkspaceActionButton
className={styles.actionButton}
icon={<DeleteOutlineIcon />}
onClick={handleAction}
label={Language.delete}
label={t("actionButton.delete")}
/>
)
}
Expand All @@ -92,15 +83,15 @@ export const CancelButton: FC<React.PropsWithChildren<WorkspaceAction>> = ({ han
}

interface DisabledProps {
workspaceState: WorkspaceStateEnum
label: string
}

export const DisabledButton: FC<React.PropsWithChildren<DisabledProps>> = ({ workspaceState }) => {
export const DisabledButton: FC<React.PropsWithChildren<DisabledProps>> = ({ label }) => {
const styles = useStyles()

return (
<Button disabled className={styles.actionButton}>
{workspaceState}
{label}
</Button>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { action } from "@storybook/addon-actions"
import { Story } from "@storybook/react"
import { WorkspaceStateEnum } from "util/workspace"
import { DeleteButton, DisabledButton, StartButton, UpdateButton } from "./ActionCtas"
import { DropdownButton, DropdownButtonProps } from "./DropdownButton"

Expand All @@ -23,7 +22,7 @@ WithDropdown.args = {

export const WithCancel = Template.bind({})
WithCancel.args = {
primaryAction: <DisabledButton workspaceState={WorkspaceStateEnum.deleting} />,
primaryAction: <DisabledButton workspaceStatus="deleting" />,
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious why we're no longer using the enum here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The WorkspaceStatus type is defined by the backend now

secondaryActions: [],
canCancel: true,
handleCancel: action("cancel"),
Expand Down
46 changes: 18 additions & 28 deletions site/src/components/Workspace/Workspace.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default {

const Template: Story<WorkspaceProps> = (args) => <Workspace {...args} />

export const Started = Template.bind({})
Started.args = {
export const Running = Template.bind({})
Running.args = {
bannerProps: {
isLoading: false,
onExtend: action("extend"),
Expand Down Expand Up @@ -53,42 +53,32 @@ Started.args = {

export const WithoutUpdateAccess = Template.bind({})
WithoutUpdateAccess.args = {
...Started.args,
...Running.args,
canUpdateWorkspace: false,
}

export const Starting = Template.bind({})
Starting.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockStartingWorkspace,
}

export const Stopped = Template.bind({})
Stopped.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockStoppedWorkspace,
}

export const Stopping = Template.bind({})
Stopping.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockStoppingWorkspace,
}

export const Error = Template.bind({})
Error.args = {
...Started.args,
workspace: {
...Mocks.MockFailedWorkspace,
latest_build: {
...Mocks.MockWorkspaceBuild,
job: {
...Mocks.MockProvisionerJob,
status: "failed",
},
transition: "start",
},
},
export const Failed = Template.bind({})
Failed.args = {
...Running.args,
workspace: Mocks.MockFailedWorkspace,
workspaceErrors: {
[WorkspaceErrors.BUILD_ERROR]: Mocks.makeMockApiError({
message: "A workspace build is already active.",
Expand All @@ -98,37 +88,37 @@ Error.args = {

export const Deleting = Template.bind({})
Deleting.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockDeletingWorkspace,
}

export const Deleted = Template.bind({})
Deleted.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockDeletedWorkspace,
}

export const Canceling = Template.bind({})
Canceling.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockCancelingWorkspace,
}

export const Canceled = Template.bind({})
Canceled.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockCanceledWorkspace,
}

export const Outdated = Template.bind({})
Outdated.args = {
...Started.args,
...Running.args,
workspace: Mocks.MockOutdatedWorkspace,
}

export const GetBuildsError = Template.bind({})
GetBuildsError.args = {
...Started.args,
...Running.args,
workspaceErrors: {
[WorkspaceErrors.GET_BUILDS_ERROR]: Mocks.makeMockApiError({
message: "There is a problem fetching builds.",
Expand All @@ -138,7 +128,7 @@ GetBuildsError.args = {

export const GetResourcesError = Template.bind({})
GetResourcesError.args = {
...Started.args,
...Running.args,
workspaceErrors: {
[WorkspaceErrors.GET_RESOURCES_ERROR]: Mocks.makeMockApiError({
message: "There is a problem fetching workspace resources.",
Expand All @@ -148,7 +138,7 @@ GetResourcesError.args = {

export const CancellationError = Template.bind({})
CancellationError.args = {
...Error.args,
...Failed.args,
workspaceErrors: {
[WorkspaceErrors.CANCELLATION_ERROR]: Mocks.makeMockApiError({
message: "Job could not be canceled.",
Expand Down
65 changes: 48 additions & 17 deletions site/src/components/WorkspaceActions/WorkspaceActions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { fireEvent, screen } from "@testing-library/react"
import { WorkspaceStateEnum } from "util/workspace"
import i18next from "i18next"
import * as Mocks from "../../testHelpers/entities"
import { render } from "../../testHelpers/renderHelpers"
import { Language } from "../DropdownButton/ActionCtas"
import { WorkspaceActions, WorkspaceActionsProps } from "./WorkspaceActions"

const { t } = i18next

const renderComponent = async (props: Partial<WorkspaceActionsProps> = {}) => {
render(
<WorkspaceActions
Expand Down Expand Up @@ -39,7 +40,9 @@ describe("WorkspaceActions", () => {
describe("when the workspace is starting", () => {
it("primary is starting; cancel is available; no secondary", async () => {
await renderComponent({ workspace: Mocks.MockStartingWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.starting)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.starting", { ns: "workspacePage" }),
)
expect(
screen.getByRole("button", {
name: "cancel action",
Expand All @@ -51,14 +54,20 @@ describe("WorkspaceActions", () => {
describe("when the workspace is started", () => {
it("primary is stop; secondary is delete", async () => {
await renderAndClick({ workspace: Mocks.MockWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.stop)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(Language.delete)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.stop", { ns: "workspacePage" }),
)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(
t("actionButton.delete", { ns: "workspacePage" }),
)
})
})
describe("when the workspace is stopping", () => {
it("primary is stopping; cancel is available; no secondary", async () => {
await renderComponent({ workspace: Mocks.MockStoppingWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.stopping)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.stopping", { ns: "workspacePage" }),
)
expect(
screen.getByRole("button", {
name: "cancel action",
Expand All @@ -70,29 +79,43 @@ describe("WorkspaceActions", () => {
describe("when the workspace is canceling", () => {
it("primary is canceling; no secondary", async () => {
await renderAndClick({ workspace: Mocks.MockCancelingWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(WorkspaceStateEnum.canceling)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("disabledButton.canceling", { ns: "workspacePage" }),
)
expect(screen.queryByTestId("secondary-ctas")).toBeNull()
})
})
describe("when the workspace is canceled", () => {
it("primary is start; secondary are stop, delete", async () => {
await renderAndClick({ workspace: Mocks.MockCanceledWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.start)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(Language.stop)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(Language.delete)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.start", { ns: "workspacePage" }),
)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(
t("actionButton.stop", { ns: "workspacePage" }),
)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(
t("actionButton.delete", { ns: "workspacePage" }),
)
})
})
describe("when the workspace is errored", () => {
it("primary is start; secondary is delete", async () => {
await renderAndClick({ workspace: Mocks.MockFailedWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.start)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(Language.delete)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.start", { ns: "workspacePage" }),
)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(
t("actionButton.delete", { ns: "workspacePage" }),
)
})
})
describe("when the workspace is deleting", () => {
it("primary is deleting; cancel is available; no secondary", async () => {
await renderComponent({ workspace: Mocks.MockDeletingWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.deleting)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.deleting", { ns: "workspacePage" }),
)
expect(
screen.getByRole("button", {
name: "cancel action",
Expand All @@ -104,16 +127,24 @@ describe("WorkspaceActions", () => {
describe("when the workspace is deleted", () => {
it("primary is deleted; no secondary", async () => {
await renderAndClick({ workspace: Mocks.MockDeletedWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(WorkspaceStateEnum.deleted)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("disabledButton.deleted", { ns: "workspacePage" }),
)
expect(screen.queryByTestId("secondary-ctas")).toBeNull()
})
})
describe("when the workspace is outdated", () => {
it("primary is update; secondary are start, delete", async () => {
await renderAndClick({ workspace: Mocks.MockOutdatedWorkspace })
expect(screen.getByTestId("primary-cta")).toHaveTextContent(Language.update)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(Language.start)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(Language.delete)
expect(screen.getByTestId("primary-cta")).toHaveTextContent(
t("actionButton.update", { ns: "workspacePage" }),
)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(
t("actionButton.start", { ns: "workspacePage" }),
)
expect(screen.getByTestId("secondary-ctas")).toHaveTextContent(
t("actionButton.delete", { ns: "workspacePage" }),
)
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for doing all these translations!!!

})
})
})
Loading