Created
October 23, 2024 09:47
-
-
Save Armenvardanyan95/d793e4df9d83283d6ceeb43cb9843abe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // great actions allow us to fully | |
| // understand how data changes and depends | |
| // on each other in our applciation | |
| const loadTodos$ = createEffect(() => { | |
| const actions = inject(Actions); | |
| const todoService = inject(TodoService); | |
| return actions.pipe( | |
| // this tells us all the situations | |
| // when a todo list must be refetched | |
| // according to the business logic | |
| ofType( | |
| TodoActions.todoListComponentLoaded, | |
| TodoActions.todoSuccessfullyDeleted, | |
| ), | |
| switchMap(() => todoService().getTodos().pipe( | |
| map(todos => TodoActions.loadTodosSuccess(todos)), | |
| )), | |
| ); | |
| }, {functional: true}); | |
| const deleteTodo$ = createEffect(() => { | |
| const actions = inject(Actions); | |
| const todoService = inject(TodoService); | |
| return actions.pipe( | |
| ofType(TodoActions.todoDeletion), | |
| switchMap(todoId => todoService().deleteTodo(todoId).pipe( | |
| // here, we only have to notify the application | |
| // that a todo has been successfully deleted | |
| // then other parts of the app can react accordingly | |
| // no tight coupling | |
| map(todos => TodoActions.todoSuccessfullyDeleted(todos)), | |
| )), | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment