-
-
Notifications
You must be signed in to change notification settings - Fork 746
/
metric.effects.ts
59 lines (53 loc) · 1.77 KB
/
metric.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
import {Injectable} from '@angular/core';
import {Actions, Effect, ofType} from '@ngrx/effects';
import {mapTo, tap, withLatestFrom} from 'rxjs/operators';
import {select, Store} from '@ngrx/store';
import {MetricActionTypes} from './metric.actions';
import {selectCurrentProjectId} from '../../project/store/project.reducer';
import {PersistenceService} from '../../../core/persistence/persistence.service';
import {selectMetricFeatureState} from './metric.selectors';
import {SnackOpen} from '../../../core/snack/store/snack.actions';
import {T} from '../../../t.const';
@Injectable()
export class MetricEffects {
@Effect({dispatch: false}) updateMetrics$: any = this._actions$
.pipe(
ofType(
MetricActionTypes.AddMetric,
MetricActionTypes.UpdateMetric,
MetricActionTypes.DeleteMetric,
MetricActionTypes.UpsertMetric,
),
withLatestFrom(
this._store$.pipe(select(selectCurrentProjectId)),
this._store$.pipe(select(selectMetricFeatureState)),
),
tap(this._saveToLs.bind(this))
);
@Effect() saveMetrics$: any = this._actions$
.pipe(
ofType(
MetricActionTypes.AddMetric,
MetricActionTypes.UpsertMetric,
MetricActionTypes.UpdateMetric,
),
mapTo(new SnackOpen({
type: 'SUCCESS',
msg: T.F.METRIC.SNACK.SAVE_METRIC
})),
);
constructor(
private _actions$: Actions,
private _store$: Store<any>,
private _persistenceService: PersistenceService
) {
}
private _saveToLs([action, currentProjectId, metricState]) {
if (currentProjectId) {
this._persistenceService.saveLastActive();
this._persistenceService.metric.save(currentProjectId, metricState);
} else {
throw new Error('No current project id');
}
}
}