Consider the following case: A modal must be closed after an action succeeds. Would this be a viable solution:
@Component({
selector: 'user-delete-dialog',
templateUrl: './user-delete-dialog.component.html',
styleUrls: ['./user-delete-dialog.component.scss']
})
export class UserDeleteDialogComponent implements OnInit {
@Input() user: User;
constructor(
public dialog: NgbActiveModal,
private store: Store
) { }
ngOnInit(): void {
}
confirm() {
this.store.dispatch(
deleteUser({
id: this.user.id,
onComplete: () => {
this.dialog.close();
},
onError: () => {
// Show errors ...
}
})
);
}
}
Another way would be subscribing to the main Actions stream, but listening to a context specific action globally just for the lifetime of the modal feels not right:
[...]
import { Actions } from '@ngrx/effects';
[...]
@Component(...)
class SomeComponent implements OnDestroy {
destroyed$ = new Subject<boolean>();
constructor(updates$: Actions) {
updates$.pipe(
ofType(UserActions.DELETE_SUCCESS),
takeUntil(this.destroyed$)
)
.subscribe(() => {
// close modal
});
}
ngOnDestroy() {
this.destroyed$.next(true);
this.destroyed$.complete();
}
confirm() {
this.store.dispatch(deleteUser({ id: this.user.id }));
}
}
Any ideas?