Skip to content

Instantly share code, notes, and snippets.

@mayrascript
Created December 18, 2019 01:59
Show Gist options
  • Select an option

  • Save mayrascript/a59a30027120125dbc0e73f41290d53e to your computer and use it in GitHub Desktop.

Select an option

Save mayrascript/a59a30027120125dbc0e73f41290d53e to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { CourseModel } from 'src/app/dashboard/shared/models/course.model';
import { StoreService } from 'src/app/core/services/store/store.service';
import { actions } from 'src/app/core/services/store/actions';
import { select } from 'src/app/core/services/store/operators';
import { Observable } from 'rxjs';
import { AppState } from 'src/app/core/services/store/app-state';
import { tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class CoursesStoreService {
static count = 0;
constructor(private storeService: StoreService<AppState>) {
}
getCourses(): Observable<CourseModel[]> {
return this.storeService
.pipe(
select('courses'),
tap(console.log)
);
}
getCurrentCourse(): Observable<CourseModel> {
return this.storeService
.pipe(
select('currentCourse'),
tap((currentCourse) => console.log('currentCourse', currentCourse))
);
}
addCourse(course: CourseModel) {
CoursesStoreService.count += 1;
course = {...course, id: CoursesStoreService.count, isPublished: false};
this.storeService.dispatch({type: actions.add, payload: {course}});
}
updateCourse(course: CourseModel) {
this.storeService.dispatch({type: actions.update, payload: {course}});
}
remove(courseId: string) {
this.storeService.dispatch({type: actions.remove, payload: {courseId}});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment