I'm newbie to ngrx effects, I'm building Angular 5 application with ngrx store. I want to dispatch a action from first action to second action i.e. I have two actions getAlertFilters and getAlertArticles.
Also, How do I construct new object ArticleRequest from getFilters response and pass it as payload to getAlertArticles action. Please guide me on how to do this. Below is my code.
@Effect()
getAlertArticles$ = this.actions$
.ofType(NewsActions.GET_ALERT_ARTICLES_REQUEST)
.pipe(
map((action: NewsActions.GetAlertArticlesRequest) => action.payload),
switchMap((articlesRequest: ArticlesRequest ) => {
return this.newsService.getArticles(articlesRequest.channel, articlesRequest.filter, articlesRequest.locale, articlesRequest.page)
.pipe(
tap(() => this.store.dispatch(new LoaderActions.HideLoader(config.LOADER_ID))),
map(res => {
return {
type: NewsActions.GET_ALERT_ARTICLES_SUCCESS,
payload: res
};
}),
catchError((e: HttpErrorResponse) => Observable.of({
type: NewsActions.GET_ALERT_ERROR,
payload: e
}))
);
}),
);
@Effect()
getAlertFilters$ = this.actions$
.ofType(NewsActions.GET_ALERT_FILTER_REQUEST)
.pipe(
map((action: NewsActions.GetAlertFilterRequest) => action.payload),
switchMap((filterRequest: FiltersRequest ) => {
return this.newsService.getFilters(filterRequest.channel, filterRequest.locale)
.pipe(
tap((res) => {
this.store.dispatch(new NewsActions.GetAlertArticlesRequest({channel: res.getChannel, filter: 768, locale: 'en_ca' , page: 1}));
}),
map(res => {
return {
type: NewsActions.GET_ALERT_FILTER_SUCCESS,
payload: res
};
}),
catchError((e: HttpErrorResponse) => Observable.of({
type: NewsActions.GET_ALERT_ERROR,
payload: e
}))
);
}),
);
I think you can do like this:
NewsActions.GET_ALERT_ARTICLES_REQUEST and NewsActions.GET_ALERT_FILTER_REQUEST to be one, maybe NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUESTNewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUESTNewsActions.GET_ALERT_FILTER_SUCCESS action, you should save the needed param for the getAlertArticles$ effect to the store.getAlertArticles$ effect, add the following chain after the .ofType(NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUEST):Here is the code:
getAlertArticles$ = this.actions$
.ofType(NewsActions.GET_ALERT_FILTER_AND_ARTICLES_REQUEST)
.combineLatest(this.store.select(fromNews.getYourNeededData)
.filter(data => !isEmpty(data))
.pipe(
switchMap(([action, data]: [NewsActions.GetAlertFilterAndArticlesRequest, YourDataFormat]) => //do your request here),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With