@@ -3,6 +3,7 @@ import { createReducer, on } from '@ngrx/store';
3
3
import {
4
4
CollectionApiActions ,
5
5
CollectionPageActions ,
6
+ SelectedBookPageActions ,
6
7
} from '@example-app/books/actions' ;
7
8
8
9
export const collectionFeatureKey = 'collection' ;
@@ -30,9 +31,15 @@ export const reducer = createReducer(
30
31
loading : false ,
31
32
ids : books . map ( book => book . id ) ,
32
33
} ) ) ,
33
- // Supports handing multiple types of actions
34
+ /**
35
+ * Optimistically add book to collection.
36
+ * If this succeeds there's nothing to do.
37
+ * If this fails we revert state by removing the book.
38
+ *
39
+ * `on` supports handling multiple types of actions
40
+ */
34
41
on (
35
- CollectionApiActions . addBookSuccess ,
42
+ SelectedBookPageActions . addBook ,
36
43
CollectionApiActions . removeBookFailure ,
37
44
( state , { book } ) => {
38
45
if ( state . ids . indexOf ( book . id ) > - 1 ) {
@@ -44,8 +51,12 @@ export const reducer = createReducer(
44
51
} ;
45
52
}
46
53
) ,
54
+ /**
55
+ * Optimistically remove book from collection.
56
+ * If addBook fails, we "undo" adding the book.
57
+ */
47
58
on (
48
- CollectionApiActions . removeBookSuccess ,
59
+ SelectedBookPageActions . removeBook ,
49
60
CollectionApiActions . addBookFailure ,
50
61
( state , { book } ) => ( {
51
62
...state ,
0 commit comments