Created
October 23, 2024 09:46
-
-
Save Armenvardanyan95/7f26917991790c8bfe5c18ecac9dcba1 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
| // bad actions lead to harder | |
| // to understand reducers/effects | |
| const loadTodos$ = createEffect(() => { | |
| const actions = inject(Actions); | |
| const todoService = inject(TodoService); | |
| return actions.pipe( | |
| // this sounds like a command | |
| // it gives us no idea about when | |
| // the todos will be loaded | |
| ofType(TodoActions.loadTodos), | |
| 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.deleteTodo), | |
| switchMap(todoId => todoService().deleteTodo(todoId).pipe( | |
| switchMap(todos => [ | |
| TodoActions.deleteTodoSuccess(todos), | |
| // we are forced to also | |
| // dispatch this other command | |
| // to refetch todos after deleting one | |
| // very tight coupling | |
| TodoActions.loadTodos(), | |
| ]), | |
| )), | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment