post-update.component.ts 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { Component, OnInit } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. import { HttpResponse, HttpErrorResponse } from '@angular/common/http';
  4. import { Observable } from 'rxjs';
  5. import * as moment from 'moment';
  6. import { JhiAlertService } from 'ng-jhipster';
  7. import { IPost } from 'app/shared/model/post.model';
  8. import { PostService } from './post.service';
  9. import { IUser, UserService } from 'app/core';
  10. import { IPrivacy } from 'app/shared/model/privacy.model';
  11. import { PrivacyService } from 'app/entities/privacy';
  12. @Component({
  13. selector: 'jhi-post-update',
  14. templateUrl: './post-update.component.html'
  15. })
  16. export class PostUpdateComponent implements OnInit {
  17. post: IPost;
  18. isSaving: boolean;
  19. users: IUser[];
  20. privacysettings: IPrivacy[];
  21. timestampDp: any;
  22. constructor(
  23. private jhiAlertService: JhiAlertService,
  24. private postService: PostService,
  25. private userService: UserService,
  26. private privacyService: PrivacyService,
  27. private activatedRoute: ActivatedRoute
  28. ) {}
  29. ngOnInit() {
  30. this.isSaving = false;
  31. this.activatedRoute.data.subscribe(({ post }) => {
  32. this.post = post;
  33. });
  34. this.userService.query().subscribe(
  35. (res: HttpResponse<IUser[]>) => {
  36. this.users = res.body;
  37. },
  38. (res: HttpErrorResponse) => this.onError(res.message)
  39. );
  40. this.privacyService.query({ filter: 'post-is-null' }).subscribe(
  41. (res: HttpResponse<IPrivacy[]>) => {
  42. if (!this.post.privacySetting || !this.post.privacySetting.id) {
  43. this.privacysettings = res.body;
  44. } else {
  45. this.privacyService.find(this.post.privacySetting.id).subscribe(
  46. (subRes: HttpResponse<IPrivacy>) => {
  47. this.privacysettings = [subRes.body].concat(res.body);
  48. },
  49. (subRes: HttpErrorResponse) => this.onError(subRes.message)
  50. );
  51. }
  52. },
  53. (res: HttpErrorResponse) => this.onError(res.message)
  54. );
  55. }
  56. previousState() {
  57. window.history.back();
  58. }
  59. save() {
  60. this.isSaving = true;
  61. if (this.post.id !== undefined) {
  62. this.subscribeToSaveResponse(this.postService.update(this.post));
  63. } else {
  64. this.subscribeToSaveResponse(this.postService.create(this.post));
  65. }
  66. }
  67. private subscribeToSaveResponse(result: Observable<HttpResponse<IPost>>) {
  68. result.subscribe((res: HttpResponse<IPost>) => this.onSaveSuccess(), (res: HttpErrorResponse) => this.onSaveError());
  69. }
  70. private onSaveSuccess() {
  71. this.isSaving = false;
  72. this.previousState();
  73. }
  74. private onSaveError() {
  75. this.isSaving = false;
  76. }
  77. private onError(errorMessage: string) {
  78. this.jhiAlertService.error(errorMessage, null, null);
  79. }
  80. trackUserById(index: number, item: IUser) {
  81. return item.id;
  82. }
  83. trackPrivacyById(index: number, item: IPrivacy) {
  84. return item.id;
  85. }
  86. }