Ignore:
Timestamp:
10/22/21 21:47:17 (3 years ago)
Author:
Стојков Марко <mst@…>
Branches:
dev
Children:
466d1ac, 5ad5988
Parents:
81c2e6f (diff), 70e04f1 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merged feature/preview-questions-and-search into dev

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.effects.ts

    r81c2e6f r8b6791f  
    11import { Injectable } from '@angular/core';
    22import { Actions, createEffect, ofType } from '@ngrx/effects';
    3 import { catchError, switchMap } from 'rxjs/operators';
     3import { catchError, filter, switchMap, withLatestFrom } from 'rxjs/operators';
     4import { PreviewQuestionsOrderEnum } from 'src/app/shared-app/models';
     5import { TranslateFromJsonService } from 'src/app/shared-app/services';
    46
    57import { BaseApiService } from 'src/app/shared-app/services/base-api.service';
    6 import { QuestionStateResponse } from './question-state.models';
     8import { QuestionFacadeService } from '../question-facade.service';
     9import { PreviewQuestionResponse, QuestionStateResponse } from './question-state.models';
    710import {
    811  EffectFinishedWorking,
    912  EffectFinishedWorkingError,
     13  GetPreviewQuestionsLatestSuccess,
     14  GetPreviewQuestionsPopularSuccess,
    1015  GetQuestionState,
    1116  GetQuestionStateSuccess,
     
    1823})
    1924export class QuestionEffects {
    20   constructor(private actions$: Actions, private api: BaseApiService) {}
     25  constructor(
     26    private actions$: Actions,
     27    private api: BaseApiService,
     28    private translate: TranslateFromJsonService,
     29    private facade: QuestionFacadeService
     30  ) {}
    2131
    2232  getQuestionState$ = createEffect(() => {
     
    3141    );
    3242  });
     43
     44  getPreviewQuestionsLatest$ = createEffect(() => {
     45    return this.actions$.pipe(
     46      ofType<GetQuestionState>(QuestionActionTypes.GetPreviewQuestionsLatest),
     47      withLatestFrom(this.facade.getPreviewQuestionsLatest()),
     48      filter(([action, questions]) => questions.length === 0),
     49      switchMap((action) => {
     50        return this.api.get<PreviewQuestionResponse[]>(`v1/questions/preview?order=${PreviewQuestionsOrderEnum.Latest}`).pipe(
     51          switchMap((state) => [
     52            new GetPreviewQuestionsLatestSuccess(QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate)),
     53            new EffectFinishedWorking()
     54          ]),
     55          catchError((err) => [new EffectFinishedWorkingError(err)])
     56        );
     57      })
     58    );
     59  });
     60
     61  getPreviewQuestionsPopular$ = createEffect(() => {
     62    return this.actions$.pipe(
     63      ofType<GetQuestionState>(QuestionActionTypes.GetPreviewQuestionsPopular),
     64      withLatestFrom(this.facade.getPreviewQuestionsPopular()),
     65      filter(([action, questions]) => questions.length === 0),
     66      switchMap((action) => {
     67        return this.api.get<PreviewQuestionResponse[]>(`v1/questions/preview?order=${PreviewQuestionsOrderEnum.Popular}`).pipe(
     68          switchMap((state) => [
     69            new GetPreviewQuestionsPopularSuccess(QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate)),
     70            new EffectFinishedWorking()
     71          ]),
     72          catchError((err) => [new EffectFinishedWorkingError(err)])
     73        );
     74      })
     75    );
     76  });
    3377}
Note: See TracChangeset for help on using the changeset viewer.