From a7a81f48c41609a1d9177063797373ce92089070 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Thu, 14 Oct 2021 23:01:03 +0300 Subject: [PATCH] remove api module, add notifications service --- apps/red-ui/src/app/app.module.ts | 2 -- .../notifications/notifications.component.ts | 9 +++--- .../src/app/services/notifications.service.ts | 32 +++++++++++++++++++ libs/red-ui-http/src/lib/api.module.ts | 31 ------------------ libs/red-ui-http/src/lib/index.ts | 1 - 5 files changed, 37 insertions(+), 38 deletions(-) create mode 100644 apps/red-ui/src/app/services/notifications.service.ts delete mode 100644 libs/red-ui-http/src/lib/api.module.ts diff --git a/apps/red-ui/src/app/app.module.ts b/apps/red-ui/src/app/app.module.ts index d9b7c5431..67dfac44c 100644 --- a/apps/red-ui/src/app/app.module.ts +++ b/apps/red-ui/src/app/app.module.ts @@ -5,7 +5,6 @@ import { ActivatedRoute, Router } from '@angular/router'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { HTTP_INTERCEPTORS, HttpClient, HttpClientModule } from '@angular/common/http'; import { BaseScreenComponent } from '@components/base-screen/base-screen.component'; -import { ApiModule } from '@redaction/red-ui-http'; import { ApiPathInterceptor } from '@utils/api-path-interceptor'; import { MissingTranslationHandler, TranslateCompiler, TranslateLoader, TranslateModule } from '@ngx-translate/core'; import { languageInitializer } from '@i18n/language.initializer'; @@ -65,7 +64,6 @@ const components = [AppComponent, AuthErrorComponent, NotificationsComponent, Sp FileUploadDownloadModule, HttpClientModule, AuthModule, - ApiModule, AppRoutingModule, MonacoEditorModule, IqserHelpModeModule, 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 f50474a7e..609beaf5f 100644 --- a/apps/red-ui/src/app/components/notifications/notifications.component.ts +++ b/apps/red-ui/src/app/components/notifications/notifications.component.ts @@ -1,7 +1,7 @@ import { Component } from '@angular/core'; import * as moment from 'moment'; import { TranslateService } from '@ngx-translate/core'; -import { Notification, NotificationControllerService, NotificationResponse } from '@redaction/red-ui-http'; +import { Notification, NotificationResponse } from '@redaction/red-ui-http'; import { DatePipe } from '@shared/pipes/date.pipe'; import { AppStateService } from '@state/app-state.service'; import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'; @@ -9,6 +9,7 @@ import { UserService } from '@services/user.service'; import { NotificationType, NotificationTypeEnum } from '@models/notification-types'; import { notificationsTranslations } from '../../translations/notifications-translations'; import { DossiersService } from '@services/entity-services/dossiers.service'; +import { NotificationsService } from '@services/notifications.service'; @Component({ selector: 'redaction-notifications', @@ -23,12 +24,12 @@ export class NotificationsComponent { constructor( private readonly _translateService: TranslateService, private readonly _userService: UserService, - private readonly _notificationControllerService: NotificationControllerService, + private readonly _notificationsService: NotificationsService, private readonly _appStateService: AppStateService, private readonly _dossiersService: DossiersService, private readonly _datePipe: DatePipe, ) { - this._notificationControllerService.getNotifications(false).subscribe((response: NotificationResponse) => { + this._notificationsService.getNotifications(false).subscribe((response: NotificationResponse) => { this.notifications = response.notifications.filter(notification => this._isSupportedType(notification)); this._groupNotifications(this.notifications); this.hasUnreadNotifications = this.notifications?.filter(n => !n.readDate).length > 0; @@ -47,7 +48,7 @@ export class NotificationsComponent { async markRead(notification: Notification, $event, isRead: boolean = true) { $event.stopPropagation(); const ids = [`${notification.id}`]; - await this._notificationControllerService.toggleNotificationRead(ids, isRead).toPromise(); + await this._notificationsService.toggleNotificationRead(ids, isRead).toPromise(); if (isRead) { notification.readDate = moment().format('YYYY-MM-DDTHH:mm:ss Z'); } else { diff --git a/apps/red-ui/src/app/services/notifications.service.ts b/apps/red-ui/src/app/services/notifications.service.ts new file mode 100644 index 000000000..8860df5a3 --- /dev/null +++ b/apps/red-ui/src/app/services/notifications.service.ts @@ -0,0 +1,32 @@ +import { Injectable, Injector } from '@angular/core'; +import { GenericService, List, QueryParam, RequiredParam, Validate } from '@iqser/common-ui'; +import { NotificationResponse } from '@redaction/red-ui-http'; + +@Injectable({ + providedIn: 'root', +}) +export class NotificationsService extends GenericService { + constructor(protected readonly _injector: Injector) { + super(_injector, 'notification'); + } + + @Validate() + getNotifications(@RequiredParam() includeSeen: boolean) { + let queryParam: QueryParam; + if (includeSeen !== undefined && includeSeen !== null) { + queryParam = { key: 'includeSeen', value: includeSeen }; + } + + return this._getOne([], this._defaultModelPath, [queryParam]); + } + + @Validate() + toggleNotificationRead(@RequiredParam() body: List, @RequiredParam() setRead: boolean) { + let queryParam: QueryParam; + if (setRead !== undefined && setRead !== null) { + queryParam = { key: 'setRead', value: setRead }; + } + + return this._post(body, `${this._defaultModelPath}/toggle-read`, [queryParam]); + } +} diff --git a/libs/red-ui-http/src/lib/api.module.ts b/libs/red-ui-http/src/lib/api.module.ts deleted file mode 100644 index 37fdf8dcf..000000000 --- a/libs/red-ui-http/src/lib/api.module.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core'; -import { Configuration } from './configuration'; -import { HttpClient } from '@angular/common/http'; -import { NotificationControllerService } from './api/notificationController.service'; - -@NgModule({ - imports: [], - declarations: [], - exports: [], - providers: [NotificationControllerService], -}) -export class ApiModule { - constructor(@Optional() @SkipSelf() parentModule: ApiModule, @Optional() http: HttpClient) { - if (parentModule) { - throw new Error('ApiModule is already loaded. Import in your base AppModule only.'); - } - if (!http) { - throw new Error( - 'You need to import the HttpClientModule in your AppModule! \n' + - 'See also https://github.com/angular/angular/issues/20575', - ); - } - } - - public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders { - return { - ngModule: ApiModule, - providers: [{ provide: Configuration, useFactory: configurationFactory }], - }; - } -} diff --git a/libs/red-ui-http/src/lib/index.ts b/libs/red-ui-http/src/lib/index.ts index 9e8b8971c..50c198439 100644 --- a/libs/red-ui-http/src/lib/index.ts +++ b/libs/red-ui-http/src/lib/index.ts @@ -2,5 +2,4 @@ export * from './api/api'; export * from './model/models'; export * from './variables'; export * from './configuration'; -export * from './api.module'; export * from './red-types';