Skip to content

Commit

Permalink
fix(admin-ui): Unsubscribe from alerts when logging out (#3071)
Browse files Browse the repository at this point in the history
relates to: #2188
  • Loading branch information
oliverstreissi authored Sep 19, 2024
1 parent f1bfb09 commit f38340b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
[availableLanguages]="availableLanguages"
(selectUiLanguage)="selectUiLanguage()"
(logOut)="logOut()"
></vdr-user-menu>
/>
</div>
</div>
<div class="content-area"><router-outlet></router-outlet></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Observable,
of,
Subject,
Subscription,
switchMap,
} from 'rxjs';
import { filter, map, startWith, take } from 'rxjs/operators';
Expand Down Expand Up @@ -132,14 +133,15 @@ export class Alert<T> {
activeAlert$: Observable<ActiveAlert | undefined>;
private hasRun$ = new BehaviorSubject(false);
private data$ = new BehaviorSubject<T | undefined>(undefined);
private readonly subscription: Subscription;
constructor(
private config: AlertConfig<T>,
private context: AlertContext,
) {
if (this.config.recheck) {
this.config.recheck(this.context).subscribe(() => this.runCheck());
this.subscription = this.config.recheck(this.context).subscribe(() => this.runCheck());
}
this.activeAlert$ = combineLatest(this.data$, this.hasRun$).pipe(
this.activeAlert$ = combineLatest([this.data$, this.hasRun$]).pipe(
map(([data, hasRun]) => {
if (!data) {
return;
Expand Down Expand Up @@ -176,6 +178,12 @@ export class Alert<T> {
}
this.hasRun$.next(false);
}

destroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}

@Injectable({
Expand Down Expand Up @@ -237,6 +245,12 @@ export class AlertsService {
}
}

clearAlerts() {
this.alertsMap.forEach(alert => alert.destroy());
this.alertsMap.clear();
this.configUpdated.next();
}

protected createContext(): AlertContext {
return {
injector: this.injector,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Injectable } from '@angular/core';
import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
import { Observable, of } from 'rxjs';
import { catchError, map, mapTo, mergeMap, switchMap } from 'rxjs/operators';
import { catchError, map, mergeMap, switchMap, tap } from 'rxjs/operators';

import { AttemptLoginMutation, CurrentUserFragment } from '../../common/generated-types';
import { DataService } from '../../data/providers/data.service';
import { ServerConfigService } from '../../data/server-config';
import { LocalStorageService } from '../local-storage/local-storage.service';
import { PermissionsService } from '../permissions/permissions.service';
import { AlertsService } from '../alerts/alerts.service';

/**
* This service handles logic relating to authentication of the current user.
Expand All @@ -21,6 +22,7 @@ export class AuthService {
private dataService: DataService,
private serverConfigService: ServerConfigService,
private permissionsService: PermissionsService,
private alertService: AlertsService,
) {}

/**
Expand Down Expand Up @@ -79,7 +81,10 @@ export class AuthService {
return [];
}
}),
mapTo(true),
tap(() => {
this.alertService.clearAlerts();
}),
map(() => true),
);
}

Expand Down Expand Up @@ -129,7 +134,7 @@ export class AuthService {
}),
);
}),
mapTo(true),
map(() => true),
catchError(err => of(false)),
);
}
Expand Down

0 comments on commit f38340b

Please sign in to comment.