Skip to content

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

Migrate React components from dispatch(addUndo) to useToast hook (#66778)

Copilotb24727409c40
diff --git a/enterprise/frontend/src/metabase-enterprise/audit_app/containers/UnsubscribeUserModal/UnsubscribeUserModal.tsx b/enterprise/frontend/src/metabase-enterprise/audit_app/containers/UnsubscribeUserModal/UnsubscribeUserModal.tsx
index 8a92d16ebae..ef81bcce533 100644
--- a/enterprise/frontend/src/metabase-enterprise/audit_app/containers/UnsubscribeUserModal/UnsubscribeUserModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/audit_app/containers/UnsubscribeUserModal/UnsubscribeUserModal.tsx
@@ -4,9 +4,8 @@ import { t } from "ttag";
import { useGetUserQuery } from "metabase/api";
import { ConfirmModal } from "metabase/common/components/ConfirmModal";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
+import { useToast } from "metabase/common/hooks/use-toast";
import { getResponseErrorMessage } from "metabase/lib/errors";
-import { useDispatch } from "metabase/lib/redux";
-import { addUndo } from "metabase/redux/undo";
import { Stack, Text } from "metabase/ui";
import { AuditApi } from "metabase-enterprise/services";
import type { User } from "metabase-types/api";
@@ -23,7 +22,7 @@ export const UnsubscribeUserModal = ({
const userId = parseInt(params.userId, 10);
const { data: user, isLoading, error } = useGetUserQuery(userId);
- const dispatch = useDispatch();
+ const [sendToast] = useToast();
const [errorMessage, setErrorMessage] = useState<string>();
const baseModalProps = {
@@ -37,7 +36,7 @@ export const UnsubscribeUserModal = ({
const handleConfirmClick = async (user: User) => {
try {
await AuditApi.unsubscribe_user({ id: user.id });
- dispatch(addUndo({ message: t`Unsubscribe successful` }));
+ sendToast({ message: t`Unsubscribe successful` });
onClose();
} catch (error) {
const msg = getResponseErrorMessage(error);
diff --git a/enterprise/frontend/src/metabase-enterprise/database_routing/DatabaseRoutingSection/DatabaseRoutingSection.tsx b/enterprise/frontend/src/metabase-enterprise/database_routing/DatabaseRoutingSection/DatabaseRoutingSection.tsx
index 62b7891bca1..0dd4a14e3ac 100644
--- a/enterprise/frontend/src/metabase-enterprise/database_routing/DatabaseRoutingSection/DatabaseRoutingSection.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/database_routing/DatabaseRoutingSection/DatabaseRoutingSection.tsx
@@ -14,8 +14,8 @@ import { hasDbRoutingEnabled } from "metabase/admin/databases/utils";
import { skipToken, useListUserAttributesQuery } from "metabase/api";
import { getErrorMessage } from "metabase/api/utils";
import { useSetting } from "metabase/common/hooks";
-import { useDispatch, useSelector } from "metabase/lib/redux";
-import { addUndo } from "metabase/redux/undo";
+import { useToast } from "metabase/common/hooks/use-toast";
+import { useSelector } from "metabase/lib/redux";
import { getUserIsAdmin } from "metabase/selectors/user";
import {
Box,
@@ -44,7 +44,7 @@ export const DatabaseRoutingSection = ({
}: {
database: Database;
}) => {
- const dispatch = useDispatch();
+ const [sendToast] = useToast();
const engines = useSetting("engines");
@@ -92,9 +92,9 @@ export const DatabaseRoutingSection = ({
await updateRouterDatabase({ id: database.id, user_attribute: attribute });
if (!hasDbRoutingEnabled(database)) {
- dispatch(addUndo({ message: t`Database routing enabled` }));
+ sendToast({ message: t`Database routing enabled` });
} else {
- dispatch(addUndo({ message: t`Database routing updated` }));
+ sendToast({ message: t`Database routing updated` });
}
};
@@ -105,7 +105,7 @@ export const DatabaseRoutingSection = ({
await updateRouterDatabase({ id: database.id, user_attribute: null });
if (hasDbRoutingEnabled(database)) {
- dispatch(addUndo({ message: t`Database routing disabled` }));
+ sendToast({ message: t`Database routing disabled` });
}
}
};
diff --git a/frontend/src/metabase/account/notifications/containers/ArchiveAlertModal/DeleteAlertModal.tsx b/frontend/src/metabase/account/notifications/containers/ArchiveAlertModal/DeleteAlertModal.tsx
index 5a05bebe4da..ed8a6288950 100644
--- a/frontend/src/metabase/account/notifications/containers/ArchiveAlertModal/DeleteAlertModal.tsx
+++ b/frontend/src/metabase/account/notifications/containers/ArchiveAlertModal/DeleteAlertModal.tsx
@@ -7,9 +7,8 @@ import {
useUpdateNotificationMutation,
} from "metabase/api";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
-import { useDispatch } from "metabase/lib/redux";
+import { useToast } from "metabase/common/hooks/use-toast";
import { DeleteAlertConfirmModal } from "metabase/notifications/modals/DeleteAlertConfirmModal";
-import { addUndo } from "metabase/redux/undo";
import type { Notification } from "metabase-types/api";
import { getAlertId } from "../../selectors";
@@ -29,7 +28,7 @@ export const DeleteAlertModal = ({
}: DeleteAlertModalProps) => {
const id = getAlertId(params?.alertId);
- const dispatch = useDispatch();
+ const [sendToast] = useToast();
const hasUnsubscribed = location.query?.unsubscribed;
@@ -47,17 +46,15 @@ export const DeleteAlertModal = ({
});
if (result.error) {
- dispatch(
- addUndo({
- icon: "warning",
- toastColor: "error",
- message: t`An error occurred`,
- }),
- );
+ sendToast({
+ icon: "warning",
+ toastColor: "error",
+ message: t`An error occurred`,
+ });
return;
}
- dispatch(addUndo({ message: t`The alert was successfully deleted.` }));
+ sendToast({ message: t`The alert was successfully deleted.` });
onClose();
};
diff --git a/frontend/src/metabase/account/notifications/containers/UnsubscribeAlertModal/UnsubscribeAlertModal.tsx b/frontend/src/metabase/account/notifications/containers/UnsubscribeAlertModal/UnsubscribeAlertModal.tsx
index 358700dd024..7dfe49334dc 100644
--- a/frontend/src/metabase/account/notifications/containers/UnsubscribeAlertModal/UnsubscribeAlertModal.tsx
+++ b/frontend/src/metabase/account/notifications/containers/UnsubscribeAlertModal/UnsubscribeAlertModal.tsx
@@ -7,9 +7,9 @@ import {
useUnsubscribeFromNotificationMutation,
} from "metabase/api";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
+import { useToast } from "metabase/common/hooks/use-toast";
import { useDispatch, useSelector } from "metabase/lib/redux";
import { UnsubscribeConfirmModal } from "metabase/notifications/modals/UnsubscribeConfirmModal";
-import { addUndo } from "metabase/redux/undo";
import { getUser } from "metabase/selectors/user";
import type { Notification, User } from "metabase-types/api";
@@ -30,6 +30,7 @@ export const UnsubscribeAlertModal = ({
const user = useSelector(getUser);
const dispatch = useDispatch();
+ const [sendToast] = useToast();
const {
data: notification,
@@ -42,17 +43,15 @@ export const UnsubscribeAlertModal = ({
const result = await unsubscribe(alert.id);
if (result.error) {
- dispatch(
- addUndo({
- icon: "warning",
- toastColor: "error",
- message: t`An error occurred`,
- }),
- );
+ sendToast({
+ icon: "warning",
+ toastColor: "error",
+ message: t`An error occurred`,
+ });
return;
}
- dispatch(addUndo({ message: t`Successfully unsubscribed.` }));
+ sendToast({ message: t`Successfully unsubscribed.` });
if (isCreator(alert, user)) {
onClose();
diff --git a/frontend/src/metabase/admin/people/components/GroupDetail.tsx b/frontend/src/metabase/admin/people/components/GroupDetail.tsx
index bd4c86a2a5b..ca2dd567338 100644
--- a/frontend/src/metabase/admin/people/components/GroupDetail.tsx
+++ b/frontend/src/metabase/admin/people/components/GroupDetail.tsx
@@ -9,6 +9,7 @@ import {
} from "metabase/api";
import { AdminPaneLayout } from "metabase/common/components/AdminPaneLayout";
import { useConfirmation } from "metabase/common/hooks/use-confirmation";
+import { useToast } from "metabase/common/hooks/use-toast";
import {
canEditMembership,
getGroupNameLocalized,
@@ -17,7 +18,6 @@ import {
} from "metabase/lib/groups";
import { useDispatch } from "metabase/lib/redux";
import { PLUGIN_GROUP_MANAGERS, PLUGIN_TENANTS } from "metabase/plugins";
-import { addUndo } from "metabase/redux/undo";
import { Box, Button, Text } from "metabase/ui";
import type { Group, Member, Membership, User } from "metabase-types/api";
@@ -36,6 +36,7 @@ export const GroupDetail = ({
currentUser,
}: GroupDetailProps) => {
const dispatch = useDispatch();
+ const [sendToast] = useToast();
const [createMembership] = useCreateMembershipMutation();
const [updateMembership] = useUpdateMembershipMutation();
@@ -82,7 +83,7 @@ export const GroupDetail = ({
} else {
const { error } = await updateMembership(membership);
if (error) {
- dispatch(addUndo({ message: t`Failed to update user` }));
+ sendToast({ message: t`Failed to update user` });
}
}
};
@@ -108,7 +109,7 @@ export const GroupDetail = ({
} else {
const { error } = await deleteMembership(membership);
if (error) {
- dispatch(addUndo({ message: t`Failed to remove user from group` }));
+ sendToast({ message: t`Failed to remove user from group` });
}
}
};
diff --git a/frontend/src/metabase/auth/components/ResetPassword/ResetPassword.tsx b/frontend/src/metabase/auth/components/ResetPassword/ResetPassword.tsx
index 7f9a1f8e026..cb1d6fd3552 100644
--- a/frontend/src/metabase/auth/components/ResetPassword/ResetPassword.tsx
+++ b/frontend/src/metabase/auth/components/ResetPassword/ResetPassword.tsx
@@ -6,8 +6,8 @@ import { t } from "ttag";
import { useGetPasswordResetTokenStatusQuery } from "metabase/api";
import Button from "metabase/common/components/Button";
import Link from "metabase/common/components/Link";
+import { useToast } from "metabase/common/hooks/use-toast";
import { useDispatch } from "metabase/lib/redux";
-import { addUndo } from "metabase/redux/undo";
import { resetPassword, validatePassword } from "../../actions";
import type { ResetPasswordData } from "../../types";
@@ -34,6 +34,7 @@ export const ResetPassword = ({
const redirectUrl = location?.query?.redirect;
const email = location?.query?.email;
const dispatch = useDispatch();
+ const [sendToast] = useToast();
const { data: status, isLoading } =
useGetPasswordResetTokenStatusQuery(token);
@@ -41,9 +42,9 @@ export const ResetPassword = ({
async ({ password }: ResetPasswordData) => {
await dispatch(resetPassword({ token, password })).unwrap();
dispatch(replace(redirectUrl || "/"));
- dispatch(addUndo({ message: t`You've updated your password.` }));
+ sendToast({ message: t`You've updated your password.` });
},
- [token, dispatch, redirectUrl],
+ [token, dispatch, redirectUrl, sendToast],
);
if (isLoading) {
diff --git a/frontend/src/metabase/collections/components/ActionMenu/ActionMenu.tsx b/frontend/src/metabase/collections/components/ActionMenu/ActionMenu.tsx
index 168f126ffd5..d88a4048e3f 100644
--- a/frontend/src/metabase/collections/components/ActionMenu/ActionMenu.tsx
+++ b/frontend/src/metabase/collections/components/ActionMenu/ActionMenu.tsx
@@ -22,11 +22,11 @@ import {
isPreviewEnabled,
} from "metabase/collections/utils";
import { ConfirmModal } from "metabase/common/components/ConfirmModal";
+import { useToast } from "metabase/common/hooks/use-toast";
import { bookmarks as BookmarkEntity } from "metabase/entities";
import { connect, useDispatch } from "metabase/lib/redux";
import { entityForObject } from "metabase/lib/schema";
import * as Urls from "metabase/lib/urls";
-import { addUndo } from "metabase/redux/undo";
import { getSetting } from "metabase/selectors/settings";
import type Database from "metabase-lib/v1/metadata/Database";
import type { Bookmark, Collection, CollectionItem } from "metabase-types/api";
@@ -85,6 +85,7 @@ function ActionMenu({
deleteBookmark,
}: ActionMenuProps & ActionMenuStateProps) {
const dispatch = useDispatch();
+ const [sendToast] = useToast();
const [modalOpened, { open: openModal, close: closeModal }] = useDisclosure();
const isBookmarked = bookmarks && getIsBookmarked(item, bookmarks);
const canBookmark = canBookmarkItem(item);
@@ -146,21 +147,18 @@ function ActionMenu({
);
const redirect = getParentEntityLink(entity, parentCollection);
- dispatch(
- addUndo({
- message: t`${item.name} has been restored.`,
- actionLabel: t`View`, // could be collection or dashboard
- action: () => dispatch(push(redirect)),
- undo: false,
- }),
- );
- }, [item, dispatch]);
+ sendToast({
+ message: t`${item.name} has been restored.`,
+ actionLabel: t`View`, // could be collection or dashboard
+ action: () => dispatch(push(redirect)),
+ });
+ }, [item, dispatch, sendToast]);
const handleDeletePermanently = useCallback(() => {
const Entity = entityForObject(item);
dispatch(Entity.actions.delete(item));
- dispatch(addUndo({ message: t`This item has been permanently deleted.` }));
- }, [item, dispatch]);
+ sendToast({ message: t`This item has been permanently deleted.` });
+ }, [item, dispatch, sendToast]);
return (
<>
diff --git a/frontend/src/metabase/notifications/modals/QuestionAlertListModal/QuestionAlertListModal.tsx b/frontend/src/metabase/notifications/modals/QuestionAlertListModal/QuestionAlertListModal.tsx
index 164296ce2cc..54af3cf4738 100644
--- a/frontend/src/metabase/notifications/modals/QuestionAlertListModal/QuestionAlertListModal.tsx
+++ b/frontend/src/metabase/notifications/modals/QuestionAlertListModal/QuestionAlertListModal.tsx
@@ -7,10 +7,9 @@ import {
useUnsubscribeFromNotificationMutation,
useUpdateNotificationMutation,
} from "metabase/api";
-import { useDispatch } from "metabase/lib/redux";
+import { useToast } from "metabase/common/hooks/use-toast";
import { DeleteAlertConfirmModal } from "metabase/notifications/modals/DeleteAlertConfirmModal";
import { UnsubscribeConfirmModal } from "metabase/notifications/modals/UnsubscribeConfirmModal";
-import { addUndo } from "metabase/redux/undo";
import type Question from "metabase-lib/v1/Question";
import type { Notification } from "metabase-types/api";
@@ -34,7 +33,7 @@ export const QuestionAlertListModal = ({
}) => {
const [editingItem, setEditingItem] = useState<Notification | null>(null);
- const dispatch = useDispatch();
+ const [sendToast] = useToast();
const { data: questionNotifications } = useListNotificationsQuery({
card_id: question.id(),
@@ -67,17 +66,15 @@ export const QuestionAlertListModal = ({
});
if (result.error) {
- dispatch(
- addUndo({
- icon: "warning",
- toastColor: "error",
- message: t`An error occurred`,
- }),
- );
+ sendToast({
+ icon: "warning",
+ toastColor: "error",
+ message: t`An error occurred`,
+ });
return;
}
- dispatch(addUndo({ message: t`The alert was successfully deleted.` }));
+ sendToast({ message: t`The alert was successfully deleted.` });
const alertCount = questionNotifications?.length || 0;
// if we have just unsubscribed from the last alert, close the popover
@@ -92,17 +89,15 @@ export const QuestionAlertListModal = ({
const result = await unsubscribe(alert.id);
if (result.error) {
- dispatch(
- addUndo({
- icon: "warning",
- toastColor: "error",
- message: t`An error occurred`,
- }),
- );
+ sendToast({
+ icon: "warning",
+ toastColor: "error",
+ message: t`An error occurred`,
+ });
return;
}
- dispatch(addUndo({ message: t`Successfully unsubscribed.` }));
+ sendToast({ message: t`Successfully unsubscribed.` });
const alertCount = questionNotifications?.length || 0;
// if we have just unsubscribed from the last alert, close the popover
diff --git a/frontend/src/metabase/setup/components/LicenseTokenStep/LicenseTokenStep.tsx b/frontend/src/metabase/setup/components/LicenseTokenStep/LicenseTokenStep.tsx
index f8062cf77ac..ce664ff43a7 100644
--- a/frontend/src/metabase/setup/components/LicenseTokenStep/LicenseTokenStep.tsx
+++ b/frontend/src/metabase/setup/components/LicenseTokenStep/LicenseTokenStep.tsx
@@ -1,7 +1,7 @@
import { t } from "ttag";
+import { useToast } from "metabase/common/hooks/use-toast";
import { useDispatch, useSelector } from "metabase/lib/redux";
-import { addUndo } from "metabase/redux/undo";
import { useStep } from "metabase/setup/useStep";
import { Text } from "metabase/ui";
@@ -19,11 +19,12 @@ export const LicenseTokenStep = ({ stepLabel }: NumberedStepProps) => {
const storeToken = useSelector((state) => state.setup.licenseToken);
const dispatch = useDispatch();
+ const [sendToast] = useToast();
const handleSubmit = async (token: string | null) => {
try {
await dispatch(submitLicenseToken(token)).unwrap();
- dispatch(addUndo({ message: t`Your license is activated` }));
+ sendToast({ message: t`Your license is activated` });
} catch (err) {
console.error(err);
throw new Error(
diff --git a/frontend/src/metabase/visualizations/components/ObjectDetail/DeleteObjectModal.tsx b/frontend/src/metabase/visualizations/components/ObjectDetail/DeleteObjectModal.tsx
index 95e832b7926..fb84a9cee3f 100644
--- a/frontend/src/metabase/visualizations/components/ObjectDetail/DeleteObjectModal.tsx
+++ b/frontend/src/metabase/visualizations/components/ObjectDetail/DeleteObjectModal.tsx
@@ -4,8 +4,7 @@ import { t } from "ttag";
import { getActionErrorMessage } from "metabase/actions/utils";
import Button from "metabase/common/components/Button";
import ModalContent from "metabase/common/components/ModalContent";
-import { useDispatch } from "metabase/lib/redux";
-import { addUndo } from "metabase/redux/undo";
+import { useToast } from "metabase/common/hooks/use-toast";
import { ActionsApi } from "metabase/services";
import type { WritebackActionId } from "metabase-types/api";
@@ -24,7 +23,7 @@ export const DeleteObjectModal: FunctionComponent<Props> = ({
onClose,
onSuccess,
}) => {
- const dispatch = useDispatch();
+ const [sendToast] = useToast();
const handleSubmit = async () => {
try {
@@ -36,12 +35,12 @@ export const DeleteObjectModal: FunctionComponent<Props> = ({
});
const message = t`Successfully deleted`;
- dispatch(addUndo({ message, toastColor: "success" }));
+ sendToast({ message, toastColor: "success" });
onClose();
onSuccess();
} catch (error) {
const message = getActionErrorMessage(error);
- dispatch(addUndo({ icon: "warning", toastColor: "error", message }));
+ sendToast({ icon: "warning", toastColor: "error", message });
}
};