1
1
import { Actions , Effect , ofType } from '@ngrx/effects' ;
2
2
import { CartService } from '../services/cart.service' ;
3
- import { Action } from '@ngrx/store' ;
3
+ import { Action , Store } from '@ngrx/store' ;
4
4
import { Observable , defer , of , timer } from 'rxjs' ;
5
5
6
6
import * as cartActions from './actions' ;
7
7
import * as productDetailsActions from '../product-details/actions' ;
8
- import { switchMap , map , catchError , concatMap } from 'rxjs/operators' ;
8
+ import * as cartDetailsActions from '../cart-details/actions' ;
9
+ import {
10
+ switchMap ,
11
+ map ,
12
+ catchError ,
13
+ concatMap ,
14
+ withLatestFrom ,
15
+ mapTo ,
16
+ } from 'rxjs/operators' ;
9
17
import { Injectable } from '@angular/core' ;
10
18
import { MatSnackBar } from '@angular/material' ;
11
19
20
+ import * as selectors from './selectors' ;
21
+ import { Router } from '@angular/router' ;
22
+
12
23
const REFRESH_CART_ITEMS_INTEVAL_MS = 20 * 1000 ; // 20 seconds
13
24
14
25
@Injectable ( )
15
26
export class CartEffects {
16
27
constructor (
17
28
private readonly actions$ : Actions ,
18
29
private readonly cartService : CartService ,
19
- private readonly snackBar : MatSnackBar
30
+ private readonly store : Store < { } > ,
31
+ private readonly snackBar : MatSnackBar ,
32
+ private readonly router : Router
20
33
) { }
21
34
22
35
@Effect ( )
@@ -42,6 +55,76 @@ export class CartEffects {
42
55
)
43
56
) ;
44
57
58
+ @Effect ( )
59
+ removeCartItem : Observable < Action > = this . actions$ . pipe (
60
+ ofType < cartDetailsActions . RemoveItem > ( cartDetailsActions . REMOVE_ITEM ) ,
61
+ concatMap ( ( { itemId } ) =>
62
+ this . cartService . removeOne ( itemId ) . pipe (
63
+ map ( ( ) => new cartActions . RemoveItemSuccess ( ) ) ,
64
+ // passing the itemId to the Error, so it can be restored.
65
+ catchError ( ( ) => of ( new cartActions . RemoveItemError ( itemId ) ) )
66
+ )
67
+ )
68
+ ) ;
69
+
70
+ @Effect ( )
71
+ removeAllItems : Observable < Action > = this . actions$ . pipe (
72
+ ofType < cartDetailsActions . RemoveAll > ( cartDetailsActions . REMOVE_ALL ) ,
73
+ withLatestFrom ( this . store . select ( selectors . getCartItemsIds ) ) ,
74
+ concatMap ( ( [ action , ids ] ) =>
75
+ this . cartService . removeAll ( ) . pipe (
76
+ map ( ( ) => new cartActions . RemoveAllSuccess ( ) ) ,
77
+ // passing the itemId to the Error, so it can be restored.
78
+ catchError ( ( ) => of ( new cartActions . RemoveAllError ( ids ) ) )
79
+ )
80
+ )
81
+ ) ;
82
+
83
+ @Effect ( )
84
+ purchaseItems : Observable < Action > = this . actions$ . pipe (
85
+ ofType < cartDetailsActions . PurchaseItems > ( cartDetailsActions . PURCHASE_ITEMS ) ,
86
+ concatMap ( ( { payload } ) =>
87
+ this . cartService . purchase ( payload ) . pipe (
88
+ map ( ( ) => new cartActions . PurchaseItemsSuccess ( ) ) ,
89
+ catchError ( ( ) => of ( new cartActions . PurchaseItemsError ( ) ) )
90
+ )
91
+ )
92
+ ) ;
93
+
94
+ @Effect ( )
95
+ handlePurchaseSuccess : Observable < Action > = this . actions$ . pipe (
96
+ ofType < cartActions . PurchaseItemsSuccess > (
97
+ cartActions . PURCHASE_ITEMS_SUCCESS
98
+ ) ,
99
+ map ( ( ) => {
100
+ this . router . navigate ( [ '/home' ] ) ;
101
+ // Setting the timeout, so that angular would re-run change detection.
102
+ setTimeout (
103
+ ( ) =>
104
+ this . snackBar . open ( 'Items purchased!' , 'Success' , {
105
+ duration : 2500 ,
106
+ } ) ,
107
+ 0
108
+ ) ;
109
+ } ) ,
110
+ mapTo ( new cartActions . FetchCartItems ( ) )
111
+ ) ;
112
+
113
+ @Effect ( { dispatch : false } )
114
+ purchaseItemsError = this . actions$ . pipe (
115
+ ofType < cartActions . PurchaseItemsError > ( cartActions . PURCHASE_ITEMS_ERROR ) ,
116
+ map ( ( ) => {
117
+ // Setting the timeout, so that angular would re-run change detection.
118
+ setTimeout (
119
+ ( ) =>
120
+ this . snackBar . open ( 'Could not purchase items' , 'Error' , {
121
+ duration : 2500 ,
122
+ } ) ,
123
+ 0
124
+ ) ;
125
+ } )
126
+ ) ;
127
+
45
128
@Effect ( { dispatch : false } )
46
129
handleFetchError = this . actions$ . pipe (
47
130
ofType < cartActions . AddItemError > ( cartActions . ADD_ITEM_ERROR ) ,
0 commit comments