From 9806776b2f9a0bc47484713e2704fd9ef37980e2 Mon Sep 17 00:00:00 2001 From: Erik Tiekstra Date: Thu, 11 Nov 2021 15:56:49 +0100 Subject: [PATCH] Updated news functionality --- .../start/components/news/news.component.html | 4 +++ .../start/components/news/news.component.scss | 7 ++++ .../components/news}/news.component.spec.ts | 0 .../start/components/news/news.component.ts | 12 +++++++ .../start/components/news}/news.module.ts | 9 ++---- .../src/app/pages/start/start.component.html | 11 +++++-- .../src/app/pages/start/start.component.ts | 6 +++- .../src/app/pages/start/start.module.ts | 6 ++-- .../src/app/pages/start/start.service.ts | 24 ++++++++++++++ .../components/nyheter/news.component.html | 6 ---- .../components/nyheter/news.component.scss | 5 --- .../components/nyheter/news.component.ts | 17 ---------- .../src/app/shared/enums/feature.enum.ts | 1 + .../shared/services/api/news.api.service.ts | 32 +++++++++---------- .../src/environments/active-features.ts | 3 ++ 15 files changed, 87 insertions(+), 56 deletions(-) create mode 100644 apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.html create mode 100644 apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.scss rename apps/mina-sidor-fa/src/app/{shared/components/nyheter => pages/start/components/news}/news.component.spec.ts (100%) create mode 100644 apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.ts rename apps/mina-sidor-fa/src/app/{shared/components/nyheter => pages/start/components/news}/news.module.ts (61%) create mode 100644 apps/mina-sidor-fa/src/app/pages/start/start.service.ts delete mode 100644 apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.html delete mode 100644 apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.scss delete mode 100644 apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.ts diff --git a/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.html b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.html new file mode 100644 index 0000000..7de96f3 --- /dev/null +++ b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.html @@ -0,0 +1,4 @@ + +

{{news.title}}

+
+
diff --git a/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.scss b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.scss new file mode 100644 index 0000000..ce38d1c --- /dev/null +++ b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.scss @@ -0,0 +1,7 @@ +@import 'variables/gutters'; + +.news { + &__content { + max-width: var(--digi--typography--text--max-width); + } +} diff --git a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.spec.ts b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.spec.ts similarity index 100% rename from apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.spec.ts rename to apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.spec.ts diff --git a/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.ts b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.ts new file mode 100644 index 0000000..b91b2ee --- /dev/null +++ b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.component.ts @@ -0,0 +1,12 @@ +import { ChangeDetectionStrategy, Component, Input } from '@angular/core'; +import { News } from '@msfa-models/news.model'; + +@Component({ + selector: 'msfa-news', + templateUrl: 'news.component.html', + styleUrls: ['news.component.scss'], + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class NewsComponent { + @Input() news: News; +} diff --git a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.module.ts b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.module.ts similarity index 61% rename from apps/mina-sidor-fa/src/app/shared/components/nyheter/news.module.ts rename to apps/mina-sidor-fa/src/app/pages/start/components/news/news.module.ts index caa9ace..26267e8 100644 --- a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.module.ts +++ b/apps/mina-sidor-fa/src/app/pages/start/components/news/news.module.ts @@ -1,14 +1,11 @@ import { CommonModule } from '@angular/common'; import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core'; -import { UiLoaderModule } from '@ui/loader/loader.module'; import { NewsComponent } from './news.component'; @NgModule({ schemas: [CUSTOM_ELEMENTS_SCHEMA], declarations: [NewsComponent], - imports: [CommonModule, UiLoaderModule], - exports: [NewsComponent] + imports: [CommonModule], + exports: [NewsComponent], }) -export class NewsModule { - -} +export class NewsModule {} diff --git a/apps/mina-sidor-fa/src/app/pages/start/start.component.html b/apps/mina-sidor-fa/src/app/pages/start/start.component.html index dfe6c20..a5b4808 100644 --- a/apps/mina-sidor-fa/src/app/pages/start/start.component.html +++ b/apps/mina-sidor-fa/src/app/pages/start/start.component.html @@ -1,5 +1,5 @@ - +
@@ -26,7 +26,9 @@

Målet är helt enkelt att underlätta ditt dagliga arbete!

- +
+ +

@@ -40,6 +42,9 @@ - + + + + diff --git a/apps/mina-sidor-fa/src/app/pages/start/start.component.ts b/apps/mina-sidor-fa/src/app/pages/start/start.component.ts index 48ce203..2a01d44 100644 --- a/apps/mina-sidor-fa/src/app/pages/start/start.component.ts +++ b/apps/mina-sidor-fa/src/app/pages/start/start.component.ts @@ -1,8 +1,10 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; +import { News } from '@msfa-models/news.model'; import { Role } from '@msfa-models/role.model'; import { UserService } from '@msfa-services/api/user.service'; import { Observable } from 'rxjs'; import { filter, map } from 'rxjs/operators'; +import { StartService } from './start.service'; @Component({ selector: 'msfa-start', @@ -16,8 +18,10 @@ export class StartComponent { map(userName => userName.fullName) ); userRoles$: Observable = this.userService.userRoles$; + news$: Observable = this.startService.fetchNews$(); + newsLoading$: Observable = this.startService.newsLoading$; - constructor(private userService: UserService) {} + constructor(private userService: UserService, private startService: StartService) {} isAuthorizedUser(userRoles: Role[]): boolean { return !!userRoles.length; diff --git a/apps/mina-sidor-fa/src/app/pages/start/start.module.ts b/apps/mina-sidor-fa/src/app/pages/start/start.module.ts index 3c7d87c..1cc0307 100644 --- a/apps/mina-sidor-fa/src/app/pages/start/start.module.ts +++ b/apps/mina-sidor-fa/src/app/pages/start/start.module.ts @@ -3,9 +3,10 @@ import { CommonModule } from '@angular/common'; import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { LayoutModule } from '@msfa-shared/components/layout/layout.module'; -import { NewsModule } from '@msfa-shared/components/nyheter/news.module'; import { UiLoaderModule } from '@ui/loader/loader.module'; +import { NewsModule } from './components/news/news.module'; import { StartComponent } from './start.component'; +import { StartService } from './start.service'; @NgModule({ schemas: [CUSTOM_ELEMENTS_SCHEMA], @@ -16,7 +17,8 @@ import { StartComponent } from './start.component'; LayoutModule, DigiNgCardModule, UiLoaderModule, - NewsModule + NewsModule, ], + providers: [StartService], }) export class StartModule {} diff --git a/apps/mina-sidor-fa/src/app/pages/start/start.service.ts b/apps/mina-sidor-fa/src/app/pages/start/start.service.ts new file mode 100644 index 0000000..fe9128a --- /dev/null +++ b/apps/mina-sidor-fa/src/app/pages/start/start.service.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; +import { mapNewsResponseToNews, News } from '@msfa-models/news.model'; +import { NewsApiService } from '@msfa-services/api/news.api.service'; +import { BehaviorSubject, Observable } from 'rxjs'; +import { map } from 'rxjs/operators'; + +@Injectable() +export class StartService { + private _newsLoading$ = new BehaviorSubject(false); + public newsLoading$: Observable = this._newsLoading$.asObservable(); + + constructor(private newsApiService: NewsApiService) {} + + public fetchNews$(): Observable { + this._newsLoading$.next(true); + + return this.newsApiService.fetchNews$().pipe( + map(response => { + this._newsLoading$.next(false); + return mapNewsResponseToNews(response); + }) + ); + } +} diff --git a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.html b/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.html deleted file mode 100644 index 081f728..0000000 --- a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.html +++ /dev/null @@ -1,6 +0,0 @@ - -

{{news.title}}

-
-
- - diff --git a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.scss b/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.scss deleted file mode 100644 index 6a3f6d8..0000000 --- a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.scss +++ /dev/null @@ -1,5 +0,0 @@ -@import 'variables/gutters'; - -h1 { - margin-top: $digi--layout--gutter--xl; -} diff --git a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.ts b/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.ts deleted file mode 100644 index 82dc79b..0000000 --- a/apps/mina-sidor-fa/src/app/shared/components/nyheter/news.component.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { Component } from '@angular/core'; -import { News } from '@msfa-models/news.model'; -import { NewsApiService } from '@msfa-services/api/news.api.service'; -import { Observable } from 'rxjs'; - -@Component({ - selector: 'msfa-news', - templateUrl: 'news.component.html', - styleUrls: ['news.component.scss'] -}) -export class NewsComponent { - newsData$: Observable = this.newsApiService.fetchNews$(); - newsLoading$: Observable = this.newsApiService.newsLoading$; - - constructor(private newsApiService: NewsApiService) { } - -} diff --git a/apps/mina-sidor-fa/src/app/shared/enums/feature.enum.ts b/apps/mina-sidor-fa/src/app/shared/enums/feature.enum.ts index 4d74555..e0e268c 100644 --- a/apps/mina-sidor-fa/src/app/shared/enums/feature.enum.ts +++ b/apps/mina-sidor-fa/src/app/shared/enums/feature.enum.ts @@ -16,4 +16,5 @@ export enum Feature { REPORTING_PERIODISK_REDOVISNING, REPORTING_INFORMATIV_RAPPORT, EXPORTS, + NEWS, } diff --git a/apps/mina-sidor-fa/src/app/shared/services/api/news.api.service.ts b/apps/mina-sidor-fa/src/app/shared/services/api/news.api.service.ts index d216ccc..b11fe1d 100644 --- a/apps/mina-sidor-fa/src/app/shared/services/api/news.api.service.ts +++ b/apps/mina-sidor-fa/src/app/shared/services/api/news.api.service.ts @@ -1,37 +1,37 @@ import { HttpClient } from '@angular/common/http'; import { Injectable } from '@angular/core'; +import { ErrorType } from '@msfa-enums/error-type.enum'; import { environment } from '@msfa-environment'; import { NewsResponse } from '@msfa-models/api/news.response.model'; -import { errorToCustomError } from '@msfa-models/error/custom-error'; -import { mapNewsResponseToNews, News } from '@msfa-models/news.model'; +import { CustomError } from '@msfa-models/error/custom-error'; import { ErrorService } from '@msfa-services/error.service'; -import { BehaviorSubject, Observable, throwError } from 'rxjs'; -import { catchError, filter, map } from 'rxjs/operators'; +import { BehaviorSubject, Observable } from 'rxjs'; +import { catchError, map } from 'rxjs/operators'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class NewsApiService { private _apiBaseUrl = `${environment.api.url}/confluence/nyheter`; private _newsLoading$ = new BehaviorSubject(false); public newsLoading$: Observable = this._newsLoading$.asObservable(); - constructor(private http: HttpClient, private errorService: ErrorService) { } + constructor(private http: HttpClient, private errorService: ErrorService) {} - public fetchNews$(): Observable { + public fetchNews$(): Observable { this._newsLoading$.next(true); - return this.http.get(this._apiBaseUrl).pipe( - filter(response => !!response), - map(response => { - this._newsLoading$.next(false); - return mapNewsResponseToNews(response['data']); - }), + return this.http.get<{ data: NewsResponse }>(this._apiBaseUrl).pipe( + map(({ data }) => data), catchError((error: Error) => { - this.errorService.add(errorToCustomError({ ...error, message: `Kunde inte hämta nyheter.\n\n${error.message}` })); - return throwError(errorToCustomError(error)); + const customError = new CustomError({ + error, + message: `Kunde inte hämta nyheter.\n\n${error.message}`, + type: ErrorType.API, + }); + this.errorService.add(customError); + throw customError; }) ); } - } diff --git a/apps/mina-sidor-fa/src/environments/active-features.ts b/apps/mina-sidor-fa/src/environments/active-features.ts index 67f1387..914f8c1 100644 --- a/apps/mina-sidor-fa/src/environments/active-features.ts +++ b/apps/mina-sidor-fa/src/environments/active-features.ts @@ -10,6 +10,8 @@ export const ACTIVE_FEATURES_PROD: Feature[] = [ Feature.REPORTING, Feature.LOGGING, Feature.REPORTING_PERIODISK_REDOVISNING, + Feature.REPORTING_INFORMATIV_RAPPORT, + Feature.EXPORTS, ]; export const ACTIVE_FEATURES_TEST: Feature[] = [ @@ -27,4 +29,5 @@ export const ACTIVE_FEATURES_TEST: Feature[] = [ Feature.REPORTING_PERIODISK_REDOVISNING, Feature.REPORTING_INFORMATIV_RAPPORT, Feature.EXPORTS, + Feature.NEWS, ];