Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close dialog component from effect

I need to close angular material dialog from @ngrx/effect

here is my code

import { MatDialogRef, MatDialog } from "@angular/material/dialog";
import { AddComponent } from "./../../add/add.component";

@Injectable()
export class UserEffects {
@Effect()
addNewUser$ = this.actions$.pipe(
    ofType(actions.UserActionTypes.addNewUser),
         mergeMap((user: actions.addNewUser) =>
             this.userService.createUser(user.user).pipe(
                map(() => {
                  new actions.LoadUsers(),
                  this.notificationService.success("User added successfully!");
                  this.dialogRef.close();     <------ // here i try to close
                }),
              catchError(error => of(new actions.Error(error.error)))
            )
        )
    );

constructor(
    private actions$: Actions<actions.UserActions>,
    private userService: UserService,
    private notificationService: NotificationPopUpServiceService,
    public dialogRef: MatDialogRef<AddComponent>
) {}
}

And with this i get error

main.ts:13 NullInjectorError: R3InjectorError[EffectsRootModule -> InjectionToken ngrx/effects: Root Effects -> UserEffects -> MatDialogRef -> MatDialogRef -> MatDialogRef]: NullInjectorError: No provider for MatDialogRef!

What is the best way to close material-dialog from effect or from service? Because we always need to set a name for the dialog component?

Thank you

like image 839
Arter Avatar asked Jun 22 '26 15:06

Arter


1 Answers

I think the best solution to closing dialog is subscribing to the effect variable ie

// close the dialog
// Inside the dialog component
 this.userEffects.addNewUser$.pipe(
     ofType(Actions.LoadUsers)
 ).subscribe(_ => this.matDialogRef.close());
like image 93
john invictus Avatar answered Jun 25 '26 06:06

john invictus