source: src/Clients/Angular/finki-chattery/src/app/core/state/question-state/question.effects.ts@ 9df3069

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

Edit answer and answer response

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