0

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;
    }
  • 1
    Instead of `return piins;` have an array and push items to it instead of returning new data again and again. or concatenate. – Maihan Nijat Jun 10 '19 at 18:26
  • hope piins$ variable used to loop in the html, share the html component also – Raghul Selvam Jun 10 '19 at 19:06
  • Thank's everyone. But I think the solution right now it's just to know how I can concat this value. If it's not the better way I use your solution. And I can use a concat in my service cause my state with reducer need to know the old states of publications – Raphaël Etang-Salé Jun 10 '19 at 21:37
  • I think this can help this topic https://stackoverflow.com/questions/52109465/angular-6-ngrx-how-to-add-new-item-to-array-in-state-object – Raphaël Etang-Salé Jun 10 '19 at 23:14

1 Answers1

1

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

skeefee
  • 71
  • 1