Search code examples
angularngrx-store

How to load data without overwrite?


in my situation I have publications on my page and I need to load others publications when I'm scrolling. The problem is, when my new publication arrive, I lost my old publications.

My service :

GetPiins(page: string): Observable<ListPiinsResponse>  {
  const limit = '8';
  const piins = this.http.get<ListPiinsResponse>(
      `${this.baseUrl}/piins/FollowerAndFriend`, {
        params: {
          limit: limit, page
        }
  });
  return piins;
}

In my ngInit in the component :

this.store$.dispatch(new PiinsFeatureStoreActions.LoadCurrentUserPiins('1'));

this.piins$ = this.store$.pipe(
  select(PiinsFeatureStoreSelectors.selectAllPiinsFeatureItems),
  filter(value => value !== undefined),
);

And my method when I detect a scroll :

onScroll() {
  this.counterPage += 1;
  this.store$.dispatch(new 
  PiinsFeatureStoreActions.LoadCurrentUserPiins(
   String(this.counterPage)
  ));
}

This is in my reducer :

    case ActionTypes.LOAD_PIINS_SUCCESS:{
      const newPiins = featureAdapter.addAll(action.payload, {
        ...state, // I think here I need to concat this value 
        isLoading: false,
        error: null
      });

      return newPiins;
    }

Solution

  • Just merge two arrays

    this.oldData= [ ...oldData, ...newData];
    

    This means that initialy your oldData must be an empty array.

    public oldArray:SomeInterface[] = [];
    

    Should do the trick