Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Refactor DeleteSubscriptionAction (#65575)

Fraserb5495dc6aa24
diff --git a/frontend/src/metabase/common/components/DeleteModalWithConfirm/DeleteModalWithConfirm.jsx b/frontend/src/metabase/common/components/DeleteModalWithConfirm/DeleteModalWithConfirm.jsx
deleted file mode 100644
index 9f4bd73e197..00000000000
--- a/frontend/src/metabase/common/components/DeleteModalWithConfirm/DeleteModalWithConfirm.jsx
+++ /dev/null
@@ -1,98 +0,0 @@
-import cx from "classnames";
-import PropTypes from "prop-types";
-import { Component } from "react";
-import { t } from "ttag";
-import _ from "underscore";
-
-import CheckBox from "metabase/common/components/CheckBox";
-import ModalContent from "metabase/common/components/ModalContent";
-import ButtonsS from "metabase/css/components/buttons.module.css";
-import CS from "metabase/css/core/index.css";
-
-import { CheckboxLabel } from "./DeleteModalWithConfirm.styled";
-
-export default class DeleteModalWithConfirm extends Component {
- constructor(props, context) {
- super(props, context);
- this.state = {
- checked: {},
- };
-
- _.bindAll(this, "onDelete");
- }
-
- static propTypes = {
- title: PropTypes.string.isRequired,
- objectType: PropTypes.string.isRequired,
- confirmItems: PropTypes.array.isRequired,
- onClose: PropTypes.func.isRequired,
- onDelete: PropTypes.func.isRequired,
- buttonText: PropTypes.string,
- };
-
- async onDelete() {
- await this.props.onDelete();
- this.props.onClose();
- }
-
- render() {
- const { title, objectType, confirmItems, buttonText } = this.props;
- const { checked } = this.state;
- const confirmed = confirmItems.reduce(
- (acc, item, index) => acc && checked[index],
- true,
- );
- return (
- <ModalContent
- title={title}
- onClose={this.props.onClose}
- data-testid={`delete-confirmation-modal-${objectType}`}
- >
- <div>
- <ul>
- {confirmItems.map((item, index) => (
- <li
- key={index}
- className={cx(
- CS.pb2,
- CS.mb2,
- CS.borderRowDivider,
- CS.flex,
- CS.alignCenter,
- )}
- >
- <CheckBox
- label={<CheckboxLabel>{item}</CheckboxLabel>}
- size={20}
- checkedColor="danger"
- uncheckedColor="danger"
- checked={checked[index]}
- onChange={(e) =>
- this.setState({
- checked: { ...checked, [index]: e.target.checked },
- })
- }
- />
- </li>
- ))}
- </ul>
- </div>
- <div className={cx("Form-actions", CS.mlAuto)}>
- <button
- className={ButtonsS.Button}
- onClick={this.props.onClose}
- >{t`Cancel`}</button>
- <button
- className={cx(ButtonsS.Button, CS.ml2, {
- [ButtonsS.ButtonDanger]: confirmed,
- [CS.disabled]: !confirmed,
- })}
- onClick={this.onDelete}
- >
- {buttonText || t`Delete this ${objectType}`}
- </button>
- </div>
- </ModalContent>
- );
- }
-}
diff --git a/frontend/src/metabase/common/components/DeleteModalWithConfirm/DeleteModalWithConfirm.styled.tsx b/frontend/src/metabase/common/components/DeleteModalWithConfirm/DeleteModalWithConfirm.styled.tsx
deleted file mode 100644
index 48ac4919704..00000000000
--- a/frontend/src/metabase/common/components/DeleteModalWithConfirm/DeleteModalWithConfirm.styled.tsx
+++ /dev/null
@@ -1,9 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-import CheckBox from "metabase/common/components/CheckBox";
-
-export const CheckboxLabel = styled(CheckBox.Label)`
- color: var(--mb-color-danger);
- font-size: 1.12em;
-`;
diff --git a/frontend/src/metabase/common/components/DeleteModalWithConfirm/index.tsx b/frontend/src/metabase/common/components/DeleteModalWithConfirm/index.tsx
deleted file mode 100644
index 761cc32e8f4..00000000000
--- a/frontend/src/metabase/common/components/DeleteModalWithConfirm/index.tsx
+++ /dev/null
@@ -1,2 +0,0 @@
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export { default } from "./DeleteModalWithConfirm";
diff --git a/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx b/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx
index ad071166995..5785779d3a4 100644
--- a/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx
+++ b/frontend/src/metabase/notifications/AddEditSidebar/AddEditEmailSidebar.tsx
@@ -30,7 +30,7 @@ import type {
import { CaveatMessage } from "./CaveatMessage";
import DefaultParametersSection from "./DefaultParametersSection";
-import DeleteSubscriptionAction from "./DeleteSubscriptionAction";
+import { DeleteSubscriptionAction } from "./DeleteSubscriptionAction";
import Heading from "./Heading";
import { CHANNEL_NOUN_PLURAL } from "./constants";
diff --git a/frontend/src/metabase/notifications/AddEditSidebar/AddEditSlackSidebar.tsx b/frontend/src/metabase/notifications/AddEditSidebar/AddEditSlackSidebar.tsx
index 4e0042f73d4..95775ddbae0 100644
--- a/frontend/src/metabase/notifications/AddEditSidebar/AddEditSlackSidebar.tsx
+++ b/frontend/src/metabase/notifications/AddEditSidebar/AddEditSlackSidebar.tsx
@@ -25,7 +25,7 @@ import type {
import { CaveatMessage } from "./CaveatMessage";
import DefaultParametersSection from "./DefaultParametersSection";
-import DeleteSubscriptionAction from "./DeleteSubscriptionAction";
+import { DeleteSubscriptionAction } from "./DeleteSubscriptionAction";
import Heading from "./Heading";
import { CHANNEL_NOUN_PLURAL } from "./constants";
diff --git a/frontend/src/metabase/notifications/AddEditSidebar/DeleteSubscriptionAction.jsx b/frontend/src/metabase/notifications/AddEditSidebar/DeleteSubscriptionAction.jsx
deleted file mode 100644
index bb457983cd3..00000000000
--- a/frontend/src/metabase/notifications/AddEditSidebar/DeleteSubscriptionAction.jsx
+++ /dev/null
@@ -1,79 +0,0 @@
-import cx from "classnames";
-import PropTypes from "prop-types";
-import { jt, msgid, ngettext, t } from "ttag";
-
-import DeleteModalWithConfirm from "metabase/common/components/DeleteModalWithConfirm";
-import ModalWithTrigger from "metabase/common/components/ModalWithTrigger";
-import ButtonsS from "metabase/css/components/buttons.module.css";
-import CS from "metabase/css/core/index.css";
-
-function getConfirmItems(pulse) {
- return pulse.channels.map((c, index) =>
- c.channel_type === "email" ? (
- <span key={index}>
- {jt`This dashboard will no longer be emailed to ${(
- <strong key="msg">
- {((n) => ngettext(msgid`${n} address`, `${n} addresses`, n))(
- c.recipients.length,
- )}
- </strong>
- )} ${(<strong key="type">{c.schedule_type}</strong>)}`}
- .
- </span>
- ) : c.channel_type === "slack" ? (
- <span key={index}>
- {jt`Slack channel ${(
- <strong key="msg">{c.details && c.details.channel}</strong>
- )} will no longer get this dashboard ${(
- <strong key="type">{c.schedule_type}</strong>
- )}`}
- .
- </span>
- ) : (
- <span key={index}>
- {jt`Channel ${(
- <strong key="msg">{c.channel_type}</strong>
- )} will no longer receive this dashboard ${(
- <strong key="type">{c.schedule_type}</strong>
- )}`}
- .
- </span>
- ),
- );
-}
-
-function DeleteSubscriptionAction({ pulse, handleArchive }) {
- return pulse.id != null && !pulse.archived ? (
- <div className={cx(CS.borderTop, CS.pt1, CS.pb3, CS.flex, CS.justifyEnd)}>
- <ModalWithTrigger
- triggerClasses={cx(
- ButtonsS.Button,
- ButtonsS.ButtonBorderless,
- CS.textLight,
- CS.textErrorHover,
- CS.flexAlignRight,
- CS.flexNoShrink,
- )}
- triggerElement={t`Delete this subscription`}
- >
- {({ onClose }) => (
- <DeleteModalWithConfirm
- objectType="pulse"
- title={t`Delete this subscription to ${pulse.name}?`}
- buttonText={t`Delete`}
- confirmItems={getConfirmItems(pulse)}
- onClose={onClose}
- onDelete={handleArchive}
- />
- )}
- </ModalWithTrigger>
- </div>
- ) : null;
-}
-
-DeleteSubscriptionAction.propTypes = {
- pulse: PropTypes.object.isRequired,
- handleArchive: PropTypes.func.isRequired,
-};
-
-export default DeleteSubscriptionAction;
diff --git a/frontend/src/metabase/notifications/AddEditSidebar/DeleteSubscriptionAction.tsx b/frontend/src/metabase/notifications/AddEditSidebar/DeleteSubscriptionAction.tsx
new file mode 100644
index 00000000000..f7e9d66add4
--- /dev/null
+++ b/frontend/src/metabase/notifications/AddEditSidebar/DeleteSubscriptionAction.tsx
@@ -0,0 +1,135 @@
+import { useDisclosure } from "@mantine/hooks";
+import cx from "classnames";
+import type { ReactNode } from "react";
+import { useState } from "react";
+import { jt, msgid, ngettext, t } from "ttag";
+
+import CS from "metabase/css/core/index.css";
+import { Button, Checkbox, Flex, Modal } from "metabase/ui";
+import type { Channel, Pulse } from "metabase-types/api";
+
+interface PulseWithMetadata extends Pulse {
+ id?: number | null;
+ archived?: boolean;
+ name?: string;
+}
+
+function getConfirmItems(pulse: PulseWithMetadata): ReactNode[] {
+ return pulse.channels.map((c: Channel, index: number) =>
+ c.channel_type === "email" ? (
+ <span key={index}>
+ {jt`This dashboard will no longer be emailed to ${(
+ <strong key="msg">
+ {((n) => ngettext(msgid`${n} address`, `${n} addresses`, n))(
+ c.recipients?.length || 0,
+ )}
+ </strong>
+ )} ${(<strong key="type">{c.schedule_type}</strong>)}`}
+ .
+ </span>
+ ) : c.channel_type === "slack" ? (
+ <span key={index}>
+ {jt`Slack channel ${(
+ <strong key="msg">{c.details && c.details.channel}</strong>
+ )} will no longer get this dashboard ${(
+ <strong key="type">{c.schedule_type}</strong>
+ )}`}
+ .
+ </span>
+ ) : (
+ <span key={index}>
+ {jt`Channel ${(
+ <strong key="msg">{c.channel_type}</strong>
+ )} will no longer receive this dashboard ${(
+ <strong key="type">{c.schedule_type}</strong>
+ )}`}
+ .
+ </span>
+ ),
+ );
+}
+
+interface DeleteSubscriptionActionProps {
+ pulse: PulseWithMetadata;
+ handleArchive: () => void;
+}
+
+export function DeleteSubscriptionAction({
+ pulse,
+ handleArchive,
+}: DeleteSubscriptionActionProps) {
+ const [modalOpened, { open: openModal, close: closeModal }] =
+ useDisclosure(false);
+ const [checked, setChecked] = useState<Record<number, boolean>>({});
+
+ const confirmItems = getConfirmItems(pulse);
+ const confirmed = confirmItems.reduce(
+ (acc, _item, index) => acc && checked[index],
+ true,
+ );
+
+ const handleDelete = async () => {
+ await handleArchive();
+ closeModal();
+ };
+
+ if (!pulse.id || pulse.archived) {
+ return null;
+ }
+
+ return (
+ <>
+ <Flex className={cx(CS.borderTop, CS.pt1, CS.pb3)} justify="flex-end">
+ <Button variant="subtle" c="text-light" onClick={openModal}>
+ {t`Delete this subscription`}
+ </Button>
+ </Flex>
+ <Modal
+ opened={modalOpened}
+ onClose={closeModal}
+ title={t`Delete this subscription to ${pulse.name}?`}
+ size="lg"
+ data-testid="delete-confirmation-modal-pulse"
+ >
+ <Flex direction="column" gap="md" mt="md">
+ <ul>
+ {confirmItems.map((item, index) => (
+ <li
+ key={index}
+ className={cx(
+ CS.pb2,
+ CS.mb2,
+ CS.borderRowDivider,
+ CS.flex,
+ CS.alignCenter,
+ )}
+ >
+ <Checkbox
+ label={item}
+ checked={checked[index]}
+ onChange={(e) =>
+ setChecked({
+ ...checked,
+ [index]: e.currentTarget.checked,
+ })
+ }
+ />
+ </li>
+ ))}
+ </ul>
+ <Flex justify="flex-end" gap="sm">
+ <Button onClick={closeModal}>{t`Cancel`}</Button>
+ <Button
+ color={confirmed ? "danger" : undefined}
+ variant="filled"
+ onClick={handleDelete}
+ disabled={!confirmed}
+ >
+ {t`Delete`}
+ </Button>
+ </Flex>
+ </Flex>
+ </Modal>
+ </>
+ );
+}