Alerts: Dedupe alerts so that we do not fill the screen with the same alert messsage (#30935)

This commit is contained in:
Torkel Ödegaard
2021-02-08 10:56:04 +01:00
committed by GitHub
parent ae64dcf063
commit 50da456bd0
2 changed files with 59 additions and 4 deletions

View File

@@ -116,4 +116,46 @@ describe('notify', () => {
expect(result).toEqual(expectedResult);
});
it('Dedupe identical alerts', () => {
const initialState = {
appNotifications: [
{
id: 'id1',
severity: AppNotificationSeverity.Success,
icon: 'success',
title: 'test',
text: 'test alert',
timeout: AppNotificationTimeout.Success,
},
],
};
const result = appNotificationsReducer(
initialState,
notifyApp({
id: 'id2',
severity: AppNotificationSeverity.Success,
icon: 'success',
title: 'test',
text: 'test alert',
timeout: AppNotificationTimeout.Success,
})
);
const expectedResult = {
appNotifications: [
{
id: 'id1',
severity: AppNotificationSeverity.Success,
icon: 'success',
title: 'test',
text: 'test alert',
timeout: AppNotificationTimeout.Success,
},
],
};
expect(result).toEqual(expectedResult);
});
});

View File

@@ -15,10 +15,23 @@ const appNotificationsSlice = createSlice({
name: 'appNotifications',
initialState,
reducers: {
notifyApp: (state, action: PayloadAction<AppNotification>): AppNotificationsState => ({
...state,
appNotifications: state.appNotifications.concat([action.payload]),
}),
notifyApp: (state, action: PayloadAction<AppNotification>) => {
const newAlert = action.payload;
for (const existingAlert of state.appNotifications) {
if (
newAlert.icon === existingAlert.icon &&
newAlert.severity === existingAlert.severity &&
newAlert.text === existingAlert.text &&
newAlert.title === existingAlert.title &&
newAlert.component === existingAlert.component
) {
return;
}
}
state.appNotifications.push(newAlert);
},
clearAppNotification: (state, action: PayloadAction<string>): AppNotificationsState => ({
...state,
appNotifications: state.appNotifications.filter((appNotification) => appNotification.id !== action.payload),