post.component.ts 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
  3. import { Subscription } from 'rxjs';
  4. import { JhiEventManager, JhiAlertService } from 'ng-jhipster';
  5. import { IPost } from 'app/shared/model/post.model';
  6. import { Principal } from 'app/core';
  7. import { PostService } from './post.service';
  8. @Component({
  9. selector: 'jhi-post',
  10. templateUrl: './post.component.html'
  11. })
  12. export class PostComponent implements OnInit, OnDestroy {
  13. posts: IPost[];
  14. currentAccount: any;
  15. eventSubscriber: Subscription;
  16. constructor(
  17. private postService: PostService,
  18. private jhiAlertService: JhiAlertService,
  19. private eventManager: JhiEventManager,
  20. private principal: Principal
  21. ) {}
  22. loadAll() {
  23. this.postService.query().subscribe(
  24. (res: HttpResponse<IPost[]>) => {
  25. this.posts = res.body;
  26. },
  27. (res: HttpErrorResponse) => this.onError(res.message)
  28. );
  29. }
  30. ngOnInit() {
  31. this.loadAll();
  32. this.principal.identity().then(account => {
  33. this.currentAccount = account;
  34. });
  35. this.registerChangeInPosts();
  36. }
  37. ngOnDestroy() {
  38. this.eventManager.destroy(this.eventSubscriber);
  39. }
  40. trackId(index: number, item: IPost) {
  41. return item.id;
  42. }
  43. registerChangeInPosts() {
  44. this.eventSubscriber = this.eventManager.subscribe('postListModification', response => this.loadAll());
  45. }
  46. private onError(errorMessage: string) {
  47. this.jhiAlertService.error(errorMessage, null, null);
  48. }
  49. }