From 654a188f80fc1f089aa14837084664c908fe27d2 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Wed, 15 Jul 2020 11:15:50 +0200 Subject: allow sorting notifications --- .../shared-main/users/user-notification.service.ts | 14 ++++++++--- .../users/user-notifications.component.ts | 28 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 8 deletions(-) (limited to 'client/src/app/shared/shared-main') diff --git a/client/src/app/shared/shared-main/users/user-notification.service.ts b/client/src/app/shared/shared-main/users/user-notification.service.ts index 8dd9472fe..ecc66ecdb 100644 --- a/client/src/app/shared/shared-main/users/user-notification.service.ts +++ b/client/src/app/shared/shared-main/users/user-notification.service.ts @@ -5,6 +5,7 @@ import { ComponentPaginationLight, RestExtractor, RestService, User, UserNotific import { ResultList, UserNotification as UserNotificationServer, UserNotificationSetting } from '@shared/models' import { environment } from '../../../../environments/environment' import { UserNotification } from './user-notification.model' +import { SortMeta } from 'primeng/api' @Injectable() export class UserNotificationService { @@ -18,9 +19,16 @@ export class UserNotificationService { private userNotificationSocket: UserNotificationSocket ) {} - listMyNotifications (pagination: ComponentPaginationLight, unread?: boolean, ignoreLoadingBar = false) { + listMyNotifications (parameters: { + pagination: ComponentPaginationLight + ignoreLoadingBar?: boolean + unread?: boolean, + sort?: SortMeta + }) { + const { pagination, ignoreLoadingBar, unread, sort } = parameters + let params = new HttpParams() - params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination)) + params = this.restService.addRestGetParams(params, this.restService.componentPaginationToRestPagination(pagination), sort) if (unread) params = params.append('unread', `${unread}`) @@ -35,7 +43,7 @@ export class UserNotificationService { } countUnreadNotifications () { - return this.listMyNotifications({ currentPage: 1, itemsPerPage: 0 }, true) + return this.listMyNotifications({ pagination: { currentPage: 1, itemsPerPage: 0 }, ignoreLoadingBar: true, unread: true }) .pipe(map(n => n.total)) } diff --git a/client/src/app/shared/shared-main/users/user-notifications.component.ts b/client/src/app/shared/shared-main/users/user-notifications.component.ts index 6abd8b7d8..48be80e3f 100644 --- a/client/src/app/shared/shared-main/users/user-notifications.component.ts +++ b/client/src/app/shared/shared-main/users/user-notifications.component.ts @@ -19,6 +19,7 @@ export class UserNotificationsComponent implements OnInit { @Output() notificationsLoaded = new EventEmitter() notifications: UserNotification[] = [] + sortField = 'createdAt' // So we can access it in the template UserNotificationType = UserNotificationType @@ -39,18 +40,25 @@ export class UserNotificationsComponent implements OnInit { totalItems: null } - this.loadMoreNotifications() + this.loadNotifications() if (this.markAllAsReadSubject) { this.markAllAsReadSubject.subscribe(() => this.markAllAsRead()) } } - loadMoreNotifications () { - this.userNotificationService.listMyNotifications(this.componentPagination, undefined, this.ignoreLoadingBar) + loadNotifications (reset?: boolean) { + this.userNotificationService.listMyNotifications({ + pagination: this.componentPagination, + ignoreLoadingBar: this.ignoreLoadingBar, + sort: { + field: this.sortField, + order: this.sortField === 'createdAt' ? -1 : 1 + } + }) .subscribe( result => { - this.notifications = this.notifications.concat(result.data) + this.notifications = reset ? result.data : this.notifications.concat(result.data) this.componentPagination.totalItems = result.total this.notificationsLoaded.emit() @@ -68,7 +76,7 @@ export class UserNotificationsComponent implements OnInit { this.componentPagination.currentPage++ if (hasMoreItems(this.componentPagination)) { - this.loadMoreNotifications() + this.loadNotifications() } } @@ -97,4 +105,14 @@ export class UserNotificationsComponent implements OnInit { err => this.notifier.error(err.message) ) } + + changeSortColumn (column: string) { + this.componentPagination = { + currentPage: 1, + itemsPerPage: this.itemsPerPage, + totalItems: null + } + this.sortField = column + this.loadNotifications(true) + } } -- cgit v1.2.3