/* * Copyright (C) 2018 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ import {Component} from '@angular/core'; import {AlertController, LoadingController} from '@ionic/angular'; import {Client} from '@openstapps/api/lib/client'; import {SCThing} from '@openstapps/core'; import {Subject} from 'rxjs'; import {debounceTime, distinctUntilChanged} from 'rxjs/operators'; import {StAppsWebHttpClient} from '../stapps-web-http-client.provider'; @Component({ selector: 'stapps-data-list', templateUrl: 'data-list.html', }) export class DataListComponent { selectedItem: any; items: SCThing[] = []; client: Client; size: number = 30; from: number = 0; query: string; queryChanged: Subject = new Subject(); loading: HTMLIonLoadingElement; constructor(private loadingController: LoadingController, private alertController: AlertController, swHttpClient: StAppsWebHttpClient) { this.client = new Client(swHttpClient, 'https://stappsbe01.innocampus.tu-berlin.de', '1.0.6'); this.queryChanged .pipe( debounceTime(1000), distinctUntilChanged()) .subscribe((model) => { this.from = 0; this.query = model; this.items = []; this.fetchItems(); }); this.fetchItems(); } search(query: string) { this.queryChanged.next(query); } async loadMore(event: any): Promise { this.from += this.size; await this.fetchItems(); event.target.complete(); return; } private async fetchItems(): Promise { if (this.from === 0) { this.loading = await this.loadingController.create(); await this.loading.present(); } return this.client.search({ from: this.from, query: this.query, size: this.size } as any).then((result) => { result.data.forEach((item) => { this.items.push(item); }); if (this.from === 0) { this.loading.dismiss(); } }, async (err) => { const alert: HTMLIonAlertElement = await this.alertController.create({ buttons: ['Dismiss'], header: 'Error', subHeader: err.message, }); await alert.present(); await this.loading.dismiss(); }); } }