Files
openstapps/src/app/modules/news/page/news-page.component.ts
2021-07-16 17:00:21 +02:00

84 lines
2.1 KiB
TypeScript

/*
* Copyright (C) 2020-2021 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 <https://www.gnu.org/licenses/>.
*/
import {Component} from '@angular/core';
import {IonRefresher} from '@ionic/angular';
import {SCMessage} from '@openstapps/core';
import {NewsProvider} from '../news.provider';
/**
* News page component
*/
@Component({
selector: 'stapps-news-page',
templateUrl: 'news-page.html',
})
export class NewsPageComponent {
/**
* Thing counter to start query the next page from
*/
from = 0;
/**
* Page size of queries
*/
pageSize = 10;
/**
* News (messages) to show
*/
news: SCMessage[] = [];
constructor(private newsProvider: NewsProvider) {}
/**
* Fetch news from the backend
*/
async fetchNews() {
this.news = await this.newsProvider.getList(this.pageSize, this.from);
}
/**
* Loads more news
*
* @param event Signal from the infinite scroll to load more data
*/
// tslint:disable-next-line:no-any
async loadMore(event: any): Promise<void> {
this.from += this.pageSize;
this.news = this.news.concat(await this.newsProvider.getList(this.pageSize, this.from));
event.target.complete();
}
/**
* Initialize the local variables on component initialization
*/
ngOnInit() {
void this.fetchNews();
}
/**
* Updates the shown list
*
* @param refresher Refresher component that triggers the update
*/
async refresh(refresher: IonRefresher) {
try {
this.from = 0;
await this.fetchNews();
} catch (e) {
this.news = [];
} finally {
await refresher.complete();
}
}
}