From 0833f0847b98a48e620333917aaeeb21e56756c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Thu, 18 Nov 2021 17:00:14 +0200 Subject: [PATCH] Prepare for notifications changes check --- .../notifications/notifications.component.ts | 17 ++++++++++++++--- .../entity-services/dossiers.service.ts | 17 +---------------- .../services/entity-services/files.service.ts | 16 ++-------------- .../src/app/services/notifications.service.ts | 13 +++++++++---- apps/red-ui/src/app/state/app-state.service.ts | 2 +- libs/common-ui | 2 +- 6 files changed, 28 insertions(+), 39 deletions(-) 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 49f816095..06d939c39 100644 --- a/apps/red-ui/src/app/components/notifications/notifications.component.ts +++ b/apps/red-ui/src/app/components/notifications/notifications.component.ts @@ -8,7 +8,10 @@ import { NotificationsService } from '@services/notifications.service'; import { Notification } from '@red/domain'; import { distinctUntilChanged, map } from 'rxjs/operators'; import { BehaviorSubject, Observable } from 'rxjs'; -import { List, shareLast } from '@iqser/common-ui'; +import { AutoUnsubscribe, List, shareLast } from '@iqser/common-ui'; + +const REFRESH_INTERVAL = 3000; +const INCLUDE_SEEN = false; interface NotificationsGroup { date: string; @@ -21,7 +24,7 @@ interface NotificationsGroup { styleUrls: ['./notifications.component.scss'], changeDetection: ChangeDetectionStrategy.OnPush, }) -export class NotificationsComponent implements OnInit { +export class NotificationsComponent extends AutoUnsubscribe implements OnInit { notifications$: Observable; hasUnreadNotifications$: Observable; groupedNotifications$: Observable; @@ -35,6 +38,7 @@ export class NotificationsComponent implements OnInit { private readonly _dossiersService: DossiersService, private readonly _datePipe: DatePipe, ) { + super(); this.notifications$ = this._notifications$.asObservable().pipe(shareLast()); this.groupedNotifications$ = this.notifications$.pipe(map(notifications => this._groupNotifications(notifications))); this.hasUnreadNotifications$ = this.notifications$.pipe( @@ -46,6 +50,13 @@ export class NotificationsComponent implements OnInit { async ngOnInit(): Promise { await this._loadData(); + + // this.addSubscription = timer(REFRESH_INTERVAL, REFRESH_INTERVAL) + // .pipe( + // switchMap(() => this._notificationsService.getNotificationsIfChanged(INCLUDE_SEEN)), + // tap(notifications => this._notifications$.next(notifications)), + // ) + // .subscribe(); } async markRead($event, notifications: List = this._notifications$.getValue().map(n => n.id), isRead = true): Promise { @@ -55,7 +66,7 @@ export class NotificationsComponent implements OnInit { } private async _loadData(): Promise { - const notifications = await this._notificationsService.getNotifications(false).toPromise(); + const notifications = await this._notificationsService.getNotifications(INCLUDE_SEEN).toPromise(); this._notifications$.next(notifications); } diff --git a/apps/red-ui/src/app/services/entity-services/dossiers.service.ts b/apps/red-ui/src/app/services/entity-services/dossiers.service.ts index fa5475ea0..2bd531b20 100644 --- a/apps/red-ui/src/app/services/entity-services/dossiers.service.ts +++ b/apps/red-ui/src/app/services/entity-services/dossiers.service.ts @@ -20,7 +20,6 @@ const GENERIC_MGS = _('add-dossier-dialog.errors.generic'); }) export class DossiersService extends EntitiesService { readonly generalStats$ = this.all$.pipe(switchMap(entities => this._generalStats$(entities))); - private _lastCheckedForChanges = '0'; constructor( private readonly _toaster: Toaster, @@ -30,19 +29,9 @@ export class DossiersService extends EntitiesService { super(_injector, Dossier, 'dossier'); } - private get _hasChanges$(): Observable { - return this._post<{ value: boolean }>({ since: this._lastCheckedForChanges }, `${this._defaultModelPath}/changes`).pipe( - map(res => res.value), - tap(() => this._updateLastChanged()), - ); - } - loadAll(): Observable { const dossierIds = (dossiers: Dossier[]) => dossiers.map(d => d.dossierId); - return super.loadAll().pipe( - tap(() => this._updateLastChanged()), - switchMap(dossiers => this._dossierStatsService.getFor(dossierIds(dossiers)).pipe(mapTo(dossiers))), - ); + return super.loadAll().pipe(switchMap(dossiers => this._dossierStatsService.getFor(dossierIds(dossiers)).pipe(mapTo(dossiers)))); } loadAllIfChanged(): Observable { @@ -88,10 +77,6 @@ export class DossiersService extends EntitiesService { return super.delete(body, 'deleted-dossiers/hard-delete', body).toPromise(); } - private _updateLastChanged(): void { - this._lastCheckedForChanges = new Date().toISOString(); - } - private _computeStats(entities: List): IDossiersStats { let totalAnalyzedPages = 0; const totalPeople = new Set(); diff --git a/apps/red-ui/src/app/services/entity-services/files.service.ts b/apps/red-ui/src/app/services/entity-services/files.service.ts index 7431fa983..2f7cf3dda 100644 --- a/apps/red-ui/src/app/services/entity-services/files.service.ts +++ b/apps/red-ui/src/app/services/entity-services/files.service.ts @@ -17,20 +17,8 @@ export class FilesService extends EntitiesService { this.get().pipe(map(files => files.map(file => new File(file, this._userService.getNameForId(file.currentReviewer))))); } - /** - * Gets the status for all files. - */ - get(): Observable; - /** - * Gets the status for a file from a dossier. - */ - get(dossierId: string, fileId: string): Observable; - get(dossierId?: string, fileId?: string): Observable { - if (dossierId && fileId) { - return super._getOne([dossierId, fileId]); - } - - return super.getAll(); + getOne(dossierId: string, fileId: string): Observable { + return super._getOne([dossierId, fileId]); } getFor(dossierId: string): Observable; diff --git a/apps/red-ui/src/app/services/notifications.service.ts b/apps/red-ui/src/app/services/notifications.service.ts index 56ee31c01..dd7410f19 100644 --- a/apps/red-ui/src/app/services/notifications.service.ts +++ b/apps/red-ui/src/app/services/notifications.service.ts @@ -2,9 +2,9 @@ import { Injectable, Injector } from '@angular/core'; import { GenericService, List, mapEach, QueryParam, RequiredParam, Validate } from '@iqser/common-ui'; import * as moment from 'moment'; import { TranslateService } from '@ngx-translate/core'; -import { Observable } from 'rxjs'; +import { iif, Observable } from 'rxjs'; import { INotification, Notification, NotificationTypes } from '@red/domain'; -import { map } from 'rxjs/operators'; +import { map, switchMap } from 'rxjs/operators'; import { notificationsTranslations } from '../translations/notifications-translations'; import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; import { DossiersService } from '@services/entity-services/dossiers.service'; @@ -14,7 +14,7 @@ import { FilesMapService } from '@services/entity-services/files-map.service'; @Injectable({ providedIn: 'root', }) -export class NotificationsService extends GenericService { +export class NotificationsService extends GenericService { constructor( protected readonly _injector: Injector, private readonly _translateService: TranslateService, @@ -32,12 +32,17 @@ export class NotificationsService extends GenericService { queryParam = { key: 'includeSeen', value: includeSeen }; } - return this._getOne<{ notifications: INotification[] }>([], this._defaultModelPath, [queryParam]).pipe( + return this.getAll<{ notifications: Notification[] }>(this._defaultModelPath, [queryParam]).pipe( map(response => response.notifications.filter(n => n.notificationType in NotificationTypes)), mapEach(notification => this._new(notification)), ); } + @Validate() + getNotificationsIfChanged(@RequiredParam() includeSeen: boolean): Observable { + return this._hasChanges$.pipe(switchMap(changed => iif(() => changed, this.getNotifications(includeSeen)))); + } + @Validate() toggleNotificationRead(@RequiredParam() body: List, @RequiredParam() setRead: boolean) { let queryParam: QueryParam; diff --git a/apps/red-ui/src/app/state/app-state.service.ts b/apps/red-ui/src/app/state/app-state.service.ts index b63ed6202..e70ef2188 100644 --- a/apps/red-ui/src/app/state/app-state.service.ts +++ b/apps/red-ui/src/app/state/app-state.service.ts @@ -104,7 +104,7 @@ export class AppStateService { return null; } - const iFile = await this._filesService.get(dossierId, fileId).toPromise(); + const iFile = await this._filesService.getOne(dossierId, fileId).toPromise(); const newFile = new File( iFile, diff --git a/libs/common-ui b/libs/common-ui index 4242cd442..1408f81f2 160000 --- a/libs/common-ui +++ b/libs/common-ui @@ -1 +1 @@ -Subproject commit 4242cd442b4ada713c9d44788d6d4a66fd06ced3 +Subproject commit 1408f81f2b230d0338aa856531a705bf8cc5014c