/
alarms.effects.ts
39 lines (34 loc) · 1.15 KB
/
alarms.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
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { AlarmsActionTypes, AlarmsLoaded } from './alarms.actions';
import { map, mergeMap, withLatestFrom } from 'rxjs/operators';
import { EMPTY } from 'rxjs';
import { AlarmsFacade } from './alarms.facade';
import { AuthFacade } from '../../../+state/auth.facade';
@Injectable()
export class AlarmsEffects {
@Effect()
loadAlarms$ = this.actions$.pipe(
ofType(AlarmsActionTypes.LoadAlarms),
map(() => new AlarmsLoaded([]))
);
@Effect()
persistOnAdditionAndDeletion$ = this.actions$.pipe(
ofType(AlarmsActionTypes.AddAlarms),
mergeMap(() => EMPTY)
);
@Effect()
persistAlarms$ = this.actions$.pipe(
ofType(AlarmsActionTypes.PersistAlarms),
withLatestFrom(this.authFacade.userId$, this.alarmsFacade.allAlarms$),
map(([, userId, alarms]) => {
return alarms.map(alarm => {
return { ...alarm, foreignKey: userId };
});
}),
mergeMap(() => EMPTY)
);
constructor(private actions$: Actions, private alarmsFacade: AlarmsFacade, private authFacade: AuthFacade) {
this.alarmsFacade.loadAlarms();
}
}