Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGRX Store adapter.updateone not changing properties for change detection

Tags:

angular

ngrx

I have the following reducer:

case ProductionCommentActionTypes.EditCommentSuccess: {
  return adapter.updateOne(action.payload, state);
}

where the payload is the following object:

{ 
    id: number,
    changes: { 
       id: number;
       avatar: string;
       createdAt: Date;
       createdBy: string;
       body: string;
       discipline: string;
       isConcern: boolean;
       isResolved: boolean;
       plan_Id: number;
       resolution: {
                    id: number;
                    comment_Id: number;
                    avatar: string;
                    createdAt: Date;
                    createdBy: string;
                    body: string;   
                   }
      }
}

Unfortunately, when I go to update the object, I can plainly see action.payload has exactly the data im annticipating. Ive checked both action.payload, and the effect:

  @Effect()
  resolveProductionConcernSuccess$: Observable<Action> = this.actions$
  .ofType<ResolveConcernSuccess>(ProductionCommentActionTypes.ResolveConcernSuccess).pipe(
    // Performing a get rather than an edit as the comment should have already 
    // been updated in the service when the resolution was added.
    switchMap(action => this.commentService.getComment(action.payload.comment_Id)), 
          map((comment: any) => {
            comment.updatedBy = "test";
        return new EditCommentSuccess(comment);
      }),
    catchError(err => of(new EditCommentFail(err)))
  );

However, what i see from this:

  this.comments$ = rootStore.select<Comment[]>(fromRoot.getCommentsForSelectedPlan);
this.comments$.subscribe(res => console.log(res));

Is the old data, even though a change is triggered. I would have at least expected to see updatedBy = "test". I also see the old data on state immediately after the updateOne if i step through in the debugger.

Another way to say it is

      return adapter.updateOne(action.payload, state);

Doesn't seem to actually change anything despite having the correct values in the payload.

Why are the properties on the state not actually updated? Is there a proper way to do that?

like image 654
Seth Avatar asked Jan 28 '26 15:01

Seth


1 Answers

The problem was in the reducer.

return adapter.updateOne({
  id: action.payload.id,
  changes: action.payload
},
  state);
like image 170
Seth Avatar answered Jan 30 '26 12:01

Seth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!