Angular/general front-end - should components "open a dialog"?

Viewed 39

Assuming MatDialog or any "dialog opening service" implementation, where a dialog is opened with dialogService.open, would the "dialog opening logic" be suited in the component itself, or in a Store library (NGRX) effect?

component.ts

foo(): void {
  this.dialogService.openDialog(BarDialog, params)
}

vs

component.ts

foo(): void {
  this.store.dispatchAction(OpenBarDialog, params)
}

and the Effect would have the "dialog opening logic". My intuition is that "opening a dialog is a side effect".

1 Answers

I don’t think there is a specific single answer to this question, it all comes down to what you need. Here’s my two cent that I absolutely mean as a matter to discuss about (!)

Having this logic in an effect makes this a lot easier to refactor and reuse eventually. If the „answer“ to this dialog (reacting to eg close-action) however is only meant to be consumed by the component you started to open the dialog with, then why abstract everything into an effect in the first place?

On the other hand if you go all the way with NGRX you eventually end up with code that’s a lot more readable and reasonable.

Related