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

450 строки
16 KiB

  1. import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
  2. import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap";
  3. import {NewContactComponent} from "@app/contacts/new-contact/new-contact.component";
  4. import {ActivatedRoute, Router} from "@angular/router";
  5. import {
  6. CommentJsonld,
  7. ContactJsonld,
  8. ContactService, PartnerFollowJsonld, PartnerFollowService,
  9. PartnerJsonld, PartnerProductJsonld, PartnerProductService,
  10. PartnerService,
  11. PostJsonld,
  12. PostService, TaskJsonld, TaskNoteJsonld, TaskService
  13. } from "@app/core/api/v1";
  14. import {Subscription} from "rxjs";
  15. import {environment} from "@environments/environment";
  16. import {ApiConverter} from "@app/_helpers/api.converter";
  17. import {MatPaginator, MatPaginatorIntl, PageEvent} from "@angular/material/paginator";
  18. import {MatTableDataSource} from "@angular/material/table";
  19. import {NewPostingComponent} from "@app/postings/new-posting/new-posting.component";
  20. import {NewTaskComponent} from "@app/tasks/new-task/new-task.component";
  21. import {ModalStatus} from "@app/_helpers/modal.states";
  22. import {AccountService} from "@app/_services";
  23. import {User} from "@app/_models";
  24. import {NewCommentComponent} from "@app/postings/new-comment/new-comment.component";
  25. import {NewPartnerComponent} from "@app/partners/new-partner/new-partner.component";
  26. import {NewTaskNoteComponent} from "@app/tasks/new-task-note/new-task-note.component";
  27. import {ToggleComponent} from "@app/_components/toggle/toggle.component";
  28. @Component({
  29. selector: 'app-partners-detail',
  30. templateUrl: './partners-detail.component.html',
  31. styleUrl: './partners-detail.component.scss'
  32. })
  33. export class PartnersDetailComponent implements OnInit, AfterViewInit {
  34. @ViewChild("toggleContacts", { static: true }) toggleContacts: ToggleComponent = new ToggleComponent();
  35. @ViewChild("toggleTasks", { static: true }) toggleTasks: ToggleComponent = new ToggleComponent();
  36. @ViewChild("togglePosts", { static: true }) togglePosts: ToggleComponent = new ToggleComponent();
  37. @ViewChild(MatPaginator) contactsPaginator: MatPaginator;
  38. @ViewChild(MatPaginator) tasksPaginator: MatPaginator;
  39. @ViewChild(MatPaginator) postsPaginator: MatPaginator;
  40. protected user: User | null;
  41. protected readonly environment = environment;
  42. protected id: string;
  43. protected partnerDetailSub: Subscription;
  44. protected partner: PartnerJsonld;
  45. protected partnerProductSub: Subscription;
  46. protected partnerProducts: Array<PartnerProductJsonld>;
  47. protected partnerFollowSub: Subscription;
  48. protected partnerFollow: PartnerFollowJsonld | null;
  49. protected contactsSub: Subscription;
  50. protected contacts: Array<ContactJsonld>;
  51. protected contactsDataSource;
  52. protected contactsLength: number;
  53. protected contactsPageEvent: PageEvent;
  54. protected contactsPageSize: number;
  55. protected contactsPageIndex: number;
  56. protected tasksSub: Subscription;
  57. protected tasks: Array<TaskJsonld>;
  58. protected tasksDataSource;
  59. protected tasksLength: number;
  60. protected tasksPageEvent: PageEvent;
  61. protected tasksPageSize: number;
  62. protected tasksPageIndex: number;
  63. protected taskNotesVisibility: Map<string, boolean>;
  64. protected commentsVisibility: Map<string, boolean>;
  65. protected postsSub: Subscription;
  66. protected posts: Array<PostJsonld>;
  67. protected postsDataSource;
  68. protected postsLength: number;
  69. protected postsPageEvent: PageEvent;
  70. protected postsPageSize: number;
  71. protected postsPageIndex: number;
  72. protected modalOptions: NgbModalOptions = {
  73. centered: true
  74. };
  75. constructor(
  76. private router: Router,
  77. private accountService: AccountService,
  78. private modalService: NgbModal,
  79. private route: ActivatedRoute,
  80. private partnerService: PartnerService,
  81. private partnerProductService: PartnerProductService,
  82. private partnerFollowService: PartnerFollowService,
  83. private contactService: ContactService,
  84. private postService: PostService,
  85. private taskService: TaskService,
  86. protected apiConverter: ApiConverter
  87. ) {
  88. this.id = "";
  89. this.partnerDetailSub = new Subscription();
  90. this.partner = {} as PartnerJsonld;
  91. this.partnerProductSub = new Subscription();
  92. this.partnerProducts = [];
  93. this.partnerFollowSub = new Subscription();
  94. this.partnerFollow = null;
  95. this.user = this.accountService.userValue;
  96. this.contactsSub = new Subscription();
  97. this.contacts = [];
  98. this.contactsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  99. this.contactsDataSource = new MatTableDataSource<ContactJsonld>(this.contacts);
  100. this.contactsLength = 0;
  101. this.contactsPageEvent = new PageEvent();
  102. this.contactsPageSize = 6;
  103. this.contactsPageIndex = 0;
  104. this.tasksSub = new Subscription();
  105. this.tasks = [];
  106. this.tasksPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  107. this.tasksDataSource = new MatTableDataSource<TaskJsonld>(this.tasks);
  108. this.tasksLength = 0;
  109. this.tasksPageEvent = new PageEvent();
  110. this.tasksPageSize = 10;
  111. this.tasksPageIndex = 0;
  112. this.taskNotesVisibility = new Map<string, boolean>();
  113. this.postsSub = new Subscription();
  114. this.posts = [];
  115. this.postsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);
  116. this.postsDataSource = new MatTableDataSource<PostJsonld>(this.posts);
  117. this.postsLength = 0;
  118. this.postsPageEvent = new PageEvent();
  119. this.postsPageSize = 10;
  120. this.postsPageIndex = 0;
  121. this.commentsVisibility = new Map<string, boolean>();
  122. }
  123. ngOnInit() {
  124. this.route.params.subscribe(params => {
  125. this.id = params['id'];
  126. });
  127. this.getPartnerData();
  128. this.getPartnerProducts();
  129. this.getContactsData();
  130. this.getPostsData();
  131. this.getTasksData();
  132. this.getPartnerFollowedStatus();
  133. }
  134. ngAfterViewInit() {
  135. this.contactsDataSource.paginator = this.contactsPaginator;
  136. this.tasksDataSource.paginator = this.tasksPaginator;
  137. this.postsDataSource.paginator = this.postsPaginator;
  138. }
  139. getPartnerData() {
  140. this.partnerDetailSub = this.partnerService.partnersIdGet(
  141. this.id
  142. ).subscribe(
  143. data => {
  144. this.partner = data;
  145. }
  146. );
  147. }
  148. getPartnerProducts() {
  149. this.partnerProductSub = this.partnerProductService.partnerProductsGetCollection(
  150. this.contactsPageIndex + 1,
  151. this.contactsPageSize,
  152. this.id
  153. ).subscribe(
  154. data => {
  155. this.partnerProducts = data["hydra:member"];
  156. }
  157. );
  158. }
  159. getContactsData() {
  160. this.contactsSub = this.contactService.contactsGetCollection(
  161. this.contactsPageIndex + 1,
  162. this.contactsPageSize,
  163. this.id
  164. ).subscribe(
  165. data => {
  166. this.contacts = data["hydra:member"];
  167. this.contactsLength = Number(data["hydra:totalItems"]);
  168. if (this.contactsPaginator !== undefined) {
  169. this.contactsPaginator.length = this.contactsLength;
  170. }
  171. }
  172. );
  173. }
  174. getPostsData() {
  175. this.postsSub = this.postService.postsGetCollection(
  176. this.postsPageIndex + 1,
  177. this.postsPageSize,
  178. this.id,
  179. undefined,
  180. undefined,
  181. undefined,
  182. undefined,
  183. undefined,
  184. false,
  185. false
  186. ).subscribe(
  187. data => {
  188. this.posts = data["hydra:member"];
  189. this.postsLength = Number(data["hydra:totalItems"]);
  190. this.posts.forEach(posts => {
  191. if (posts.id) {
  192. this.commentsVisibility.set(posts.id, false);
  193. }
  194. });
  195. }
  196. );
  197. }
  198. getTasksData() {
  199. this.tasksSub = this.taskService.tasksGetCollection(
  200. this.tasksPageIndex + 1,
  201. this.tasksPageSize,
  202. undefined,
  203. undefined,
  204. this.id
  205. ).subscribe(
  206. data => {
  207. this.tasks = data["hydra:member"];
  208. console.log(this.tasks);
  209. this.tasksLength = Number(data["hydra:totalItems"]);
  210. this.tasks.forEach(task => {
  211. if (task.id) {
  212. this.taskNotesVisibility.set(task.id, false);
  213. }
  214. });
  215. }
  216. );
  217. }
  218. contactsHandlePageEvent(e: PageEvent) {
  219. this.contactsPageEvent = e;
  220. this.contactsLength = e.length;
  221. this.contactsPageIndex = e.pageIndex.valueOf();
  222. this.contactsPageSize = e.pageSize.valueOf();
  223. this.getContactsData();
  224. }
  225. tasksHandlePageEvent(e: PageEvent) {
  226. this.tasksPageEvent = e;
  227. this.tasksLength = e.length;
  228. this.tasksPageIndex = e.pageIndex.valueOf();
  229. this.tasksPageSize = e.pageSize.valueOf();
  230. this.getTasksData();
  231. }
  232. postsHandlePageEvent(e: PageEvent) {
  233. this.postsPageEvent = e;
  234. this.postsLength = e.length;
  235. this.postsPageIndex = e.pageIndex.valueOf();
  236. this.postsPageSize = e.pageSize.valueOf();
  237. this.getPostsData();
  238. }
  239. navigateToContactDetails(element: any) {
  240. const contact: ContactJsonld = element as ContactJsonld;
  241. this.router.navigate(['/contacts', this.apiConverter.extractId(contact.id)]);
  242. }
  243. openModalNewContact() {
  244. const modalRefContact = this.modalService.open(NewContactComponent, this.modalOptions);
  245. let contact: ContactJsonld = {} as ContactJsonld;
  246. contact.partner = this.partner.id ?? null;
  247. modalRefContact.componentInstance.contact = contact;
  248. modalRefContact.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  249. if (modalStatus === ModalStatus.Submitted) {
  250. modalRefContact.dismiss();
  251. this.getContactsData();
  252. }
  253. });
  254. }
  255. openModalNewTask() {
  256. const modalRefTask = this.modalService.open(NewTaskComponent, this.modalOptions);
  257. let task: TaskJsonld = {} as TaskJsonld;
  258. task.partner = this.partner.id ?? null;
  259. task.completed = false;
  260. modalRefTask.componentInstance.task = task;
  261. modalRefTask.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  262. if (modalStatus === ModalStatus.Submitted) {
  263. modalRefTask.dismiss();
  264. this.getTasksData();
  265. }
  266. });
  267. }
  268. openModalNewTaskNote(task: TaskJsonld) {
  269. const modalRefTaskNote = this.modalService.open(NewTaskNoteComponent, this.modalOptions);
  270. let taskNote: TaskNoteJsonld = {} as TaskNoteJsonld;
  271. taskNote.task = task.id ?? null;
  272. modalRefTaskNote.componentInstance.taskNote = taskNote;
  273. modalRefTaskNote.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  274. if (modalStatus === ModalStatus.Submitted) {
  275. modalRefTaskNote.dismiss();
  276. this.getTasksData();
  277. }
  278. });
  279. }
  280. openModalNewPosting() {
  281. const modalRefPosting = this.modalService.open(NewPostingComponent, this.modalOptions);
  282. let posting: PostJsonld = {} as PostJsonld;
  283. posting.partner = this.partner.id ?? null;
  284. modalRefPosting.componentInstance.posting = posting;
  285. modalRefPosting.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  286. if (modalStatus === ModalStatus.Submitted) {
  287. modalRefPosting.dismiss();
  288. this.getPostsData();
  289. }
  290. });
  291. }
  292. openModalNewComment(post: PostJsonld) {
  293. const modalRefComment = this.modalService.open(NewCommentComponent, this.modalOptions);
  294. let comment: CommentJsonld = {} as CommentJsonld;
  295. comment.post = post.id ?? null;
  296. modalRefComment.componentInstance.comment = comment;
  297. modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  298. if (modalStatus === ModalStatus.Submitted) {
  299. modalRefComment.dismiss();
  300. this.getPostsData();
  301. }
  302. });
  303. }
  304. openModalEditTask(task: TaskJsonld) {
  305. const modalRefTaskEdit = this.modalService.open(NewTaskComponent, this.modalOptions);
  306. modalRefTaskEdit.componentInstance.task = task;
  307. modalRefTaskEdit.componentInstance.dueAtValue = this.apiConverter.convertDate(task.dueAt);
  308. modalRefTaskEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  309. if (modalStatus === ModalStatus.Submitted) {
  310. modalRefTaskEdit.dismiss();
  311. this.getTasksData();
  312. }
  313. });
  314. }
  315. openModalEditTaskNote(taskNote: TaskNoteJsonld) {
  316. const modalRefTaskNote = this.modalService.open(NewTaskNoteComponent, this.modalOptions);
  317. modalRefTaskNote.componentInstance.taskNote = taskNote;
  318. modalRefTaskNote.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  319. if (modalStatus === ModalStatus.Submitted) {
  320. modalRefTaskNote.dismiss();
  321. this.getTasksData();
  322. }
  323. });
  324. }
  325. openModalEditPosting(post: PostJsonld) {
  326. const modalRefPostingEdit = this.modalService.open(NewPostingComponent, this.modalOptions);
  327. modalRefPostingEdit.componentInstance.posting = post;
  328. modalRefPostingEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  329. if (modalStatus === ModalStatus.Submitted) {
  330. modalRefPostingEdit.dismiss();
  331. this.getPostsData();
  332. }
  333. });
  334. }
  335. openModalEditComment(comment: CommentJsonld) {
  336. const modalRefComment = this.modalService.open(NewCommentComponent, this.modalOptions);
  337. modalRefComment.componentInstance.comment = comment;
  338. modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  339. if (modalStatus === ModalStatus.Submitted) {
  340. modalRefComment.dismiss();
  341. this.getPostsData();
  342. }
  343. });
  344. }
  345. openModalEditPartner() {
  346. const modalRef = this.modalService.open(NewPartnerComponent, this.modalOptions);
  347. modalRef.componentInstance.partner = this.partner;
  348. modalRef.componentInstance.submit.subscribe((modalStatus: ModalStatus) => {
  349. if (modalStatus === ModalStatus.Submitted) {
  350. modalRef.dismiss();
  351. this.getPartnerData();
  352. }
  353. });
  354. }
  355. showTaskNotes(task: TaskJsonld) {
  356. if (task.id) {
  357. const currentVisibility = this.taskNotesVisibility.get(task.id);
  358. this.taskNotesVisibility.set(task.id, !currentVisibility);
  359. }
  360. }
  361. showComments(post: PostJsonld) {
  362. if (post.id) {
  363. const currentVisibility = this.commentsVisibility.get(post.id);
  364. this.commentsVisibility.set(post.id, !currentVisibility);
  365. }
  366. }
  367. getPartnerFollowedStatus() {
  368. this.partnerFollowSub = this.partnerFollowService.partnerFollowsGetCollection(
  369. 1,
  370. 50,
  371. this.id,
  372. undefined,
  373. this.user?.id
  374. ).subscribe(
  375. data => {
  376. let partnerFollows = data["hydra:member"];
  377. if (partnerFollows.length > 0) {
  378. this.partnerFollow = partnerFollows[0];
  379. }
  380. }
  381. );
  382. }
  383. followPartner(event: any) {
  384. if (this.partnerFollow === null) {
  385. this.partnerFollowSub = this.partnerFollowService.partnerFollowsPost(
  386. {
  387. partner: this.partner.id,
  388. contact: null,
  389. partnerProduct: null
  390. } as PartnerFollowJsonld
  391. ).subscribe(
  392. data => {
  393. this.partnerFollow = data;
  394. }
  395. );
  396. } else {
  397. this.partnerFollowSub = this.partnerFollowService.partnerFollowsIdDelete(
  398. this.apiConverter.extractId(this.partnerFollow.id)
  399. ).subscribe(
  400. data => {
  401. this.partnerFollow = null;
  402. }
  403. );
  404. }
  405. }
  406. }