Skip to content

Instantly share code, notes, and snippets.

@Armenvardanyan95
Created October 23, 2024 09:47
Show Gist options
  • Save Armenvardanyan95/d793e4df9d83283d6ceeb43cb9843abe to your computer and use it in GitHub Desktop.
Save Armenvardanyan95/d793e4df9d83283d6ceeb43cb9843abe to your computer and use it in GitHub Desktop.
// 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