From 3b3f4fd3b13cf2a8845ea0bc4e0eda98f9819eab Mon Sep 17 00:00:00 2001 From: Jan Szumotalski Date: Fri, 13 Jun 2025 16:07:23 +0200 Subject: [PATCH] fix: News now display author --- .../news-edit/news-edit.component.html | 84 +++++++++---------- .../news-edit/news-edit.component.scss | 14 +++- .../news-edit/news-edit.component.ts | 29 +++---- .../admin-view/news-edit/news-edit.service.ts | 44 ++++++++-- src/app/app-view/news/news.component.html | 4 +- src/app/app-view/news/news.component.ts | 12 ++- .../load-shade/load-shade.component.scss | 1 + src/app/services/local-storage.service.ts | 2 +- src/app/services/updates.service.ts | 2 +- src/app/types/{news.ts => news.model.ts} | 7 +- 10 files changed, 124 insertions(+), 75 deletions(-) rename src/app/types/{news.ts => news.model.ts} (53%) diff --git a/src/app/admin-view/news-edit/news-edit.component.html b/src/app/admin-view/news-edit/news-edit.component.html index ddda9c1..d58fe55 100644 --- a/src/app/admin-view/news-edit/news-edit.component.html +++ b/src/app/admin-view/news-edit/news-edit.component.html @@ -1,43 +1,43 @@ - -@if (loading) { - -} -@for (item of news; track item) { - - - {{item.title}} - @if (item.pinned) { - push_pin - } - - - - - - - @switch (item.visible) { - @case (true) { - - } - @default { - - } + +
+ @if (ac.state() != 2) { + + } + @for (item of ac.news | async; track item) { + + + {{item.title}} + @if (item.pinned) { + push_pin } - - - -

{{item.date | date:'d-LL-yyyy HH:mm'}}

-
-
-} -@if (news.length == 0) { - -

- Brak wiadomości. -

-
-} + + + + + + @switch (item.visible) { + @case (true) { + + } + @default { + + } + } + + + +

{{fullName(item)}} {{item.date | date:'d-LL-yyyy HH:mm'}}

+
+ + } @empty { + +

+ Brak wiadomości. +

+
+ } +
diff --git a/src/app/admin-view/news-edit/news-edit.component.scss b/src/app/admin-view/news-edit/news-edit.component.scss index f8241bf..706ab5e 100644 --- a/src/app/admin-view/news-edit/news-edit.component.scss +++ b/src/app/admin-view/news-edit/news-edit.component.scss @@ -1,7 +1,6 @@ mat-card { margin: 15px; padding: 1ch; - width: 100%; } mat-card-title { @@ -31,8 +30,19 @@ button { } :host { - padding: 8pt; display: flex; flex-direction: column; align-items: center; + height: 100%; + width: 100%; +} + +.mainc { + position: relative; + height: 100%; + width: 100%; +} + +.newPost { + margin: 1ch; } diff --git a/src/app/admin-view/news-edit/news-edit.component.ts b/src/app/admin-view/news-edit/news-edit.component.ts index 29fde9b..7670d95 100644 --- a/src/app/admin-view/news-edit/news-edit.component.ts +++ b/src/app/admin-view/news-edit/news-edit.component.ts @@ -3,8 +3,7 @@ import { MatDialog } from '@angular/material/dialog' import { NewPostComponent } from './new-post/edit-post.component' import { catchError, throwError } from 'rxjs' import { MatSnackBar } from '@angular/material/snack-bar' -import { News } from 'src/app/types/news' -import { marked } from 'marked' +import { News } from 'src/app/types/news.model' import { NewsEditService } from './news-edit.service' @Component({ @@ -14,29 +13,15 @@ import { NewsEditService } from './news-edit.service' standalone: false, }) export class NewsEditComponent implements OnInit { - news: Array = new Array< - News & { formatted: string } - >() - loading = true constructor( - private ac: NewsEditService, + protected ac: NewsEditService, private dialog: MatDialog, private sb: MatSnackBar ) { } ngOnInit() { - this.loading = true - this.ac.getNews().subscribe(data => { - this.loading = false - this.news = data.map(v => { - var nd: News & { formatted: string } = { - ...v, - formatted: marked.parse(v.content, { breaks: true }).toString(), - } - return nd - }) - }) + this.ac.refresh() } newPost() { @@ -131,4 +116,12 @@ export class NewsEditComponent implements OnInit { } }) } + + fullName(n: News): string { + const { author: { fname, surname, uname } } = n; + if (fname || surname) { + return [fname, surname].filter(Boolean).join(' '); + } + return uname; + } } diff --git a/src/app/admin-view/news-edit/news-edit.service.ts b/src/app/admin-view/news-edit/news-edit.service.ts index ada1c84..967eff5 100644 --- a/src/app/admin-view/news-edit/news-edit.service.ts +++ b/src/app/admin-view/news-edit/news-edit.service.ts @@ -1,6 +1,9 @@ import { HttpClient } from '@angular/common/http'; -import { Injectable } from '@angular/core'; -import { News } from 'src/app/types/news'; +import { Injectable, signal } from '@angular/core'; +import { marked } from 'marked'; +import { BehaviorSubject, catchError, map, of } from 'rxjs'; +import { News } from 'src/app/types/news.model'; +import { STATE } from 'src/app/types/state'; import { environment } from 'src/environments/environment'; @Injectable({ @@ -8,12 +11,41 @@ import { environment } from 'src/environments/environment'; }) export class NewsEditService { + private _news = new BehaviorSubject<(News & {formatted: string})[]>([]) + public readonly news = this._news.asObservable() + private _state = signal(STATE.NOT_LOADED); + public readonly state = this._state.asReadonly(); + private _error = signal(undefined); + public readonly error = this._error.asReadonly(); + constructor(private http: HttpClient) { } - getNews() { - return this.http.get(environment.apiEndpoint + `/admin/news`, { - withCredentials: true, - }) + public refresh() { + this.getNews() + } + + private getNews() { + this._state.set(STATE.PENDING) + this.http.get + + (environment.apiEndpoint + `/admin/news`, { withCredentials: true, }) + .pipe( + catchError((err: Error) => { + this._state.set(STATE.ERROR) + this._error.set(err.message) + return of() + }), + map(i => { + return i.map(v => ({ + ...v, + formatted: marked.parse(v.content, { breaks: true }).toString() + })) + }) + ).subscribe(v => { + this._error.set(undefined) + this._news.next(v ?? []) + this._state.set(STATE.LOADED) + }) } postNews(title: string, content: string) { diff --git a/src/app/app-view/news/news.component.html b/src/app/app-view/news/news.component.html index 9582378..83e6533 100644 --- a/src/app/app-view/news/news.component.html +++ b/src/app/app-view/news/news.component.html @@ -12,7 +12,7 @@ -

{{item.date | date:'d-LL-yyyy HH:mm'}}

+

{{fullName(item)}} {{item.date | date:'d-LL-yyyy HH:mm'}}

} @@ -22,4 +22,4 @@ Brak wiadomości.

-} \ No newline at end of file +} diff --git a/src/app/app-view/news/news.component.ts b/src/app/app-view/news/news.component.ts index a1a94c8..3b8e24f 100644 --- a/src/app/app-view/news/news.component.ts +++ b/src/app/app-view/news/news.component.ts @@ -1,7 +1,7 @@ import { Component, OnInit } from '@angular/core' import { UpdatesService } from '../../services/updates.service' import { LocalStorageService } from 'src/app/services/local-storage.service' -import { News } from 'src/app/types/news' +import { News } from 'src/app/types/news.model' import { marked } from 'marked' @Component({ @@ -16,7 +16,7 @@ export class NewsComponent implements OnInit { constructor( private newsapi: UpdatesService, private ls: LocalStorageService - ) {} + ) { } ngOnInit() { this.ls.newsflag = false @@ -31,4 +31,12 @@ export class NewsComponent implements OnInit { this.ls.news = this.news }) } + + fullName(n: News): string { + const { author: { fname, surname, uname } } = n; + if (fname || surname) { + return [fname, surname].filter(Boolean).join(' '); + } + return uname; + } } diff --git a/src/app/commonComponents/load-shade/load-shade.component.scss b/src/app/commonComponents/load-shade/load-shade.component.scss index 6efa867..ec00705 100644 --- a/src/app/commonComponents/load-shade/load-shade.component.scss +++ b/src/app/commonComponents/load-shade/load-shade.component.scss @@ -8,4 +8,5 @@ display: flex; align-items: center; justify-content: center; + z-index: 10; } diff --git a/src/app/services/local-storage.service.ts b/src/app/services/local-storage.service.ts index a8f0556..c1290d0 100644 --- a/src/app/services/local-storage.service.ts +++ b/src/app/services/local-storage.service.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core' -import { News } from '../types/news' +import { News } from '../types/news.model' @Injectable({ providedIn: 'root', diff --git a/src/app/services/updates.service.ts b/src/app/services/updates.service.ts index aebb619..99fcd41 100644 --- a/src/app/services/updates.service.ts +++ b/src/app/services/updates.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core' import { HttpClient } from '@angular/common/http' import { Menu } from '../types/menu' import { environment } from 'src/environments/environment' -import { News } from '../types/news' +import { News } from '../types/news.model' import { UKey } from '../types/key' import { CleanNote } from '../types/clean-note' import { Status } from '../types/status' diff --git a/src/app/types/news.ts b/src/app/types/news.model.ts similarity index 53% rename from src/app/types/news.ts rename to src/app/types/news.model.ts index 1638f11..6f833c2 100644 --- a/src/app/types/news.ts +++ b/src/app/types/news.model.ts @@ -4,5 +4,10 @@ export interface News { content: string date: string visible?: boolean - pinned?: boolean + pinned?: boolean, + author: { + fname?: string, + surname?: string, + uname: string + } }