-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f540f0
commit 9096223
Showing
2 changed files
with
72 additions
and
56 deletions.
There are no files selected for viewing
126 changes: 71 additions & 55 deletions
126
YeProfspilka.Admin/src/app/store/effects/question.effect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,83 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ToastrService, } from 'ngx-toastr'; | ||
import { catchError, EMPTY, exhaustMap, map, mergeMap, of, } from 'rxjs'; | ||
import { QuestionService, } from 'src/app/services/question.service'; | ||
|
||
import { Actions, createEffect, ofType } from '@ngrx/effects'; | ||
import { ToastrService } from 'ngx-toastr'; | ||
import { catchError, exhaustMap, map, mergeMap, of } from 'rxjs'; | ||
|
||
import { QuestionService } from 'src/app/services/question.service'; | ||
import { Injectable, } from '@angular/core'; | ||
import { Actions, createEffect, ofType, } from '@ngrx/effects'; | ||
|
||
import { | ||
createQuestion, | ||
createQuestionSuccess, | ||
deleteQuestion, | ||
deleteQuestionSuccess, | ||
fetchQuestions, | ||
fetchQuestionsSuccess, | ||
updateQuestion, | ||
updateQuestionSuccess, | ||
createQuestion, createQuestionSuccess, deleteQuestion, deleteQuestionSuccess, fetchQuestions, | ||
fetchQuestionsSuccess, updateQuestion, updateQuestionSuccess, | ||
} from '../actions/questions.action'; | ||
|
||
@Injectable() | ||
export class QuestionEffect { | ||
constructor(private actions$: Actions, private questionService: QuestionService, private toastrService: ToastrService) {} | ||
constructor( | ||
private actions$: Actions, | ||
private questionService: QuestionService, | ||
private toastrService: ToastrService | ||
) {} | ||
|
||
fetchQuestions$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(fetchQuestions), | ||
exhaustMap(() => this.questionService.getAll().pipe(map((questions) => fetchQuestionsSuccess({ questions })))) | ||
) | ||
); | ||
fetchQuestions$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(fetchQuestions), | ||
exhaustMap(() => | ||
this.questionService | ||
.getAll() | ||
.pipe(map((questions) => fetchQuestionsSuccess({ questions }))) | ||
) | ||
) | ||
); | ||
|
||
createQuestion$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(createQuestion), | ||
mergeMap(({ question }) => | ||
this.questionService | ||
.create({ | ||
answer: question.answer, | ||
questionText: question.questionText, | ||
}) | ||
.pipe( | ||
map(() => { | ||
this.toastrService.success('Питання успішно створено!'); | ||
return createQuestionSuccess({ question }); | ||
}), | ||
catchError(() => { | ||
this.toastrService.success('Питання успішно створено!'); | ||
return of(undefined); | ||
}) | ||
) | ||
) | ||
) | ||
); | ||
createQuestion$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(createQuestion), | ||
mergeMap(({ question }) => | ||
this.questionService | ||
.create({ | ||
answer: question.answer, | ||
questionText: question.questionText, | ||
}) | ||
.pipe( | ||
map(() => { | ||
this.toastrService.success('Питання успішно створено!'); | ||
return createQuestionSuccess({ question }); | ||
}), | ||
catchError(() => { | ||
this.toastrService.success('Питання успішно створено!'); | ||
return of(undefined); | ||
}) | ||
) | ||
) | ||
) | ||
); | ||
|
||
updateQuestion$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(updateQuestion), | ||
exhaustMap(({ question }) => this.questionService.update(question).pipe(map((question) => updateQuestionSuccess({ question })))) | ||
) | ||
); | ||
updateQuestion$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(updateQuestion), | ||
exhaustMap(({ question }) => | ||
this.questionService | ||
.update(question) | ||
.pipe(map((question) => updateQuestionSuccess({ question }))) | ||
) | ||
) | ||
); | ||
|
||
deleteQuestion$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(deleteQuestion), | ||
exhaustMap(({ id }) => this.questionService.delete(id).pipe(map(() => deleteQuestionSuccess({ id })))) | ||
) | ||
); | ||
deleteQuestion$ = createEffect(() => | ||
this.actions$.pipe( | ||
ofType(deleteQuestion), | ||
exhaustMap(({ id }) => | ||
this.questionService.delete(id).pipe( | ||
map(() => { | ||
this.toastrService.success('Успішно видалено'); | ||
return deleteQuestionSuccess({ id }); | ||
}), | ||
catchError((err) => { | ||
this.toastrService.success('Щось пішло не так!'); | ||
return EMPTY; | ||
}) | ||
) | ||
) | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters