source: src/Clients/Angular/finki-chattery/src/app/core/state/category-facade.service.ts@ 68d02ca

dev
Last change on this file since 68d02ca was 31c006c, checked in by Стојков Марко <mst@…>, 3 years ago

Add categories endpoint and state for categories, added questions preview component

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import { HttpErrorResponse } from '@angular/common/http';
2import { Injectable } from '@angular/core';
3import { Action, Store } from '@ngrx/store';
4import { Observable, throwError } from 'rxjs';
5import { catchError, filter, map } from 'rxjs/operators';
6
7import { CategoryStateViewModel } from 'src/app/shared-app/models';
8import { EffectStartedWorking, GetCategoriesState } from './category-state/category.actions';
9import { categoriesStateQuery } from './category-state/category.selectors';
10import { CategoryState } from './category-state/category.state';
11
12@Injectable({
13 providedIn: 'root'
14})
15export class CategoryFacadeService {
16 effectWorking$: Observable<boolean | HttpErrorResponse>;
17
18 constructor(private store: Store<CategoryState>) {
19 this.effectWorking$ = this.store.select(categoriesStateQuery.effectWorking).pipe(
20 filter((effect) => effect !== null),
21 map((effect) => {
22 if (effect instanceof HttpErrorResponse) {
23 throw effect;
24 } else {
25 return effect;
26 }
27 }),
28 catchError((err) => {
29 return throwError(err);
30 })
31 );
32 }
33
34 public getCategories(): Observable<CategoryStateViewModel[]> {
35 return this.store.select(categoriesStateQuery.getCategories);
36 }
37
38 public fetchCategories(): void {
39 this.dispatchEffect(new GetCategoriesState());
40 }
41
42 private dispatch(action: Action): void {
43 this.store.dispatch(action);
44 }
45
46 private dispatchEffect(action: Action): void {
47 this.dispatch(new EffectStartedWorking());
48 this.dispatch(action);
49 }
50}
Note: See TracBrowser for help on using the repository browser.