Created
April 20, 2018 15:12
-
-
Save sliceofbytes/8bfeafd421e93a3156909a6d8ace3316 to your computer and use it in GitHub Desktop.
Course events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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