Commit 14c4b7e6 authored by Tifenn Guillas's avatar Tifenn Guillas
Browse files

WIP: Implements navigation guard

parent 20084a95
import { NgModule } from '@angular/core';
import { Routes, RouterModule, PreloadAllModules } from '@angular/router';
import { NavigationGuard } from './core/navigation.guard';
import { NotFoundPageComponent } from './core/containers/not-found-page.component';
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: 'search',
loadChildren: () => import('./search/search.module').then(m => m.SearchModule)
loadChildren: () => import('./search/search.module').then(m => m.SearchModule),
canLoad: [NavigationGuard]
},
{
path: 'search-multiple',
......
import { TestBed } from '@angular/core/testing';
import { NavigationGuard } from './navigation.guard';
describe('NavigationGuardGuard', () => {
let guard: NavigationGuard;
beforeEach(() => {
TestBed.configureTestingModule({});
guard = TestBed.inject(NavigationGuard);
});
it('should be created', () => {
expect(guard).toBeTruthy();
});
});
import { Injectable } from '@angular/core';
import { CanLoad, Route, UrlSegment } from '@angular/router';
import { Observable, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { Actions } from '@ngrx/effects';
import { SearchService } from '../search/store/search.service';
import { ToastrService } from 'ngx-toastr';
import { Store } from '@ngrx/store';
import * as fromRouter from '@ngrx/router-store';
import * as utils from '../shared/utils';
import * as fromSearch from '../search/store/search.reducer';
import * as fromMetamodel from '../metamodel/reducers';
import * as fromConeSearch from '../shared/cone-search/store/cone-search.reducer';
import { Instance } from '../metamodel/model';
@Injectable({
providedIn: 'root'
})
export class NavigationGuard implements CanLoad {
constructor(private store$: Store<{ metamodel: fromMetamodel.State }>) { }
canLoad(
route: Route,
segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
// console.error(route.path);
switch (route.path) {
case 'search':
console.error(this.isSearchAllowed());
return this.isSearchAllowed();
break;
// default:
// return false;
}
}
waitForDataToLoad(): Observable<Instance> {
return this.store$.select(state => state.metamodel.instance.instance);
}
isSearchAllowed(): Observable<boolean> {
return this.waitForDataToLoad().pipe(
switchMap(instance => {
// console.error(instance.config.search);
return of(instance.config.search);
})
)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment