1 | import { Injectable } from '@angular/core';
|
---|
2 | import { act, Actions, createEffect, ofType } from '@ngrx/effects';
|
---|
3 | import { catchError, filter, mergeMap, switchMap, tap, withLatestFrom } from 'rxjs/operators';
|
---|
4 | import { PreviewQuestionsOrderEnum, SearchQuestionsQueryViewModel } from 'src/app/shared-app/models';
|
---|
5 | import { TranslateFromJsonService } from 'src/app/shared-app/services';
|
---|
6 |
|
---|
7 | import { BaseApiService } from 'src/app/shared-app/services/base-api.service';
|
---|
8 | import { NotificationService } from '../../services/notification.service';
|
---|
9 | import { QuestionFacadeService } from '../question-facade.service';
|
---|
10 | import { RespondToAnswerRequest, VoteAnswerRequest } from './question-state-request.models';
|
---|
11 | import {
|
---|
12 | AnswerResponseQuestionStateResponse,
|
---|
13 | PreviewQuestionResponse,
|
---|
14 | QuestionStateResponse,
|
---|
15 | VoteAnswerResponse
|
---|
16 | } from './question-state-response.models';
|
---|
17 | import {
|
---|
18 | EffectFinishedWorking,
|
---|
19 | EffectFinishedWorkingError,
|
---|
20 | GetPreviewQuestionsLatest,
|
---|
21 | GetPreviewQuestionsLatestSuccess,
|
---|
22 | GetPreviewQuestionsPopular,
|
---|
23 | GetPreviewQuestionsPopularSuccess,
|
---|
24 | GetQuestionState,
|
---|
25 | GetQuestionStateSuccess,
|
---|
26 | GetSearchQuestions,
|
---|
27 | GetSearchQuestionsSuccess,
|
---|
28 | QuestionActionTypes,
|
---|
29 | RespondToAnswer,
|
---|
30 | RespondToAnswerSuccess,
|
---|
31 | SetCorrectAnswer,
|
---|
32 | SetCorrectAnswerSuccess,
|
---|
33 | VoteAnswer,
|
---|
34 | VoteAnswerSuccess
|
---|
35 | } from './question.actions';
|
---|
36 | import { QuestionMapper } from './question.mapper';
|
---|
37 |
|
---|
38 | @Injectable({
|
---|
39 | providedIn: 'root'
|
---|
40 | })
|
---|
41 | export class QuestionEffects {
|
---|
42 | constructor(
|
---|
43 | private actions$: Actions,
|
---|
44 | private api: BaseApiService,
|
---|
45 | private translate: TranslateFromJsonService,
|
---|
46 | private facade: QuestionFacadeService,
|
---|
47 | private notification: NotificationService
|
---|
48 | ) {}
|
---|
49 |
|
---|
50 | getQuestionState$ = createEffect(() => {
|
---|
51 | return this.actions$.pipe(
|
---|
52 | ofType<GetQuestionState>(QuestionActionTypes.GetQuestionState),
|
---|
53 | switchMap((action) => {
|
---|
54 | return this.api.get<QuestionStateResponse>(`v1/questions/${action.questionUid}`).pipe(
|
---|
55 | switchMap((state) => [
|
---|
56 | new GetQuestionStateSuccess(QuestionMapper.ToQuestionStateViewModel(state, this.translate)),
|
---|
57 | new EffectFinishedWorking()
|
---|
58 | ]),
|
---|
59 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
60 | );
|
---|
61 | })
|
---|
62 | );
|
---|
63 | });
|
---|
64 |
|
---|
65 | getPreviewQuestionsLatest$ = createEffect(() => {
|
---|
66 | return this.actions$.pipe(
|
---|
67 | ofType<GetPreviewQuestionsLatest>(QuestionActionTypes.GetPreviewQuestionsLatest),
|
---|
68 | withLatestFrom(this.facade.getPreviewQuestionsLatest()),
|
---|
69 | filter(([action, questions]) => questions.length === 0),
|
---|
70 | switchMap((action) => {
|
---|
71 | return this.api.get<PreviewQuestionResponse[]>(`v1/questions/preview?order=${PreviewQuestionsOrderEnum.Latest}`).pipe(
|
---|
72 | switchMap((state) => [
|
---|
73 | new GetPreviewQuestionsLatestSuccess(QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate)),
|
---|
74 | new EffectFinishedWorking()
|
---|
75 | ]),
|
---|
76 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
77 | );
|
---|
78 | })
|
---|
79 | );
|
---|
80 | });
|
---|
81 |
|
---|
82 | getPreviewQuestionsPopular$ = createEffect(() => {
|
---|
83 | return this.actions$.pipe(
|
---|
84 | ofType<GetPreviewQuestionsPopular>(QuestionActionTypes.GetPreviewQuestionsPopular),
|
---|
85 | withLatestFrom(this.facade.getPreviewQuestionsPopular()),
|
---|
86 | filter(([action, questions]) => questions.length === 0),
|
---|
87 | switchMap((action) => {
|
---|
88 | return this.api.get<PreviewQuestionResponse[]>(`v1/questions/preview?order=${PreviewQuestionsOrderEnum.Popular}`).pipe(
|
---|
89 | switchMap((state) => [
|
---|
90 | new GetPreviewQuestionsPopularSuccess(QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate)),
|
---|
91 | new EffectFinishedWorking()
|
---|
92 | ]),
|
---|
93 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
94 | );
|
---|
95 | })
|
---|
96 | );
|
---|
97 | });
|
---|
98 |
|
---|
99 | getSearchQuestions$ = createEffect(() => {
|
---|
100 | return this.actions$.pipe(
|
---|
101 | ofType<GetSearchQuestions>(QuestionActionTypes.GetSearchQuestions),
|
---|
102 | mergeMap((action) => {
|
---|
103 | const categoriesAsString = action.categories !== null ? action.categories.join(',') : '';
|
---|
104 | return this.api
|
---|
105 | .get<PreviewQuestionResponse[]>(`v1/questions/search?searchText=${action.searchText}&categories=${categoriesAsString}`)
|
---|
106 | .pipe(
|
---|
107 | switchMap((state) => [
|
---|
108 | new GetSearchQuestionsSuccess(
|
---|
109 | QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate),
|
---|
110 | new SearchQuestionsQueryViewModel(action.searchText)
|
---|
111 | ),
|
---|
112 | new EffectFinishedWorking()
|
---|
113 | ]),
|
---|
114 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
115 | );
|
---|
116 | })
|
---|
117 | );
|
---|
118 | });
|
---|
119 |
|
---|
120 | voteAnswer$ = createEffect(() => {
|
---|
121 | return this.actions$.pipe(
|
---|
122 | ofType<VoteAnswer>(QuestionActionTypes.VoteAnswer),
|
---|
123 | mergeMap((action) => {
|
---|
124 | const body = new VoteAnswerRequest(action.voteType);
|
---|
125 | return this.api.post<VoteAnswerResponse>(`v1/questions/${action.questionUid}/answers/${action.answerUid}/votes`, body).pipe(
|
---|
126 | tap((state) => this.notification.successNotification('sucess-vote')),
|
---|
127 | switchMap((state) => [new VoteAnswerSuccess(QuestionMapper.ToVoteAnswerViewModel(state)), new EffectFinishedWorking()]),
|
---|
128 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
129 | );
|
---|
130 | })
|
---|
131 | );
|
---|
132 | });
|
---|
133 |
|
---|
134 | setCorrectAnswer$ = createEffect(() => {
|
---|
135 | return this.actions$.pipe(
|
---|
136 | ofType<SetCorrectAnswer>(QuestionActionTypes.SetCorrectAnswer),
|
---|
137 | mergeMap((action) => {
|
---|
138 | return this.api.put<string>(`v1/questions/${action.questionUid}/answers/${action.answerUid}/correct`).pipe(
|
---|
139 | tap((state) => this.notification.successNotification('success-correct-answer')),
|
---|
140 | switchMap((state) => [new SetCorrectAnswerSuccess(state), new EffectFinishedWorking()]),
|
---|
141 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
142 | );
|
---|
143 | })
|
---|
144 | );
|
---|
145 | });
|
---|
146 |
|
---|
147 | respondToAnswer$ = createEffect(() => {
|
---|
148 | return this.actions$.pipe(
|
---|
149 | ofType<RespondToAnswer>(QuestionActionTypes.RespondToAnswer),
|
---|
150 | mergeMap((action) => {
|
---|
151 | return this.api
|
---|
152 | .post<AnswerResponseQuestionStateResponse>(
|
---|
153 | `v1/questions/${action.questionUid}/answers/${action.answerUid}/answerresponses`,
|
---|
154 | new RespondToAnswerRequest(action.text)
|
---|
155 | )
|
---|
156 | .pipe(
|
---|
157 | tap((state) => this.notification.successNotification('success-answer-response')),
|
---|
158 | switchMap((state) => [
|
---|
159 | new RespondToAnswerSuccess(QuestionMapper.ToAnswerResponseQuestionStateViewModel(state), action.answerUid),
|
---|
160 | new EffectFinishedWorking()
|
---|
161 | ]),
|
---|
162 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
163 | );
|
---|
164 | })
|
---|
165 | );
|
---|
166 | });
|
---|
167 | }
|
---|