-
Notifications
You must be signed in to change notification settings - Fork 0
/
love-route.guard.ts
34 lines (29 loc) · 1.19 KB
/
love-route.guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router';
import { CoreServiceModule } from '../core-service.module';
import { UnicornService } from '../services/unicorns.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Unicorn } from '../models/unicorn.model';
@Injectable({
providedIn: CoreServiceModule
})
export class LoveRouteGuard implements CanActivateChild, CanActivate {
constructor(
private readonly unicornService: UnicornService,
private readonly router: Router
) { }
canActivate(_route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.unicornService.getAllUnicorns().pipe(
map((unicorns: Unicorn[]) => {
if (unicorns || this.unicornService.atLeastOneMaleAndOneFemale(unicorns)) {
return true;
}
this.router.navigate(['/', 'home']);
return false;
}));
}
canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
return this.canActivate(childRoute, state);
}
}