-
-
Save uno-de-piera/a75dcd2c59cbce45a892760885d0ce17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import {AfireService} from '../afire.service'; | |
import {map} from 'rxjs/operators'; | |
import * as _ from 'lodash'; | |
@Component({ | |
selector: 'app-pagination', | |
templateUrl: './pagination.component.html', | |
styleUrls: ['./pagination.component.css'] | |
}) | |
export class PaginationComponent implements OnInit { | |
todos: Array<any> = []; | |
batch: number = 4; | |
last: any = Date.now(); | |
empty: boolean = false; | |
loading: boolean = false; | |
constructor(public aFireService: AfireService) {} | |
ngOnInit() { | |
this.fetchTodosPaginated(); | |
} | |
onScroll () { | |
this.loading = true; | |
setTimeout(() => { | |
this.fetchTodosPaginated(); | |
this.loading = false; | |
}, 1500); | |
} | |
fetchTodosPaginated () { | |
this.aFireService.paginate(this.batch, this.last).pipe( | |
map(data => { | |
if ( !data.length) { | |
this.empty = true; | |
} | |
let last = _.last(data); | |
if (last) { | |
this.last = last.payload.doc.data().id; | |
data.map(todoSnap => { | |
this.todos.push(todoSnap.payload.doc.data()); | |
}) | |
} | |
}) | |
).subscribe(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment