What is the recommended approach for implementing if/else logic in ngrx/effects?

In my project, I am utilizing ngrx/effects. The goal is to dispatch different actions depending on the value of a foo state in the store.

This is my current approach:

@Effect() foo1$ = this.updates$
    .whenAction(Actions.FOO)
    .filter(obj => !obj.state.product.foo)
    .map<string>(toPayload)
    .map(x => ({ type: Actions.BAR1, payload: { x }}));

@Effect() foo2$ = this.updates$
    .whenAction(Actions.FOO)
    .filter(obj => obj.state.product.foo)
    .map<string>(toPayload)
    .map(x => ({ type: Actions.BAR2, payload: { x }}));

Is there a way to utilize RxJS 5 operators such as partition, groupBy, if, case following the example in this post? For some reason, I am having trouble implementing them effectively at the moment.

Answer №1

It is perfectly acceptable to have two separate sources for effects. Alternatively, you can simplify it as follows:

@Effect() bar$ = this.changes$
    .whenEvent(Events.BAR)
    .map(({event, state}) => {
        if (state.product.bar) {
            return { type: Events.CHAT_SEND_MESSAGES2, payload: { event.payload }};
        } else {
            return { type: Events.CHAT_SEND_MESSAGES, payload: { event.payload }};
        }
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmitting information from a modal component to its parent component in Angular using MDB

I encountered an issue with my CRUD application where uploaded files do not immediately appear on the page until I refresh it. The same problem occurs when trying to delete a file. For deleting, I was able to fix it by subscribing to the onClose event on ...

What triggers the execution of the catch method while chaining HTTP promises?

In my Angular application, I have developed a custom loaderService that displays a loading spinner while the back-end processes requests. Below is the code snippet: export class ComponentOne { constructor(loaderService: LoaderService, orderService: Orde ...

Are AngularJS Material Components compatible with Angular2?

Recently, I inherited a web app project from a former colleague who has since moved on from our company. The web app was originally built using Angular2 as its framework, but it seems that the previous developer attempted to incorporate AngularJS Material ...

Sharing Angular classes and components between a shell and Micro Frontends using module federation

Currently, I am experimenting with a module federation proof of concept in Angular, taking inspiration from Manfred's example. My goal is to figure out a way to share utility classes, directives, individual components, and any other code that does not ...

What strategies can I use to steer clear of the pyramid of doom when using chains in fp-ts?

There are times when I encounter a scenario where I must perform multiple operations in sequence. If each operation relies solely on data from the previous step, then it's simple with something like pipe(startingData, TE.chain(op1), TE.chain(op2), TE. ...

Testing useEffect with React hooks, Jest, and Enzyme to add and remove event listeners on a ref

Here is a component I've been working on: export const DeviceModule = (props: Props) => { const [isTooltipVisible, changeTooltipVisibility] = useState(false) const deviceRef = useRef(null) useEffect(() => { if (deviceRef && dev ...

Searching in an empty array using Typescript and the includes method

I want to retrieve data based on the IDs in an array. If the array is empty, I want all the data to be fetched. How can I achieve this? selectedProductGroups: number[] = []; products: Product[]; getProducts() { this.generic?.Get_All("Products/Gener ...

When using Vue with Vuetify, be aware that object literals can only specify known properties. In this case, the type 'vuetify' does not exist in the ComponentOptions of Vue with DefaultData

Started a fresh Vue project with TypeScript by following this guide: https://v2.vuejs.org/v2/guide/typescript.html If you don't have it installed yet, install Vue CLI using: npm install --global @vue/cli Create a new project and choose the "Manual ...

Dealing with the possibility of an empty array when accessing elements by index in Typescript

What is the best way to handle accessing elements by index in an array in Typescript when the array can be empty, resulting in potentially undefined elements? I am developing a simple game using React and Typescript where I have a variable named game whic ...

Ways to integrate object-or-array with recursion in Typescript

I am striving to achieve the following: type Foo = ({bar: string} & Record<string, Foo>) | Foo[] However, I keep encountering issues such as circular references in type or the constraint that An interface can only extend an object type or inte ...

Tips for creating a nodeCategoryProperty function for a TypeScript Model:

nodeCategoryProperty function has a signature requirement of (a: ObjectData, b?: string) => string. In my opinion, this should be updated to (a: ObjectData, b?: string) => string | void, as the function is intended to not return anything if used as a ...

The error seems to vanish when commenting out the following div structure

Since upgrading to Angular 5, I've been encountering the following error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: undefined'. Current value: 'ngIf: null&apo ...

User interface for dynamically generated elements using Typescript with React

Looking to create a translator hook that can pull language json files based on the selected language and return a portion of this large object depending on the arguments provided. How can I create an interface for an object that has been dynamically create ...

Struggling to update minimist in Angular 9?

Lately, I've been working on a project using Angular 9 and came across a warning about a security vulnerability related to the minimist package. Despite my efforts to fix it with "(sudo) npm audit fix" or updating with "(sudo) npm update", the issues ...

Can you explain the distinction between angular-highcharts and highcharts-angular?

Our team has been utilizing both angular-highcharts and highcharts-angular in various projects. It appears that one functions as a directive while the other serves as a wrapper. I'm seeking clarification on the distinctions between the two and recomme ...

Error message: Angular 2 JsonpModule import not defined

Within my app.module.ts file, I have the specified code import {NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {JsonpModule} from '@angular/http'; import {AppComponent} from & ...

What is the best way to incorporate master/detail components without relying on hierarchical routes?

I need to combine the following elements. An index page with the route /items (plural). This page should be scrollable. When clicking on individual items, I want to display a detail page with the route /item/:id (singular). The detail page should have a ...

Angular 5 - Issue with ngModel not binding correctly to tinymce editor

I have successfully integrated TinyMCE into my Angular 5 project, but I am facing an issue with data binding. How can I achieve data binding with TinyMCE in my Angular 5 project? Specifically, I am trying to bind the TinyMCE text editor with ngModel, but ...

Analyzing bundle files with Angular CLI output

Currently, I am utilizing Angular CLI to develop a production app by using the --prod switch. The resulting bundle is generated in the dist directory. Is there a method to determine which classes and functions have been included in the final bundle after ...

How can I retrieve the access token from the Id_token using Azure AD Authentication Library (ADAL in angular 5)?

Within my Angular 5 project, I have incorporated the adal service for Microsoft auth2.0 authentication. When retrieving the id_token, I utilize this.adalService.getCachedToken(this.secretService.adalConfig.clientId); However, I require an Access Token to ...