I am trying to use Ionic's Infinite Scroll with paginated data from an api that gets added to a ngrx store, but I'm not sure I fully understand how to make it work.
I found this SO answer for help with Observables, but getting data from the store is a little different. I can't subscribe to the response from the api since it is handled by an Effect.
Here's my simplified code:
Template
<ion-content padding>
<ion-list>
<ion-item *ngFor="let item of (items | async).results">
<h2>{{ item.name }}</h2>
</ion-item>
</ion-list>
<ion-infinite-scroll (ionInfinite)="infiniteScrolling$.next($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>
Component
export class InventoryPage {
items: Observable<Items>;
infiniteScrolling$: Subject<any> = new Subject();
constructor(
public store: Store<AppState>,
public itemsActions: ItemsActions
) {}
ngOnInit() {
this.items = this.store.select(appState => appState.items);
this.store.dispatch(createAction('FETCH_ITEMS'));
// What should this.infiniteScrolling$ do here since dispatching
// actions does not return an observable?
}
}
Store
const initialState = { results: [] };
export function itemsReducer(items: Items = initialState, action: Action): Items {
switch (action.type) {
case 'FETCH_ITEMS_SUCCESS':
return { results: [...items.results, ...action.payload.results] };
default:
return items;
}
}
Am I on the right track and if yes, where should I call infiniteScroll.complete()
once the data is returned from the api?