review-front/src/app/home/home.component.ts
2024-12-26 16:22:54 +01:00

65 lines
1.8 KiB
TypeScript

import {Component, OnDestroy} from '@angular/core';
import {AvatarModule} from 'primeng/avatar';
import {Button} from 'primeng/button';
import {AuthorService} from '../services/author.service';
import {Author} from '../models/author';
import {JsonPipe} from '@angular/common';
import {Subscription} from 'rxjs';
import {MessageService} from 'primeng/api';
import {HeaderComponent} from '../header/header.component';
import {ToastModule} from 'primeng/toast';
import {CookieService} from 'ngx-cookie-service';
import {PostService} from '../services/post.service';
import {Post} from '../models/post';
import {PostHomeComponent} from '../post-home/post-home.component';
@Component({
selector: 'app-home',
standalone: true,
imports: [
AvatarModule,
Button,
HeaderComponent,
ToastModule,
JsonPipe,
PostHomeComponent
],
templateUrl: './home.component.html',
styleUrl: './home.component.css'
})
export class HomeComponent implements OnDestroy {
actualAuthor: Author | undefined;
posts: Post[] = []
subs: Subscription[] = []
constructor(
private messageService: MessageService,
private postService: PostService,
private cookieService: CookieService) {
if (this.cookieService.get('author')) {
this.actualAuthor = JSON.parse(this.cookieService.get('author'));
}
this.subs.push(this.postService.list()
.subscribe({
next: (posts: Post[]) => this.posts = posts,
error: (err) => console.error(err.message),
}));
}
successMessage(summary: string, detail: string): void {
this.messageService.add({
severity: 'success',
summary: summary,
detail: detail,
life: 3000,
closable: false
});
}
ngOnDestroy(): void {
this.subs.forEach(sub => sub.unsubscribe());
}
}