mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-18 07:23:03 +00:00
Merge remote-tracking branch 'origin/main' into more-eslint
This commit is contained in:
@@ -2,13 +2,16 @@ import { Injectable } from '@angular/core';
|
||||
import {
|
||||
ActivatedRouteSnapshot,
|
||||
CanActivateChild,
|
||||
NavigationStart,
|
||||
Router,
|
||||
RouterStateSnapshot,
|
||||
UrlTree,
|
||||
} from '@angular/router';
|
||||
import { Observable } from 'rxjs';
|
||||
import {
|
||||
filter,
|
||||
map,
|
||||
take,
|
||||
tap,
|
||||
} from 'rxjs/operators';
|
||||
|
||||
import { getPageInternalServerErrorRoute } from '../../app-routing-paths';
|
||||
@@ -30,17 +33,38 @@ export class ServerCheckGuard implements CanActivateChild {
|
||||
*/
|
||||
canActivateChild(
|
||||
route: ActivatedRouteSnapshot,
|
||||
state: RouterStateSnapshot): Observable<boolean> {
|
||||
state: RouterStateSnapshot,
|
||||
): Observable<boolean | UrlTree> {
|
||||
|
||||
return this.rootDataService.checkServerAvailability().pipe(
|
||||
take(1),
|
||||
tap((isAvailable: boolean) => {
|
||||
map((isAvailable: boolean) => {
|
||||
if (!isAvailable) {
|
||||
this.rootDataService.invalidateRootCache();
|
||||
this.router.navigateByUrl(getPageInternalServerErrorRoute());
|
||||
return this.router.parseUrl(getPageInternalServerErrorRoute());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen to all router events. Every time a new navigation starts, invalidate the cache
|
||||
* for the root endpoint. That way we retrieve it once per routing operation to ensure the
|
||||
* backend is not down. But if the guard is called multiple times during the same routing
|
||||
* operation, the cached version is used.
|
||||
*/
|
||||
listenForRouteChanges(): void {
|
||||
// we'll always be too late for the first NavigationStart event with the router subscribe below,
|
||||
// so this statement is for the very first route operation. A `find` without using the cache,
|
||||
// rather than an invalidateRootCache, because invalidating as the app is bootstrapping can
|
||||
// break other features
|
||||
this.rootDataService.findRoot(false);
|
||||
|
||||
this.router.events.pipe(
|
||||
filter(event => event instanceof NavigationStart),
|
||||
).subscribe(() => {
|
||||
this.rootDataService.invalidateRootCache();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user