Skip to content
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

fix(alerts/reports): removing duplicate notification method options #27239

Merged
merged 2 commits into from
Apr 10, 2024
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
49 changes: 31 additions & 18 deletions superset-frontend/src/features/alerts/AlertReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { useCommonConf } from 'src/features/databases/state';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import {
NotificationMethodOption,
NotificationSetting,
AlertObject,
ChartObject,
DashboardObject,
Expand Down Expand Up @@ -395,12 +396,6 @@ const NotificationMethodAdd: FunctionComponent<NotificationMethodAddProps> = ({
);
};

type NotificationSetting = {
method?: NotificationMethodOption;
recipients: string;
options: NotificationMethodOption[];
};

const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
addDangerToast,
onAdd,
Expand Down Expand Up @@ -497,15 +492,26 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
NotificationSetting[]
>([]);
const onNotificationAdd = () => {
const settings: NotificationSetting[] = notificationSettings.slice();
settings.push({
recipients: '',
options: allowedNotificationMethods,
});
setNotificationSettings([
...notificationSettings,
{
recipients: '',
// options shown in the newly added notification method
options: allowedNotificationMethods.filter(
// are filtered such that
option =>
// options are not included
!notificationSettings.reduce(
// when it exists in previous notificationSettings
(accum, setting) => accum || option === setting.method,
false,
),
),
},
]);

setNotificationSettings(settings);
setNotificationAddState(
settings.length === allowedNotificationMethods.length
notificationSettings.length === allowedNotificationMethods.length
? 'hidden'
: 'disabled',
);
Expand Down Expand Up @@ -547,13 +553,20 @@ const AlertReportModal: FunctionComponent<AlertReportModalProps> = ({
index: number,
setting: NotificationSetting,
) => {
const settings = notificationSettings.slice();
// if you've changed notification method
if (notificationSettings[index].method !== setting.method) {
notificationSettings[index] = setting;

settings[index] = setting;
setNotificationSettings(settings);
setNotificationSettings(
notificationSettings.filter((_, idx) => idx <= index),
);
if (notificationSettings.length - 1 > index) {
setNotificationAddState('active');
}

if (setting.method !== undefined && notificationAddState !== 'hidden') {
setNotificationAddState('active');
if (setting.method !== undefined && notificationAddState !== 'hidden') {
setNotificationAddState('active');
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import React, { FunctionComponent, useState } from 'react';
import { styled, t, useTheme } from '@superset-ui/core';
import { Select } from 'src/components';
import Icons from 'src/components/Icons';
import { NotificationMethodOption } from '../types';
import { NotificationMethodOption, NotificationSetting } from '../types';
import { StyledInputContainer } from '../AlertReportModal';

const StyledNotificationMethod = styled.div`
Expand All @@ -46,12 +46,6 @@ const StyledNotificationMethod = styled.div`
}
`;

type NotificationSetting = {
method?: NotificationMethodOption;
recipients: string;
options: NotificationMethodOption[];
};

interface NotificationMethodProps {
setting?: NotificationSetting | null;
index: number;
Expand Down Expand Up @@ -130,7 +124,7 @@ export const NotificationMethod: FunctionComponent<NotificationMethodProps> = ({
)}
value={method}
/>
{method !== undefined && index !== 0 && !!onRemove ? (
{index !== 0 && !!onRemove ? (
<span
role="button"
tabIndex={0}
Expand Down
6 changes: 6 additions & 0 deletions superset-frontend/src/features/alerts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export type DatabaseObject = {

export type NotificationMethodOption = 'Email' | 'Slack';

export type NotificationSetting = {
method?: NotificationMethodOption;
recipients: string;
options: NotificationMethodOption[];
};

export type Recipient = {
recipient_config_json: {
target: string;
Expand Down
Loading