Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

193 строки
7.0 KiB

  1. import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
  2. import {CommentJsonld, ContactJsonld, ContactService, PostJsonld, PostService} from "@app/core/api/v1";
  3. import {Subscription} from "rxjs";
  4. import {ActivatedRoute} from "@angular/router";
  5. import {MatPaginator, MatPaginatorIntl, PageEvent} from "@angular/material/paginator";
  6. import {MatTableDataSource} from "@angular/material/table";
  7. import {NewPostingComponent} from "@app/postings/new-posting/new-posting.component";
  8. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  9. import {NewContactComponent} from "@app/contacts/new-contact/new-contact.component";
  10. import {ModalStatus} from "@app/_helpers/modal.states";
  11. import {User} from "@app/_models";
  12. import {AccountService} from "@app/_services";
  13. import {NewCommentComponent} from "@app/postings/new-comment/new-comment.component";
  14. import {ApiConverter} from "@app/_helpers/api.converter";
  15. @Component({
  16. selector: 'app-contacts-detail',
  17. templateUrl: './contacts-detail.component.html',
  18. styleUrl: './contacts-detail.component.scss'
  19. })
  20. export class ContactsDetailComponent implements OnInit, AfterViewInit {
  21. @ViewChild(MatPaginator) postsPaginator: MatPaginator;
  22. protected user: User | null;
  23. protected id: string;
  24. protected contact: ContactJsonld;
  25. protected contactSub: Subscription;
  26. protected postsSub: Subscription;
  27. protected posts: Array<PostJsonld>;
  28. protected postsDataSource;
  29. protected postsLength: number;
  30. protected postsPageEvent: PageEvent;
  31. protected postsPageSize: number;
  32. protected postsPageIndex: number;
  33. protected commentsVisibility: Map<string, boolean>;
  34. protected modalOptions: NgbModalOptions = {
  35. centered: true
  36. };
  37. constructor(
  38. private contactService: ContactService,
  39. private accountService: AccountService,
  40. private route: ActivatedRoute,
  41. private postService: PostService,
  42. private modalService: NgbModal
  43. ) {
  44. this.user = this.accountService.userValue;
  45. this.id = "";
  46. this.contact = {} as ContactJsonld;
  47. this.contactSub = new Subscription();
  48. this.postsSub = new Subscription();
  49. this.posts = [];
  50. this.postsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  51. this.postsDataSource = new MatTableDataSource<PostJsonld>(this.posts);
  52. this.postsLength = 0;
  53. this.postsPageEvent = new PageEvent();
  54. this.postsPageSize = 10;
  55. this.postsPageIndex = 0;
  56. this.commentsVisibility = new Map<string, boolean>();
  57. }
  58. ngOnInit() {
  59. this.route.params.subscribe(params => {
  60. this.id = params['id'];
  61. });
  62. this.getContactData();
  63. this.getPostsData();
  64. }
  65. ngAfterViewInit() {
  66. this.postsDataSource.paginator = this.postsPaginator;
  67. }
  68. getContactData() {
  69. // switch over this.dataType (customers, etc.)
  70. this.contactSub = this.contactService.contactsIdGet(
  71. this.id
  72. ).subscribe(
  73. data => {
  74. this.contact = data;
  75. }
  76. );
  77. }
  78. getPostsData() {
  79. this.postsSub = this.postService.postsGetCollection(
  80. this.postsPageIndex + 1,
  81. this.postsPageSize,
  82. this.contact.partner + '',
  83. [],
  84. this.id,
  85. undefined,
  86. undefined,
  87. undefined,
  88. undefined,
  89. false
  90. ).subscribe(
  91. data => {
  92. this.posts = data["hydra:member"];
  93. this.postsLength = Number(data["hydra:totalItems"]);
  94. this.posts.forEach(posts => {
  95. if (posts.id) {
  96. this.commentsVisibility.set(posts.id, false);
  97. }
  98. });
  99. }
  100. );
  101. }
  102. postsHandlePageEvent(e: PageEvent) {
  103. this.postsPageEvent = e;
  104. this.postsLength = e.length;
  105. this.postsPageIndex = e.pageIndex.valueOf();
  106. this.postsPageSize = e.pageSize.valueOf();
  107. this.getPostsData();
  108. }
  109. openModalNewPosting() {
  110. const modalRefPosting = this.modalService.open(NewPostingComponent, this.modalOptions);
  111. let posting: PostJsonld = {} as PostJsonld;
  112. posting.contact = this.contact.id ?? null;
  113. posting.partner = this.contact.partner ?? null;
  114. modalRefPosting.componentInstance.posting = posting;
  115. modalRefPosting.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  116. if (modalStatus === ModalStatus.Submitted) {
  117. modalRefPosting.dismiss();
  118. this.getPostsData();
  119. }
  120. });
  121. }
  122. openModalNewComment(post: PostJsonld) {
  123. const modalRefComment = this.modalService.open(NewCommentComponent, this.modalOptions);
  124. let comment: CommentJsonld = {} as CommentJsonld;
  125. comment.post = post.id ?? null;
  126. modalRefComment.componentInstance.comment = comment;
  127. modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  128. if (modalStatus === ModalStatus.Submitted) {
  129. modalRefComment.dismiss();
  130. this.getPostsData();
  131. }
  132. });
  133. }
  134. openModalEditPosting(post: PostJsonld) {
  135. const modalRefPostingEdit = this.modalService.open(NewPostingComponent, this.modalOptions);
  136. modalRefPostingEdit.componentInstance.posting = post;
  137. modalRefPostingEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  138. if (modalStatus === ModalStatus.Submitted) {
  139. modalRefPostingEdit.dismiss();
  140. this.getPostsData();
  141. }
  142. });
  143. }
  144. openModalEditComment(comment: CommentJsonld) {
  145. const modalRefComment = this.modalService.open(NewCommentComponent, this.modalOptions);
  146. modalRefComment.componentInstance.comment = comment;
  147. modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  148. if (modalStatus === ModalStatus.Submitted) {
  149. modalRefComment.dismiss();
  150. this.getPostsData();
  151. }
  152. });
  153. }
  154. openModalEditContact() {
  155. const modalRefContact = this.modalService.open(NewContactComponent, this.modalOptions);
  156. modalRefContact.componentInstance.contact = this.contact;
  157. if (this.contact.birthday !== undefined) {
  158. modalRefContact.componentInstance.birthdayValue = ApiConverter.convertDate(this.contact.birthday);
  159. }
  160. modalRefContact.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  161. if (modalStatus === ModalStatus.Submitted) {
  162. modalRefContact.dismiss();
  163. this.getContactData();
  164. }
  165. });
  166. }
  167. showComments(post: PostJsonld) {
  168. if (post.id) {
  169. const currentVisibility = this.commentsVisibility.get(post.id);
  170. this.commentsVisibility.set(post.id, !currentVisibility);
  171. }
  172. }
  173. }