Added effects

This commit is contained in:
NejcZdovc
2016-09-28 14:52:34 +02:00
committed by József Farkas
parent c28f564e3f
commit f93477487c
11 changed files with 199 additions and 89 deletions

View File

@@ -0,0 +1,2 @@
export * from './keymap';
export * from './macro';

View File

@@ -1,32 +1,38 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, Effect } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/withLatestFrom';
import { KeymapActions } from '../actions/keymap';
import { KeymapActions } from '../actions';
import { AppState } from '../index';
@Injectable()
export class KeymapEffects {
@Effect()remove$: any = this.actions$
@Effect({ dispatch: false })remove$: any = this.actions$
.ofType(KeymapActions.REMOVE)
.map(() => {
// TODO: Waiting for the fix: https://github.com/angular/angular/issues/10770
// If state is empty router.navigate(['/keymap']);
// Else router.navigate(['/keymap']);
.withLatestFrom(this.store)
.do((latest) => {
let state: AppState = latest[1];
if (state.keymaps.entities.length === 0) {
this.router.navigate(['/keymap/add']);
} else {
this.router.navigate(['/keymap']);
}
});
@Effect() editAbbr$: any = this.actions$
@Effect({ dispatch: false }) editAbbr$: any = this.actions$
.ofType(KeymapActions.EDIT_ABBR)
.map<string>(action => action.payload.abbr)
.map((abbr: string) => {
console.log(abbr);
// TODO: Waiting for the fix: https://github.com/angular/angular/issues/10770
// // router.navigate(['/keymap', abbr]);
.withLatestFrom(this.store)
.do((latest) => {
let state: AppState = latest[1];
this.router.navigate(['/keymap', state.keymaps.newAbbr]);
});
constructor(
private actions$: Actions
) {}
constructor(private actions$: Actions, private router: Router, private store: Store<AppState>) {}
}

View File

@@ -1,21 +1,30 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions, Effect } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/withLatestFrom';
import { MacroActions } from '../actions';
import { AppState } from '../index';
@Injectable()
export class MacroEffects {
@Effect()remove$: any = this.actions$
@Effect({dispatch: false}) remove$: any = this.actions$
.ofType(MacroActions.REMOVE)
.map(() => {
// TODO: Waiting for the fix: https://github.com/angular/angular/issues/10770
// If state is empty router.navigate(['/macro']);
// Else router.navigate(['/macro']);
.withLatestFrom(this.store)
.do((latest) => {
let state: AppState = latest[1];
if (state.macros.entities.length === 0) {
this.router.navigate(['/macro/add']);
} else {
this.router.navigate(['/macro']);
}
});
constructor(private actions$: Actions) {}
constructor(private actions$: Actions, private router: Router, private store: Store<AppState>) {}
}