source: src/Clients/Angular/finki-chattery/src/app/core/state/question-facade.service.ts@ dab7a9b

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

Preview question component

  • Property mode set to 100644
File size: 1.6 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 { QuestionStateViewModel } from 'src/app/shared-app/models';
8import { EffectStartedWorking, GetQuestionState } from './question-state/question.actions';
9import { questionStateQuery } from './question-state/question.selectors';
10import { QuestionState } from './question-state/question.state';
11
12@Injectable({
13 providedIn: 'root'
14})
15export class QuestionFacadeService {
16 effectWorking$: Observable<boolean | HttpErrorResponse>;
17
18 constructor(private store: Store<QuestionState>) {
19 this.effectWorking$ = this.store.select(questionStateQuery.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 getQuestion(): Observable<QuestionStateViewModel> {
35 return this.store
36 .select(questionStateQuery.getQuestion)
37 .pipe(filter((x: QuestionStateViewModel | null): x is QuestionStateViewModel => x !== null));
38 }
39
40 public fetchQuestion(questionUid: string): void {
41 this.dispatchEffect(new GetQuestionState(questionUid));
42 }
43
44 private dispatch(action: Action): void {
45 this.store.dispatch(action);
46 }
47
48 private dispatchEffect(action: Action): void {
49 this.dispatch(new EffectStartedWorking());
50 this.dispatch(action);
51 }
52}
Note: See TracBrowser for help on using the repository browser.