Skip to content

Instantly share code, notes, and snippets.

@sliceofbytes
Created April 20, 2018 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sliceofbytes/34911a7b55f07631f398503960a2a02a to your computer and use it in GitHub Desktop.
Save sliceofbytes/34911a7b55f07631f398503960a2a02a to your computer and use it in GitHub Desktop.
Course events
import { Action } from '@ngrx/store';
import { Effect, Actions } from '@ngrx/effects';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import * as course from '../actions/course';
import { CourseService, Course } from '../../shared';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/startWith';
import { CourseCopyAction } from '../actions/course';
@Injectable()
export class CourseEffects {
constructor(private actions$: Actions, private service: CourseService) { }
@Effect()
courseLoadAll$: Observable<Action> = this.actions$
.ofType(course.COURSE_LOAD_ALL)
.startWith(new course.CourseLoadAllAction)
.switchMap(() => {
return this.service.getCourses()
.map((courses) => new course.CourseLoadAllSuccessAction({ courses: courses, error: undefined, loading: false }))
.catch((error) => of(new course.CourseLoadAllFailAction({ error: error, loading:false})));
});
@Effect()
courseCopy$: Observable<Action> = this.actions$
.ofType(course.COURSE_COPY)
.switchMap((action:CourseCopyAction) => {
return this.service.copyCourse(action.payload.group,action.payload.id,action.payload.name)
.map((crs: Course) => new course.CourseCopySuccessAction({ course: crs, error: undefined }))
.catch((error: Error) => of(new course.CourseCopyFailAction({ error: error })));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment