1
votes

I am using ngrx-store in my project. I got a reducer like belom with handle two different actions. The payload of the action is number of task count. I would like to sum up the number from both of the actions when value emitted.

In my component, I dispatch the actions separately. So the emitted value would be log asynchronously.

How do I add the result from the two emitted value?

export function countTaskReducer(state = {},
                                        action: CountAssignedSuccessAction | CountPooledSuccessAction) {
    switch (action.type) {
        case COUNT_ASSIGNED_REQUISITIONS_SUCCESS:
            return {...state, ...action.payload};
        case COUNT_POOLED_REQUISITIONS_SUCCESS:
            return {...state, ...action.payload};
        default: {
            return state;
        }
    }
}


export class CountAssignedSuccessAction implements Action {
    readonly type: string = COUNT_ASSIGNED_SUCCESS;

    constructor(public payload: {count:number}) {
    }
}

export class CountPooledSuccessAction implements Action {
    readonly type: string = COUNT_POOLED_SUCCESS;

    constructor(public payload: {count:number}) {
    }
}
1

1 Answers

0
votes

is this what you want?

{...state, count: state.count+ action.payload.count}