Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close modals in project that uses NgRX to manage state

Tags:

angular

ngrx

Is there any good solution when working with modals in a project where state is managed by NgRX?

The problem I have the following:

  • User clicks on button to create a new item.
  • Modal window opens.
  • User fills out the form and clicks on Submit.
  • Dispatch an action, effects fires up and creates an item by sending HTTP request.
  • Now I need to close modal on success. How do I know when to close it either inside modal component or inside component that opened this modal?

One of the solutions I see right now is to return multiple actions from the effect, one that adds created item to the store, second one to close the modal. For this I should include some modal identifier in "Create" action to identify which modal to close after "Create$" effect completes, but this will make effect more complicated.

I can't believe there is no ready solution to this problem.

I use ngx-bootstrap for modals.

UPDATE: Seems like to make this work I need to store (state + reducer + effects + actions) open/closed status for each modal available in the application. But anyway, no existing solution?

like image 316
Vladimir Prudnikov Avatar asked Oct 27 '19 10:10

Vladimir Prudnikov


People also ask

How do you close a modal component?

To close the modal, simply call the handleClose() function inside the onLoginFormSubmit() function body.

What are the benefits of using an NgRx store to manage state in an Angular app?

The NgRx Store is a Redux-inspired state management system that enables you to use observables to manage state in an Angular application. The primary advantage to using the NgRx Store is the ability to store all state in a single tree that is accessible from any part of the application.

Why use NgRx?

NgRx implements the Flux-Pattern. At a high level, it helps you unify all events and derive a common state in your Angular app. With NgRx, you store a single state and use actions to express state changes. It is ideal for apps with many user interactions and multiple data sources.

What is NgRx state?

NgRx is a framework for building reactive applications in Angular. NgRx is inspired by the Redux pattern - unifying the events in your application and deriving state using RxJS. At a high level, NgRx stores a single state and uses actions to express state changes.


1 Answers

I like to use an effect to handle the whole dialog flow, from opening the dialog to saving the entity and closing the dialog afterwards.

@Effect()
openDialog = this.actions.pipe(
  ofType(LoginActionTypes.OpenLoginDialog),
  exhaustMap(_ => {
    let dialogRef = this.dialog.open(LoginDialog);
    return dialogRef.afterClosed();
  }),
  map((result: any) => {
    if (result === undefined) {
      return new CloseDialog();
    }
    return new LoginDialogSuccess(result);
  }),
);

See Start using ngrx/effects for this for more info.

like image 65
timdeschryver Avatar answered Nov 14 '22 23:11

timdeschryver