mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-17 15:03:07 +00:00
added preloading guard for search results
This commit is contained in:
27
src/app/+search-page/search-page-preloader.guard.ts
Normal file
27
src/app/+search-page/search-page-preloader.guard.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ActivatedRouteSnapshot, CanActivate, Resolve, RouterStateSnapshot } from '@angular/router';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import { RemoteData } from '../core/data/remote-data';
|
||||
import { getSucceededRemoteData } from '../core/shared/operators';
|
||||
import { ItemDataService } from '../core/data/item-data.service';
|
||||
import { Item } from '../core/shared/item.model';
|
||||
import { SearchService } from './search-service/search.service';
|
||||
import { SearchConfigurationService } from './search-service/search-configuration.service';
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class SearchPagePreloader implements CanActivate {
|
||||
constructor(private searchService: SearchService, private configService: SearchConfigurationService) {
|
||||
}
|
||||
|
||||
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
|
||||
const searchResults$ = this.configService.paginatedSearchOptions.flatMap((options) =>
|
||||
this.searchService.search(options)
|
||||
);
|
||||
route.data.results = searchResults$;
|
||||
return searchResults$.pipe(
|
||||
getSucceededRemoteData(),
|
||||
map(() => true),
|
||||
).catch(() => Observable.of(false))
|
||||
}
|
||||
}
|
@@ -2,12 +2,22 @@ import { NgModule } from '@angular/core';
|
||||
import { RouterModule } from '@angular/router';
|
||||
|
||||
import { SearchPageComponent } from './search-page.component';
|
||||
import { SearchPagePreloader } from './search-page-preloader.guard';
|
||||
|
||||
@NgModule({
|
||||
imports: [
|
||||
RouterModule.forChild([
|
||||
{ path: '', component: SearchPageComponent, data: { title: 'search.title' } }
|
||||
{
|
||||
path: '',
|
||||
component: SearchPageComponent,
|
||||
data: { title: 'search.title' },
|
||||
canActivate: [SearchPagePreloader]
|
||||
}
|
||||
])
|
||||
],
|
||||
providers: [
|
||||
SearchPagePreloader
|
||||
]
|
||||
})
|
||||
export class SearchPageRoutingModule { }
|
||||
export class SearchPageRoutingModule {
|
||||
}
|
||||
|
Reference in New Issue
Block a user