Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

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