Exercise: Dialog
To begin, checkout the 05-effects-exercise-1 branch:
git checkout 05-effects-exercise-1
🤫 The solution branch is available if you get stuck.
You can checkout the solution via:
git checkout 05-effects-exercise-1-solution
Goals
- Practice declaring asynchronous actions.
- Practice declaring a reducer function.
- Practice declaring projector functions and selectors.
- Practice declaring an effects class.
- Practice selecting a slice of data from the state object.
- Practice dispatching actions.
Steps
- Create a new directory at src/app/state/dialog.
- Create a new file at src/app/state/dialog/dialog.actions.ts.
- Declare the following actions:
CloseDialogs
andOpenDialog
. TheOpenDialog
action should require aComponentType
and an optionalMatDialogConfig
object in theconstructor()
. TheCloseDialogs
action will close all of the open dialogs. - Declare the
reducer()
function in a new file at src/app/state/dialog/dialog.reducer.ts and update anopened
boolean property of the current feature state. - Declare the
DialogEffects
class and create two effects toopen
andclose
the dialog in src/app/state/dialog/dialog/effects.ts. - Add the
DialogEffects
class to the array of effects defined in theEffectsModule.forRoot()
static method in src/app/state/state.module.ts. - Add the
dialog
feature state in src/app/state/index.ts. - Update the
ShellComponent.openDialog()
method in src/app/core/shell/shell.component.ts to dispatch theOpenDialog
action.
Hints
- While we are updating an
opened
boolean value in state, we're not going to create any selectors at this time. We could later use this value to determine the application's behavior when a dialog is opened (or not). - Use the
{ dispatch: false }
option in the@Effect()
decorator to prevent the effect from recursively dispatching the source action. - Inject the
MatDialog
service to open and close dialogs. Refer to the Angular Material documentation for the Dialog. - Use the
ofType()
operator to filter for a specific action. - Use the
tap()
operator to perform a side effect in the sequence of operators in the observablepipe()
method.
Try it Out
Use the Redex Devtools to view the [Dialog] Open Dialog action being dispatched: