Skip to content

chore: refactor dialogs #3935

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
Sep 7, 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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fireEvent, render } from "@testing-library/react"
import { FC } from "react"
import { WrapperComponent } from "../../testHelpers/renderHelpers"
import { WrapperComponent } from "../../../testHelpers/renderHelpers"
import { ConfirmDialog, ConfirmDialogProps } from "./ConfirmDialog"

namespace Helpers {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import DialogActions from "@material-ui/core/DialogActions"
import { fade, makeStyles } from "@material-ui/core/styles"
import Typography from "@material-ui/core/Typography"
import React, { ReactNode } from "react"
import { Dialog, DialogActionButtons, DialogActionButtonsProps } from "../Dialog/Dialog"
import { ConfirmDialogType } from "../Dialog/types"
import { Dialog, DialogActionButtons, DialogActionButtonsProps } from "../Dialog"
import { ConfirmDialogType } from "../types"

interface ConfirmDialogTypeConfig {
confirmText: ReactNode
Expand Down
33 changes: 33 additions & 0 deletions site/src/components/Dialogs/DeleteDialog/DeleteDialog.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ComponentMeta, Story } from "@storybook/react"
import { DeleteDialog, DeleteDialogProps } from "./DeleteDialog"

export default {
title: "Components/Dialogs/DeleteDialog",
component: DeleteDialog,
argTypes: {
onCancel: {
action: "onClose",
},
onConfirm: {
action: "onConfirm",
},
open: {
control: "boolean",
defaultValue: true,
},
title: {
defaultValue: "Delete Something",
},
description: {
defaultValue:
"This is irreversible. To confirm, type the name of the thing you want to delete.",
},
},
} as ComponentMeta<typeof DeleteDialog>

const Template: Story<DeleteDialogProps> = (args) => <DeleteDialog {...args} />

export const Example = Template.bind({})
Example.args = {
isOpen: true,
}
31 changes: 31 additions & 0 deletions site/src/components/Dialogs/DeleteDialog/DeleteDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { ReactNode } from "react"
import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog"

export interface DeleteDialogProps {
isOpen: boolean
onConfirm: () => void
onCancel: () => void
title: string
description: string | ReactNode
confirmLoading?: boolean
}

export const DeleteDialog: React.FC<React.PropsWithChildren<DeleteDialogProps>> = ({
isOpen,
onCancel,
onConfirm,
title,
description,
confirmLoading,
}) => (
<ConfirmDialog
type="delete"
hideCancel={false}
open={isOpen}
title={title}
onConfirm={onConfirm}
onClose={onCancel}
description={description}
confirmLoading={confirmLoading}
/>
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Story } from "@storybook/react"
import { MockUser } from "../../testHelpers/renderHelpers"
import { MockUser } from "../../../testHelpers/renderHelpers"
import { ResetPasswordDialog, ResetPasswordDialogProps } from "./ResetPasswordDialog"

export default {
title: "components/ResetPasswordDialog",
title: "components/Dialogs/ResetPasswordDialog",
component: ResetPasswordDialog,
argTypes: {
onClose: { action: "onClose" },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import DialogContentText from "@material-ui/core/DialogContentText"
import { makeStyles } from "@material-ui/core/styles"
import { FC } from "react"
import * as TypesGen from "../../api/typesGenerated"
import { CodeExample } from "../CodeExample/CodeExample"
import * as TypesGen from "../../../api/typesGenerated"
import { CodeExample } from "../../CodeExample/CodeExample"
import { ConfirmDialog } from "../ConfirmDialog/ConfirmDialog"

export interface ResetPasswordDialogProps {
Expand Down
3 changes: 1 addition & 2 deletions site/src/i18n/en/templatePage.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"deleteDialog": {
"title": "Delete template",
"message": "Are you sure you want to delete this template?",
"confirm": "Delete"
"description": "Deleting a template is irreversible. Are you sure you want to proceed?"
},
"deleteSuccess": "Template successfully deleted."
}
4 changes: 4 additions & 0 deletions site/src/i18n/en/workspacePage.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"deleteDialog": {
"title": "Delete workspace",
"description": "Deleting a workspace is irreversible. Are you sure you want to proceed?"
},
"workspaceScheduleButton": {
"schedule": "Schedule",
"editDeadlineMinus": "Subtract one hour",
Expand Down
13 changes: 5 additions & 8 deletions site/src/pages/TemplatePage/TemplatePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMachine, useSelector } from "@xstate/react"
import { ConfirmDialog } from "components/ConfirmDialog/ConfirmDialog"
import { DeleteDialog } from "components/Dialogs/DeleteDialog/DeleteDialog"
import { FC, useContext } from "react"
import { Helmet } from "react-helmet-async"
import { useTranslation } from "react-i18next"
Expand Down Expand Up @@ -74,20 +74,17 @@ export const TemplatePage: FC<React.PropsWithChildren<unknown>> = () => {
deleteTemplateError={deleteTemplateError}
/>

<ConfirmDialog
type="delete"
hideCancel={false}
open={templateState.matches("confirmingDelete")}
<DeleteDialog
isOpen={templateState.matches("confirmingDelete")}
confirmLoading={templateState.matches("deleting")}
title={t("deleteDialog.title")}
confirmText={t("deleteDialog.confirm")}
description={t("deleteDialog.description")}
onConfirm={() => {
templateSend("CONFIRM_DELETE")
}}
onClose={() => {
onCancel={() => {
templateSend("CANCEL_DELETE")
}}
description={<>{t("deleteDialog.message")}</>}
/>
</>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useActor } from "@xstate/react"
import React, { useContext, useEffect } from "react"
import { ConfirmDialog } from "../../../components/ConfirmDialog/ConfirmDialog"
import { ConfirmDialog } from "../../../components/Dialogs/ConfirmDialog/ConfirmDialog"
import { Section } from "../../../components/Section/Section"
import { XServiceContext } from "../../../xServices/StateContext"
import { SSHKeysPageView } from "./SSHKeysPageView"
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/UsersPage/UsersPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { rest } from "msw"
import { Language as usersXServiceLanguage } from "xServices/users/usersXService"
import * as API from "../../api/api"
import { Role } from "../../api/typesGenerated"
import { Language as ResetPasswordDialogLanguage } from "../../components/Dialogs/ResetPasswordDialog/ResetPasswordDialog"
import { GlobalSnackbar } from "../../components/GlobalSnackbar/GlobalSnackbar"
import { Language as ResetPasswordDialogLanguage } from "../../components/ResetPasswordDialog/ResetPasswordDialog"
import { Language as RoleSelectLanguage } from "../../components/RoleSelect/RoleSelect"
import { Language as UsersTableBodyLanguage } from "../../components/UsersTable/UsersTableBody"
import {
Expand Down
4 changes: 2 additions & 2 deletions site/src/pages/UsersPage/UsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { FC, ReactNode, useContext, useEffect } from "react"
import { Helmet } from "react-helmet-async"
import { useNavigate } from "react-router"
import { useSearchParams } from "react-router-dom"
import { ConfirmDialog } from "../../components/ConfirmDialog/ConfirmDialog"
import { ResetPasswordDialog } from "../../components/ResetPasswordDialog/ResetPasswordDialog"
import { ConfirmDialog } from "../../components/Dialogs/ConfirmDialog/ConfirmDialog"
import { ResetPasswordDialog } from "../../components/Dialogs/ResetPasswordDialog/ResetPasswordDialog"
import { userFilterQuery } from "../../util/filters"
import { pageTitle } from "../../util/page"
import { XServiceContext } from "../../xServices/StateContext"
Expand Down
13 changes: 9 additions & 4 deletions site/src/pages/WorkspacePage/WorkspacePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import dayjs from "dayjs"
import minMax from "dayjs/plugin/minMax"
import { FC, useContext, useEffect } from "react"
import { Helmet } from "react-helmet-async"
import { useTranslation } from "react-i18next"
import { useParams } from "react-router-dom"
import { DeleteWorkspaceDialog } from "../../components/DeleteWorkspaceDialog/DeleteWorkspaceDialog"
import { DeleteDialog } from "../../components/Dialogs/DeleteDialog/DeleteDialog"
import { ErrorSummary } from "../../components/ErrorSummary/ErrorSummary"
import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
import { Workspace, WorkspaceErrors } from "../../components/Workspace/Workspace"
Expand All @@ -25,6 +26,8 @@ export const WorkspacePage: FC = () => {
const username = firstOrItem(usernameQueryParam, null)
const workspaceName = firstOrItem(workspaceQueryParam, null)

const { t } = useTranslation("workspacePage")

const xServices = useContext(XServiceContext)
const me = useSelector(xServices.authXService, selectUser)

Expand Down Expand Up @@ -136,10 +139,12 @@ export const WorkspacePage: FC = () => {
}}
buildInfo={buildInfoState.context.buildInfo}
/>
<DeleteWorkspaceDialog
<DeleteDialog
title={t("deleteDialog.title")}
description={t("deleteDialog.description")}
isOpen={workspaceState.matches({ ready: { build: "askingDelete" } })}
handleCancel={() => workspaceSend("CANCEL_DELETE")}
handleConfirm={() => {
onCancel={() => workspaceSend("CANCEL_DELETE")}
onConfirm={() => {
workspaceSend("DELETE")
}}
/>
Expand Down