From 44fd1c5d4cfa6fdaaf1f4e03b2317aa801da68d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Thu, 21 Oct 2021 01:26:11 +0300 Subject: [PATCH] RED-2465: Unread notifications dot --- .../components/notifications/notification.ts | 16 +----- .../notifications.component.html | 2 +- .../notifications/notifications.component.ts | 57 ++++++++++++------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/apps/red-ui/src/app/components/notifications/notification.ts b/apps/red-ui/src/app/components/notifications/notification.ts index 2ac41f706..dcd879a1d 100644 --- a/apps/red-ui/src/app/components/notifications/notification.ts +++ b/apps/red-ui/src/app/components/notifications/notification.ts @@ -1,20 +1,17 @@ import { INotification } from '@redaction/red-ui-http'; import { IListable } from '@iqser/common-ui'; -import { BehaviorSubject, Observable } from 'rxjs'; -import { shareReplay } from 'rxjs/operators'; export class Notification implements INotification, IListable { readonly creationDate?: string; readonly issuerId?: string; readonly notificationDetails?: string; readonly notificationType?: string; - readonly readDate$: Observable; + readonly readDate?: string; readonly seenDate?: string; readonly softDeleted?: string; readonly target?: any; readonly userId?: string; readonly id: string; - private readonly _readDate$: BehaviorSubject; constructor(notification: INotification, readonly message: string, readonly time: string) { this.creationDate = notification.creationDate; @@ -22,23 +19,14 @@ export class Notification implements INotification, IListable { this.notificationDetails = notification.notificationDetails; this.notificationType = notification.notificationType; this.seenDate = notification.seenDate; + this.readDate = notification.readDate; this.softDeleted = notification.softDeleted; this.target = notification.target; this.userId = notification.userId; this.id = notification.id; - this._readDate$ = new BehaviorSubject(notification.readDate); - this.readDate$ = this._readDate$.asObservable().pipe(shareReplay(1)); } get searchKey(): string { return this.id; } - - get readDate(): string { - return this._readDate$.value; - } - - setReadDate(value: string | undefined): void { - this._readDate$.next(value); - } } diff --git a/apps/red-ui/src/app/components/notifications/notifications.component.html b/apps/red-ui/src/app/components/notifications/notifications.component.html index d67495ee9..3b2f42e76 100644 --- a/apps/red-ui/src/app/components/notifications/notifications.component.html +++ b/apps/red-ui/src/app/components/notifications/notifications.component.html @@ -15,7 +15,7 @@
diff --git a/apps/red-ui/src/app/components/notifications/notifications.component.ts b/apps/red-ui/src/app/components/notifications/notifications.component.ts index 7356378dd..931eb9f4d 100644 --- a/apps/red-ui/src/app/components/notifications/notifications.component.ts +++ b/apps/red-ui/src/app/components/notifications/notifications.component.ts @@ -1,5 +1,4 @@ -import { ChangeDetectionStrategy, Component } from '@angular/core'; -import * as moment from 'moment'; +import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { DatePipe } from '@shared/pipes/date.pipe'; import { AppStateService } from '@state/app-state.service'; @@ -7,7 +6,13 @@ import { UserService } from '@services/user.service'; import { DossiersService } from '@services/entity-services/dossiers.service'; import { NotificationsService } from '@services/notifications.service'; import { Notification } from '@components/notifications/notification'; -import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators'; +import { distinctUntilChanged, map } from 'rxjs/operators'; +import { BehaviorSubject, Observable } from 'rxjs'; + +interface NotificationsGroup { + date: string; + notifications: Notification[]; +} @Component({ selector: 'redaction-notifications', @@ -15,13 +20,11 @@ import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators'; styleUrls: ['./notifications.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class NotificationsComponent { - notifications$ = this._notificationsService.getNotifications(false).pipe(shareReplay(1)); - hasUnreadNotifications$ = this.notifications$.pipe( - map(notifications => notifications.filter(n => !n.readDate).length > 0), - distinctUntilChanged(), - ); - groupedNotifications$ = this.notifications$.pipe(map(notifications => this._groupNotifications(notifications))); +export class NotificationsComponent implements OnInit { + notifications$: Observable; + hasUnreadNotifications$: Observable; + groupedNotifications$: Observable; + private _notifications$ = new BehaviorSubject([]); constructor( private readonly _translateService: TranslateService, @@ -30,19 +33,31 @@ export class NotificationsComponent { private readonly _appStateService: AppStateService, private readonly _dossiersService: DossiersService, private readonly _datePipe: DatePipe, - ) {} - - async markRead(notification: Notification, $event, isRead = true) { - $event.stopPropagation(); - await this._notificationsService.toggleNotificationRead([notification.id], isRead).toPromise(); - if (isRead) { - notification.setReadDate(moment().format('YYYY-MM-DDTHH:mm:ss Z')); - } else { - notification.setReadDate(null); - } + ) { + this.notifications$ = this._notifications$.asObservable(); + this.groupedNotifications$ = this.notifications$.pipe(map(notifications => this._groupNotifications(notifications))); + this.hasUnreadNotifications$ = this.notifications$.pipe( + map(notifications => notifications.filter(n => !n.readDate).length > 0), + distinctUntilChanged(), + ); } - private _groupNotifications(notifications: Notification[]): { date: string; notifications: Notification[] }[] { + async ngOnInit(): Promise { + await this._loadData(); + } + + async markRead(notification: Notification, $event, isRead = true): Promise { + $event.stopPropagation(); + await this._notificationsService.toggleNotificationRead([notification.id], isRead).toPromise(); + await this._loadData(); + } + + private async _loadData(): Promise { + const notifications = await this._notificationsService.getNotifications(false).toPromise(); + this._notifications$.next(notifications); + } + + private _groupNotifications(notifications: Notification[]): NotificationsGroup[] { const res = {}; for (const notification of notifications) {