-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathinstruments.effects.ts
116 lines (108 loc) · 3.72 KB
/
instruments.effects.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Injectable } from "@angular/core";
import { Actions, createEffect, ofType, concatLatestFrom } from "@ngrx/effects";
import { InstrumentApi, Instrument } from "shared/sdk";
import * as fromActions from "state-management/actions/instruments.actions";
import { switchMap, map, catchError, mergeMap } from "rxjs/operators";
import { of } from "rxjs";
import { Store } from "@ngrx/store";
import { selectFilters } from "state-management/selectors/instruments.selectors";
import {
loadingAction,
loadingCompleteAction,
} from "state-management/actions/user.actions";
@Injectable()
export class InstrumentEffects {
filters$ = this.store.select(selectFilters);
fetchInstruments$ = createEffect(() => {
return this.actions$.pipe(
ofType(
fromActions.fetchInstrumentsAction,
fromActions.changePageAction,
fromActions.sortByColumnAction,
),
concatLatestFrom(() => this.filters$),
map(([action, filters]) => filters),
switchMap(({ sortField: order, skip, limit }) =>
this.instrumentApi.find<Instrument>({ order, limit, skip }).pipe(
mergeMap((instruments: Instrument[]) => [
fromActions.fetchInstrumentsCompleteAction({ instruments }),
fromActions.fetchCountAction(),
]),
catchError(() => of(fromActions.fetchInstrumentsFailedAction())),
),
),
);
});
fetchCount$ = createEffect(() => {
return this.actions$.pipe(
ofType(fromActions.fetchCountAction),
switchMap(() =>
this.instrumentApi.find().pipe(
map((instruments) =>
fromActions.fetchCountCompleteAction({ count: instruments.length }),
),
catchError(() => of(fromActions.fetchCountFailedAction())),
),
),
);
});
fetchInstrument$ = createEffect(() => {
return this.actions$.pipe(
ofType(fromActions.fetchInstrumentAction),
switchMap(({ pid }) =>
this.instrumentApi.findById<Instrument>(encodeURIComponent(pid)).pipe(
map((instrument: Instrument) =>
fromActions.fetchInstrumentCompleteAction({ instrument }),
),
catchError(() => of(fromActions.fetchInstrumentFailedAction())),
),
),
);
});
saveCustomMetadata$ = createEffect(() => {
return this.actions$.pipe(
ofType(fromActions.saveCustomMetadataAction),
switchMap(({ pid, customMetadata }) =>
this.instrumentApi
.patchAttributes(encodeURIComponent(pid), { customMetadata })
.pipe(
map((instrument: Instrument) =>
fromActions.saveCustomMetadataCompleteAction({ instrument }),
),
catchError(() => of(fromActions.saveCustomMetadataFailedAction())),
),
),
);
});
loading$ = createEffect(() => {
return this.actions$.pipe(
ofType(
fromActions.fetchInstrumentsAction,
fromActions.fetchCountAction,
fromActions.fetchInstrumentAction,
fromActions.saveCustomMetadataAction,
),
switchMap(() => of(loadingAction())),
);
});
loadingComplete$ = createEffect(() => {
return this.actions$.pipe(
ofType(
fromActions.fetchInstrumentsCompleteAction,
fromActions.fetchInstrumentsFailedAction,
fromActions.fetchCountCompleteAction,
fromActions.fetchCountFailedAction,
fromActions.fetchInstrumentCompleteAction,
fromActions.fetchInstrumentFailedAction,
fromActions.saveCustomMetadataCompleteAction,
fromActions.saveCustomMetadataFailedAction,
),
switchMap(() => of(loadingCompleteAction())),
);
});
constructor(
private actions$: Actions,
private instrumentApi: InstrumentApi,
private store: Store,
) {}
}