mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 00:23:03 +00:00
refactor: sort news by datePublished on backend
This commit is contained in:
@@ -15,7 +15,6 @@
|
|||||||
import {Injectable} from '@angular/core';
|
import {Injectable} from '@angular/core';
|
||||||
import {SCMessage} from '@openstapps/core';
|
import {SCMessage} from '@openstapps/core';
|
||||||
import {DataProvider} from '../data/data.provider';
|
import {DataProvider} from '../data/data.provider';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service for providing news messages
|
* Service for providing news messages
|
||||||
*/
|
*/
|
||||||
@@ -23,16 +22,15 @@ import {DataProvider} from '../data/data.provider';
|
|||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
})
|
})
|
||||||
export class NewsProvider {
|
export class NewsProvider {
|
||||||
constructor(private dataProvider: DataProvider) {}
|
constructor(private dataProvider: DataProvider) {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get news messages
|
* Get news messages
|
||||||
* TODO: make dates sortable on the backend side and then adjust this method
|
|
||||||
*
|
*
|
||||||
* @param size How many messages/news to fetch
|
* @param size How many messages/news to fetch
|
||||||
* @param sort If sort by date needs to be performed
|
|
||||||
*/
|
*/
|
||||||
async getList(size: number, sort?: boolean): Promise<SCMessage[]> {
|
async getList(size: number): Promise<SCMessage[]> {
|
||||||
const result = await this.dataProvider.search({
|
const result = await this.dataProvider.search({
|
||||||
filter: {
|
filter: {
|
||||||
type: 'value',
|
type: 'value',
|
||||||
@@ -41,28 +39,16 @@ export class NewsProvider {
|
|||||||
value: 'message',
|
value: 'message',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
sort: [{
|
||||||
|
type: 'generic',
|
||||||
|
arguments: {
|
||||||
|
field: 'datePublished',
|
||||||
|
},
|
||||||
|
order: 'desc',
|
||||||
|
}],
|
||||||
size: size,
|
size: size,
|
||||||
});
|
});
|
||||||
|
|
||||||
const news = result.data as SCMessage[];
|
return result.data as SCMessage[];
|
||||||
|
|
||||||
if (sort) {
|
|
||||||
news.sort((a, b) => {
|
|
||||||
if (
|
|
||||||
typeof a.datePublished !== 'undefined' &&
|
|
||||||
typeof b.datePublished !== 'undefined'
|
|
||||||
) {
|
|
||||||
return a.datePublished > b.datePublished
|
|
||||||
? -1
|
|
||||||
: a.datePublished < b.datePublished
|
|
||||||
? 1
|
|
||||||
: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return news;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License along with
|
* You should have received a copy of the GNU General Public License along with
|
||||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
import {Component, OnInit} from '@angular/core';
|
import {Component} from '@angular/core';
|
||||||
import {IonRefresher} from '@ionic/angular';
|
import {IonRefresher} from '@ionic/angular';
|
||||||
import {SCMessage} from '@openstapps/core';
|
import {SCMessage} from '@openstapps/core';
|
||||||
import {NewsProvider} from '../news.provider';
|
import {NewsProvider} from '../news.provider';
|
||||||
@@ -23,20 +23,21 @@ import {NewsProvider} from '../news.provider';
|
|||||||
selector: 'stapps-news-page',
|
selector: 'stapps-news-page',
|
||||||
templateUrl: 'news-page.html',
|
templateUrl: 'news-page.html',
|
||||||
})
|
})
|
||||||
export class NewsPageComponent implements OnInit {
|
export class NewsPageComponent {
|
||||||
/**
|
/**
|
||||||
* News (messages) to show
|
* News (messages) to show
|
||||||
*/
|
*/
|
||||||
news: SCMessage[] = [];
|
news: SCMessage[] = [];
|
||||||
|
|
||||||
constructor(private newsProvider: NewsProvider) {}
|
constructor(private newsProvider: NewsProvider) {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch news from the backend
|
* Fetch news from the backend
|
||||||
*/
|
*/
|
||||||
async fetchNews() {
|
async fetchNews() {
|
||||||
/* eslint-disable no-magic-numbers */
|
/* tslint:disable:no-magic-numbers */
|
||||||
this.news = await this.newsProvider.getList(30, true);
|
this.news = await this.newsProvider.getList(30);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,7 +55,7 @@ export class NewsPageComponent implements OnInit {
|
|||||||
async refresh(refresher: IonRefresher) {
|
async refresh(refresher: IonRefresher) {
|
||||||
try {
|
try {
|
||||||
await this.fetchNews();
|
await this.fetchNews();
|
||||||
} catch {
|
} catch (e) {
|
||||||
this.news = [];
|
this.news = [];
|
||||||
} finally {
|
} finally {
|
||||||
await refresher.complete();
|
await refresher.complete();
|
||||||
|
|||||||
Reference in New Issue
Block a user