@Kliment Ru @bygrace , , snackbars (Material) .
message.action.ts
import { Action } from '@ngrx/store';
export const MESSAGE = '[Messages] Show Message';
export class Message implements Action {
readonly type = MESSAGE;
constructor(
public payload: {
message: string;
action?: string;
duration?: number;
callback?: Function;
}
) { }
}
, snackbar ngrx.
message.effect.ts
import { Injectable } from '@angular/core';
import { Effect, Actions } from '@ngrx/effects';
import * as MessageActions from '../actions/message.action';
import { tap, map } from 'rxjs/operators';
import { MatSnackBar } from '@angular/material';
import { first } from 'rxjs/operators/first';
@Injectable()
export class MessageEffects {
constructor(
private actions$: Actions,
private snackBar: MatSnackBar
) { }
@Effect({ dispatch: false })
navigate$ = this.actions$
.ofType(MessageActions.MESSAGE)
.pipe(
map((action: MessageActions.Message) => action.payload),
tap(({ message, action, duration, callback }) => {
duration = duration ? duration : 3000;
// incase of an action assigned, subscribe to the snackbar, else just show the message
if (callback) {
this.snackBar.open(message, action, { duration: duration })
.onAction()
.pipe(
first()
)
.subscribe(() => {
callback();
});
} else {
this.snackBar.open(message, action, { duration: duration });
}
}));
}
, .
, , :
this.store.dispatch(new fromRoot.Message({ message: 'Something went wrong, please try again later' }));
a simple one-line interface that encapsulates all the logic and user interface of messages in your application, the good thing behind it is that I can change my snackbar to whatever I want using any library, and I should change the code only in one place.
source
share