This repository has been archived by the owner on May 6, 2019. It is now read-only.
/
book-exists.ts
101 lines (92 loc) · 3.3 KB
/
book-exists.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/let';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';
import { Router, CanActivate, ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { GoogleBooksService } from '../services/google-books';
import * as fromRoot from '../reducers';
import * as book from '../actions/book';
/**
* Guards are hooks into the route resolution process, providing an opportunity
* to inform the router's navigation process whether the route should continue
* to activate this route. Guards must return an observable of true or false.
*/
@Injectable()
export class BookExistsGuard implements CanActivate {
constructor(
private store: Store<fromRoot.State>,
private googleBooks: GoogleBooksService,
private router: Router
) { }
/**
* This method creates an observable that waits for the `loaded` property
* of the collection state to turn `true`, emitting one time once loading
* has finished.
*/
waitForCollectionToLoad(): Observable<boolean> {
return this.store.select(fromRoot.getCollectionLoaded)
.filter(loaded => loaded)
.take(1);
}
/**
* This method checks if a book with the given ID is already registered
* in the Store
*/
hasBookInStore(id: string): Observable<boolean> {
return this.store.select(fromRoot.getBookEntities)
.map(entities => !!entities[id])
.take(1);
}
/**
* This method loads a book with the given ID from the API and caches
* it in the store, returning `true` or `false` if it was found.
*/
hasBookInApi(id: string): Observable<boolean> {
return this.googleBooks.retrieveBook(id)
.map(bookEntity => new book.LoadAction(bookEntity))
.do((action: book.LoadAction) => this.store.dispatch(action))
.map(book => !!book)
.catch(() => {
this.router.navigate(['/404']);
return of(false);
});
}
/**
* `hasBook` composes `hasBookInStore` and `hasBookInApi`. It first checks
* if the book is in store, and if not it then checks if it is in the
* API.
*/
hasBook(id: string): Observable<boolean> {
return this.hasBookInStore(id)
.switchMap(inStore => {
if (inStore) {
return of(inStore);
}
return this.hasBookInApi(id);
});
}
/**
* This is the actual method the router will call when our guard is run.
*
* Our guard waits for the collection to load, then it checks if we need
* to request a book from the API or if we already have it in our cache.
* If it finds it in the cache or in the API, it returns an Observable
* of `true` and the route is rendered successfully.
*
* If it was unable to find it in our cache or in the API, this guard
* will return an Observable of `false`, causing the router to move
* on to the next candidate route. In this case, it will move on
* to the 404 page.
*/
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
return this.waitForCollectionToLoad()
.switchMap(() => this.hasBook(route.params['id']));
}
}