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 { AnswerQuestionRequest, RespondToAnswerRequest, VoteAnswerRequest } from './question-state-request.models';
|
---|
11 | import {
|
---|
12 | AnswerQuestionStateResponse,
|
---|
13 | AnswerResponseQuestionStateResponse,
|
---|
14 | PreviewQuestionResponse,
|
---|
15 | QuestionStateResponse,
|
---|
16 | VoteAnswerResponse
|
---|
17 | } from './question-state-response.models';
|
---|
18 | import {
|
---|
19 | AnswerQuestion,
|
---|
20 | AnswerQuestionSuccess,
|
---|
21 | DeleteAnswer,
|
---|
22 | DeleteAnswerResponse,
|
---|
23 | DeleteAnswerResponseSuccess,
|
---|
24 | DeleteAnswerSuccess,
|
---|
25 | EditAnswerQuestion,
|
---|
26 | EditAnswerQuestionSuccess,
|
---|
27 | EditAnswerResponse,
|
---|
28 | EditAnswerResponseSuccess,
|
---|
29 | EffectFinishedWorking,
|
---|
30 | EffectFinishedWorkingError,
|
---|
31 | GetPreviewQuestionsLatest,
|
---|
32 | GetPreviewQuestionsLatestSuccess,
|
---|
33 | GetPreviewQuestionsPopular,
|
---|
34 | GetPreviewQuestionsPopularSuccess,
|
---|
35 | GetQuestionState,
|
---|
36 | GetQuestionStateSuccess,
|
---|
37 | GetSearchQuestions,
|
---|
38 | GetSearchQuestionsSuccess,
|
---|
39 | QuestionActionTypes,
|
---|
40 | RespondToAnswer,
|
---|
41 | RespondToAnswerSuccess,
|
---|
42 | SetCorrectAnswer,
|
---|
43 | SetCorrectAnswerSuccess,
|
---|
44 | VoteAnswer,
|
---|
45 | VoteAnswerSuccess
|
---|
46 | } from './question.actions';
|
---|
47 | import { QuestionMapper } from './question.mapper';
|
---|
48 |
|
---|
49 | @Injectable({
|
---|
50 | providedIn: 'root'
|
---|
51 | })
|
---|
52 | export class QuestionEffects {
|
---|
53 | constructor(
|
---|
54 | private actions$: Actions,
|
---|
55 | private api: BaseApiService,
|
---|
56 | private translate: TranslateFromJsonService,
|
---|
57 | private facade: QuestionFacadeService,
|
---|
58 | private notification: NotificationService
|
---|
59 | ) {}
|
---|
60 |
|
---|
61 | getQuestionState$ = createEffect(() => {
|
---|
62 | return this.actions$.pipe(
|
---|
63 | ofType<GetQuestionState>(QuestionActionTypes.GetQuestionState),
|
---|
64 | switchMap((action) => {
|
---|
65 | return this.api.get<QuestionStateResponse>(`v1/questions/${action.questionUid}`).pipe(
|
---|
66 | switchMap((state) => [
|
---|
67 | new GetQuestionStateSuccess(QuestionMapper.ToQuestionStateViewModel(state, this.translate)),
|
---|
68 | new EffectFinishedWorking()
|
---|
69 | ]),
|
---|
70 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
71 | );
|
---|
72 | })
|
---|
73 | );
|
---|
74 | });
|
---|
75 |
|
---|
76 | getPreviewQuestionsLatest$ = createEffect(() => {
|
---|
77 | return this.actions$.pipe(
|
---|
78 | ofType<GetPreviewQuestionsLatest>(QuestionActionTypes.GetPreviewQuestionsLatest),
|
---|
79 | withLatestFrom(this.facade.getPreviewQuestionsLatest()),
|
---|
80 | filter(([action, questions]) => questions.length === 0),
|
---|
81 | switchMap((action) => {
|
---|
82 | return this.api.get<PreviewQuestionResponse[]>(`v1/questions/preview?order=${PreviewQuestionsOrderEnum.Latest}`).pipe(
|
---|
83 | switchMap((state) => [
|
---|
84 | new GetPreviewQuestionsLatestSuccess(QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate)),
|
---|
85 | new EffectFinishedWorking()
|
---|
86 | ]),
|
---|
87 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
88 | );
|
---|
89 | })
|
---|
90 | );
|
---|
91 | });
|
---|
92 |
|
---|
93 | getPreviewQuestionsPopular$ = createEffect(() => {
|
---|
94 | return this.actions$.pipe(
|
---|
95 | ofType<GetPreviewQuestionsPopular>(QuestionActionTypes.GetPreviewQuestionsPopular),
|
---|
96 | withLatestFrom(this.facade.getPreviewQuestionsPopular()),
|
---|
97 | filter(([action, questions]) => questions.length === 0),
|
---|
98 | switchMap((action) => {
|
---|
99 | return this.api.get<PreviewQuestionResponse[]>(`v1/questions/preview?order=${PreviewQuestionsOrderEnum.Popular}`).pipe(
|
---|
100 | switchMap((state) => [
|
---|
101 | new GetPreviewQuestionsPopularSuccess(QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate)),
|
---|
102 | new EffectFinishedWorking()
|
---|
103 | ]),
|
---|
104 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
105 | );
|
---|
106 | })
|
---|
107 | );
|
---|
108 | });
|
---|
109 |
|
---|
110 | getSearchQuestions$ = createEffect(() => {
|
---|
111 | return this.actions$.pipe(
|
---|
112 | ofType<GetSearchQuestions>(QuestionActionTypes.GetSearchQuestions),
|
---|
113 | mergeMap((action) => {
|
---|
114 | const categoriesAsString = action.categories !== null ? action.categories.join(',') : '';
|
---|
115 | return this.api
|
---|
116 | .get<PreviewQuestionResponse[]>(`v1/questions/search?searchText=${action.searchText}&categories=${categoriesAsString}`)
|
---|
117 | .pipe(
|
---|
118 | switchMap((state) => [
|
---|
119 | new GetSearchQuestionsSuccess(
|
---|
120 | QuestionMapper.ToPreviwQuestionsViewModel(state, this.translate),
|
---|
121 | new SearchQuestionsQueryViewModel(action.searchText)
|
---|
122 | ),
|
---|
123 | new EffectFinishedWorking()
|
---|
124 | ]),
|
---|
125 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
126 | );
|
---|
127 | })
|
---|
128 | );
|
---|
129 | });
|
---|
130 |
|
---|
131 | voteAnswer$ = createEffect(() => {
|
---|
132 | return this.actions$.pipe(
|
---|
133 | ofType<VoteAnswer>(QuestionActionTypes.VoteAnswer),
|
---|
134 | mergeMap((action) => {
|
---|
135 | const body = new VoteAnswerRequest(action.voteType);
|
---|
136 | return this.api.post<VoteAnswerResponse>(`v1/questions/${action.questionUid}/answers/${action.answerUid}/votes`, body).pipe(
|
---|
137 | tap((state) => this.notification.successNotification('sucess-vote')),
|
---|
138 | switchMap((state) => [new VoteAnswerSuccess(QuestionMapper.ToVoteAnswerViewModel(state)), new EffectFinishedWorking()]),
|
---|
139 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
140 | );
|
---|
141 | })
|
---|
142 | );
|
---|
143 | });
|
---|
144 |
|
---|
145 | setCorrectAnswer$ = createEffect(() => {
|
---|
146 | return this.actions$.pipe(
|
---|
147 | ofType<SetCorrectAnswer>(QuestionActionTypes.SetCorrectAnswer),
|
---|
148 | mergeMap((action) => {
|
---|
149 | return this.api.put<string>(`v1/questions/${action.questionUid}/answers/${action.answerUid}/correct`).pipe(
|
---|
150 | tap((state) => this.notification.successNotification('success-correct-answer')),
|
---|
151 | switchMap((state) => [new SetCorrectAnswerSuccess(state), new EffectFinishedWorking()]),
|
---|
152 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
153 | );
|
---|
154 | })
|
---|
155 | );
|
---|
156 | });
|
---|
157 |
|
---|
158 | respondToAnswer$ = createEffect(() => {
|
---|
159 | return this.actions$.pipe(
|
---|
160 | ofType<RespondToAnswer>(QuestionActionTypes.RespondToAnswer),
|
---|
161 | mergeMap((action) => {
|
---|
162 | return this.api
|
---|
163 | .post<AnswerResponseQuestionStateResponse>(
|
---|
164 | `v1/questions/${action.questionUid}/answers/${action.answerUid}/answerresponses`,
|
---|
165 | new RespondToAnswerRequest(action.text)
|
---|
166 | )
|
---|
167 | .pipe(
|
---|
168 | tap((state) => this.notification.successNotification('success-answer-response')),
|
---|
169 | switchMap((state) => [
|
---|
170 | new RespondToAnswerSuccess(QuestionMapper.ToAnswerResponseQuestionStateViewModel(state), action.answerUid),
|
---|
171 | new EffectFinishedWorking()
|
---|
172 | ]),
|
---|
173 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
174 | );
|
---|
175 | })
|
---|
176 | );
|
---|
177 | });
|
---|
178 |
|
---|
179 | answerQuestion$ = createEffect(() => {
|
---|
180 | return this.actions$.pipe(
|
---|
181 | ofType<AnswerQuestion>(QuestionActionTypes.AnswerQuestion),
|
---|
182 | mergeMap((action) => {
|
---|
183 | return this.api
|
---|
184 | .post<AnswerQuestionStateResponse>(`v1/questions/${action.questionUid}/answers`, new AnswerQuestionRequest(action.text))
|
---|
185 | .pipe(
|
---|
186 | tap((state) => this.notification.successNotification('success-answer')),
|
---|
187 | switchMap((state) => [
|
---|
188 | new AnswerQuestionSuccess(QuestionMapper.ToAnswerQuestionStateViewModel(state)),
|
---|
189 | new EffectFinishedWorking()
|
---|
190 | ]),
|
---|
191 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
192 | );
|
---|
193 | })
|
---|
194 | );
|
---|
195 | });
|
---|
196 |
|
---|
197 | editResponseToAnswer$ = createEffect(() => {
|
---|
198 | return this.actions$.pipe(
|
---|
199 | ofType<EditAnswerResponse>(QuestionActionTypes.EditAnswerResponse),
|
---|
200 | mergeMap((action) => {
|
---|
201 | return this.api
|
---|
202 | .put<AnswerResponseQuestionStateResponse>(
|
---|
203 | `v1/questions/${action.questionUid}/answers/${action.answerUid}/answerresponses/${action.answerResponseUid}`,
|
---|
204 | new RespondToAnswerRequest(action.text)
|
---|
205 | )
|
---|
206 | .pipe(
|
---|
207 | tap((state) => this.notification.successNotification('success-edit-answer-response')),
|
---|
208 | switchMap((state) => [
|
---|
209 | new EditAnswerResponseSuccess(
|
---|
210 | QuestionMapper.ToAnswerResponseQuestionStateViewModel(state),
|
---|
211 | action.answerUid,
|
---|
212 | action.answerResponseUid
|
---|
213 | ),
|
---|
214 | new EffectFinishedWorking()
|
---|
215 | ]),
|
---|
216 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
217 | );
|
---|
218 | })
|
---|
219 | );
|
---|
220 | });
|
---|
221 |
|
---|
222 | editAnswer$ = createEffect(() => {
|
---|
223 | return this.actions$.pipe(
|
---|
224 | ofType<EditAnswerQuestion>(QuestionActionTypes.EditAnswerQuestion),
|
---|
225 | mergeMap((action) => {
|
---|
226 | return this.api
|
---|
227 | .put<AnswerQuestionStateResponse>(
|
---|
228 | `v1/questions/${action.questionUid}/answers/${action.answerUid}`,
|
---|
229 | new AnswerQuestionRequest(action.text)
|
---|
230 | )
|
---|
231 | .pipe(
|
---|
232 | tap((state) => this.notification.successNotification('success-edit-answer')),
|
---|
233 | switchMap((state) => [
|
---|
234 | new EditAnswerQuestionSuccess(QuestionMapper.ToAnswerQuestionStateViewModel(state), action.answerUid),
|
---|
235 | new EffectFinishedWorking()
|
---|
236 | ]),
|
---|
237 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
238 | );
|
---|
239 | })
|
---|
240 | );
|
---|
241 | });
|
---|
242 |
|
---|
243 | deleteResponseToAnswer$ = createEffect(() => {
|
---|
244 | return this.actions$.pipe(
|
---|
245 | ofType<DeleteAnswerResponse>(QuestionActionTypes.DeleteAnswerResponse),
|
---|
246 | mergeMap((action) => {
|
---|
247 | return this.api
|
---|
248 | .delete<string>(`v1/questions/${action.questionUid}/answers/${action.answerUid}/answerresponses/${action.answerResponseUid}`)
|
---|
249 | .pipe(
|
---|
250 | tap((state) => this.notification.successNotification('success-delete-answer-response')),
|
---|
251 | switchMap((state) => [new DeleteAnswerResponseSuccess(state, action.answerUid), new EffectFinishedWorking()]),
|
---|
252 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
253 | );
|
---|
254 | })
|
---|
255 | );
|
---|
256 | });
|
---|
257 |
|
---|
258 | deleteAnswer$ = createEffect(() => {
|
---|
259 | return this.actions$.pipe(
|
---|
260 | ofType<DeleteAnswer>(QuestionActionTypes.DeleteAnswer),
|
---|
261 | mergeMap((action) => {
|
---|
262 | return this.api.delete<string>(`v1/questions/${action.questionUid}/answers/${action.answerUid}`).pipe(
|
---|
263 | tap((state) => this.notification.successNotification('success-delete-answer')),
|
---|
264 | switchMap((state) => [new DeleteAnswerSuccess(state), new EffectFinishedWorking()]),
|
---|
265 | catchError((err) => [new EffectFinishedWorkingError(err)])
|
---|
266 | );
|
---|
267 | })
|
---|
268 | );
|
---|
269 | });
|
---|
270 | }
|
---|