Skip to content

Commit

Permalink
NAS-123151 / 24.04 / HA: Alert is duplicated (#8582)
Browse files Browse the repository at this point in the history
* NAS-123151: HA: Alert is duplicated

* NAS-123151: PR update

* NAS-123151: PR update

* NAS-123151: PR update

* NAS-123151: PR update

* NAS-123151: PR update

* NAS-123151: PR update
  • Loading branch information
AlexKarpov98 authored Aug 18, 2023
1 parent bfbb8e8 commit 11aee52
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { WebSocketService } from 'app/services/ws.service';
import { adminUiInitialized } from 'app/store/admin-panel/admin.actions';
import { haInfoReducer } from 'app/store/ha-info/ha-info.reducer';
import { haInfoStateKey } from 'app/store/ha-info/ha-info.selectors';
import { alertIndicatorPressed } from 'app/store/topbar/topbar.actions';

const unreadAlerts = [
{
Expand Down Expand Up @@ -188,4 +189,9 @@ describe('AlertsPanelComponent', () => {
expect(alertPanel.unreadAlertComponents).toHaveLength(1);
expect(alertPanel.dismissedAlertComponents).toHaveLength(3);
});

it('calls alert.list when alerts panel is open', () => {
spectator.inject(Store).dispatch(alertIndicatorPressed());
expect(websocket.call).toHaveBeenCalledWith('alert.list');
});
});
1 change: 1 addition & 0 deletions src/app/modules/alerts/store/alert.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const alertsNotLoaded = createAction('[Alerts API] Not Loaded', props<{ e

export const alertAdded = createAction('[Alerts API] Alert Added', props<{ alert: Alert }>());
export const alertChanged = createAction('[Alerts API] Alert Changed', props<{ alert: Alert }>());
export const alertReceivedWhenPanelIsOpen = createAction('[Alerts API] Alert Received (when alerts panel is open)');
export const alertRemoved = createAction('[Alerts API] Alert Removed', props<{ id: string }>());

export const dismissAlertPressed = createAction('[Alert Panel] Dismiss Pressed', props<{ id: string }>());
Expand Down
59 changes: 32 additions & 27 deletions src/app/modules/alerts/store/alert.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,35 @@ import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { TranslateService } from '@ngx-translate/core';
import { EMPTY, forkJoin, of } from 'rxjs';
import {
catchError, filter, map, mergeMap, pairwise, switchMap, withLatestFrom,
EMPTY, forkJoin, of,
} from 'rxjs';
import {
catchError, map, mergeMap, pairwise, switchMap, withLatestFrom,
} from 'rxjs/operators';
import { IncomingApiMessageType } from 'app/enums/api-message-type.enum';
import {
dismissAlertPressed, dismissAllAlertsPressed,
reopenAlertPressed,
reopenAllAlertsPressed,
alertAdded,
alertChanged,
alertRemoved,
alertsLoaded,
alertsNotLoaded,
alertReceivedWhenPanelIsOpen,
alertAdded,
alertsLoaded,
alertChanged,
} from 'app/modules/alerts/store/alert.actions';
import { AlertSlice, selectDismissedAlerts, selectUnreadAlerts } from 'app/modules/alerts/store/alert.selectors';
import {
AlertSlice, selectDismissedAlerts, selectIsAlertPanelOpen, selectUnreadAlerts,
} from 'app/modules/alerts/store/alert.selectors';
import { WebSocketService } from 'app/services/ws.service';
import { adminUiInitialized } from 'app/store/admin-panel/admin.actions';
import { alertIndicatorPressed } from 'app/store/topbar/topbar.actions';

@Injectable()
export class AlertEffects {
loadAlerts$ = createEffect(() => this.actions$.pipe(
ofType(adminUiInitialized),
ofType(adminUiInitialized, alertIndicatorPressed, alertReceivedWhenPanelIsOpen),
switchMap(() => {
return this.ws.call('alert.list').pipe(
map((alerts) => alertsLoaded({ alerts })),
Expand All @@ -43,32 +49,31 @@ export class AlertEffects {
ofType(adminUiInitialized),
switchMap(() => {
return this.ws.subscribe('alert.list').pipe(
filter((event) => event.msg !== IncomingApiMessageType.Removed),
switchMap((event) => {
switch (event.msg) {
case IncomingApiMessageType.Added:
return of(alertAdded({ alert: event.fields }));
case IncomingApiMessageType.Changed:
return of(alertChanged({ alert: event.fields }));
default:
return EMPTY;
}
return this.store$.select(selectIsAlertPanelOpen).pipe(
switchMap((isAlertsPanelOpen) => {
switch (true) {
case [
IncomingApiMessageType.Added, IncomingApiMessageType.Changed,
].includes(event.msg) && isAlertsPanelOpen:
return of(alertReceivedWhenPanelIsOpen());
case event.msg === IncomingApiMessageType.Added && !isAlertsPanelOpen:
return of(alertAdded({ alert: event.fields }));
case event.msg === IncomingApiMessageType.Changed && !isAlertsPanelOpen:
return of(alertChanged({ alert: event.fields }));
case event.msg === IncomingApiMessageType.Removed:
return of(alertRemoved({ id: event.id.toString() }));
default:
return EMPTY;
}
}),
);
}),
);
}),
));

subscribeToRemoval$ = createEffect(() => this.actions$.pipe(
ofType(adminUiInitialized),
switchMap(() => {
return this.ws.subscribe('alert.list').pipe(
filter((event) => event.msg === IncomingApiMessageType.Removed),
map((event) => alertRemoved({ id: event.id.toString() })),
);
}),
));

// TODO: Action errors are not handled. Standartize on how to report on errors and show them.
// TODO: Action errors are not handled. Standardize on how to report on errors and show them.
dismissAlert$ = createEffect(() => this.actions$.pipe(
ofType(dismissAlertPressed),
mergeMap(({ id }) => {
Expand Down
3 changes: 2 additions & 1 deletion src/app/modules/alerts/store/alert.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { AlertLevel } from 'app/enums/alert-level.enum';
import { Alert } from 'app/interfaces/alert.interface';
import {
alertAdded,
alertChanged, alertPanelClosed,
alertChanged,
alertPanelClosed,
alertRemoved,
alertsLoaded,
alertsNotLoaded, dismissAlertPressed, dismissAllAlertsPressed, reopenAlertPressed, reopenAllAlertsPressed,
Expand Down

0 comments on commit 11aee52

Please sign in to comment.