import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core'; import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap"; import {NewContactComponent} from "@app/contacts/new-contact/new-contact.component"; import {ActivatedRoute, Router} from "@angular/router"; import { CommentJsonld, ContactJsonld, ContactService, PartnerFollowJsonld, PartnerFollowService, PartnerJsonld, PartnerService, PostJsonld, PostService, TaskJsonld, TaskNoteJsonld, TaskService } from "@app/core/api/v1"; import {Subscription} from "rxjs"; import {environment} from "@environments/environment"; import {ApiConverter} from "@app/_helpers/api.converter"; import {MatPaginator, MatPaginatorIntl, PageEvent} from "@angular/material/paginator"; import {MatTableDataSource} from "@angular/material/table"; import {NewPostingComponent} from "@app/postings/new-posting/new-posting.component"; import {NewTaskComponent} from "@app/tasks/new-task/new-task.component"; import {ModalStatus} from "@app/_helpers/modal.states"; import {AccountService} from "@app/_services"; import {User} from "@app/_models"; import {NewCommentComponent} from "@app/postings/new-comment/new-comment.component"; import {NewPartnerComponent} from "@app/partners/new-partner/new-partner.component"; import {NewTaskNoteComponent} from "@app/tasks/new-task-note/new-task-note.component"; @Component({ selector: 'app-partners-detail', templateUrl: './partners-detail.component.html', styleUrl: './partners-detail.component.scss' }) export class PartnersDetailComponent implements OnInit, AfterViewInit { @ViewChild(MatPaginator) contactsPaginator: MatPaginator; @ViewChild(MatPaginator) tasksPaginator: MatPaginator; @ViewChild(MatPaginator) postsPaginator: MatPaginator; protected user: User | null; protected userFollows: boolean; protected readonly ApiConverter = ApiConverter; protected readonly environment = environment; protected id: string; protected partnerDetailSub: Subscription; protected partner: PartnerJsonld; protected partnerFollowSub: Subscription; protected partnerFollow: PartnerFollowJsonld protected contactsSub: Subscription; protected contacts: Array; protected contactsDataSource; protected contactsLength: number; protected contactsPageEvent: PageEvent; protected contactsPageSize: number; protected contactsPageIndex: number; protected tasksSub: Subscription; protected tasks: Array; protected tasksDataSource; protected tasksLength: number; protected tasksPageEvent: PageEvent; protected tasksPageSize: number; protected tasksPageIndex: number; protected taskNotesVisibility: Map; protected postsSub: Subscription; protected posts: Array; protected postsDataSource; protected postsLength: number; protected postsPageEvent: PageEvent; protected postsPageSize: number; protected postsPageIndex: number; protected modalOptions: NgbModalOptions = { centered: true }; constructor( private router: Router, private accountService: AccountService, private modalService: NgbModal, private route: ActivatedRoute, private partnerService: PartnerService, private partnerFollowService: PartnerFollowService, private contactService: ContactService, private postService: PostService, private taskService: TaskService ) { this.id = ""; this.userFollows = false; this.partnerDetailSub = new Subscription(); this.partner = {} as PartnerJsonld; this.partnerFollowSub = new Subscription(); this.partnerFollow = {} as PartnerFollowJsonld; this.user = this.accountService.userValue; this.contactsSub = new Subscription(); this.contacts = []; this.contactsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype); this.contactsDataSource = new MatTableDataSource(this.contacts); this.contactsLength = 0; this.contactsPageEvent = new PageEvent(); this.contactsPageSize = 6; this.contactsPageIndex = 0; this.tasksSub = new Subscription(); this.tasks = []; this.tasksPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype); this.tasksDataSource = new MatTableDataSource(this.tasks); this.tasksLength = 0; this.tasksPageEvent = new PageEvent(); this.tasksPageSize = 10; this.tasksPageIndex = 0; this.taskNotesVisibility = new Map(); this.postsSub = new Subscription(); this.posts = []; this.postsPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype); this.postsDataSource = new MatTableDataSource(this.posts); this.postsLength = 0; this.postsPageEvent = new PageEvent(); this.postsPageSize = 10; this.postsPageIndex = 0; } ngOnInit() { this.route.params.subscribe(params => { this.id = params['id']; }); this.getPartnerData(); this.getContactsData(); this.getPostsData(); this.getTasksData(); this.getPartnerFollowedStatus(); } ngAfterViewInit() { this.contactsDataSource.paginator = this.contactsPaginator; this.tasksDataSource.paginator = this.tasksPaginator; this.postsDataSource.paginator = this.postsPaginator; } getPartnerData() { this.partnerDetailSub = this.partnerService.partnersIdGet( this.id ).subscribe( data => { this.partner = data; } ); } getContactsData() { this.contactsSub = this.contactService.contactsGetCollection( this.contactsPageIndex + 1, this.contactsPageSize, this.id ).subscribe( data => { this.contacts = data["hydra:member"]; this.contactsLength = Number(data["hydra:totalItems"]); if (this.contactsPaginator !== undefined) { this.contactsPaginator.length = this.contactsLength; } } ); } getPostsData() { this.postsSub = this.postService.postsGetCollection( this.postsPageIndex + 1, this.postsPageSize, this.id ).subscribe( data => { this.posts = data["hydra:member"]; this.postsLength = Number(data["hydra:totalItems"]); } ); } getTasksData() { this.tasksSub = this.taskService.tasksGetCollection( this.tasksPageIndex + 1, this.tasksPageSize, // TODO: User-ID muss übergeben werden können, damit man nur die Tasks bekommt, die einem User zugewiesen sind this.id ).subscribe( data => { this.tasks = data["hydra:member"]; this.tasksLength = Number(data["hydra:totalItems"]); this.tasks.forEach(task => { if (task.id) { this.taskNotesVisibility.set(task.id, false); } }); console.log(this.tasks); } ); } contactsHandlePageEvent(e: PageEvent) { this.contactsPageEvent = e; this.contactsLength = e.length; this.contactsPageIndex = e.pageIndex.valueOf(); this.contactsPageSize = e.pageSize.valueOf(); this.getContactsData(); } tasksHandlePageEvent(e: PageEvent) { this.tasksPageEvent = e; this.tasksLength = e.length; this.tasksPageIndex = e.pageIndex.valueOf(); this.tasksPageSize = e.pageSize.valueOf(); this.getTasksData(); } postsHandlePageEvent(e: PageEvent) { this.postsPageEvent = e; this.postsLength = e.length; this.postsPageIndex = e.pageIndex.valueOf(); this.postsPageSize = e.pageSize.valueOf(); this.getPostsData(); } navigateToContactDetails(element: any) { const contact: ContactJsonld = element as ContactJsonld; this.router.navigate(['/contacts', ApiConverter.extractId(contact.id)]); } openModalNewContact() { const modalRefContact = this.modalService.open(NewContactComponent, this.modalOptions); let contact: ContactJsonld = {} as ContactJsonld; contact.partner = this.partner.id ?? null; modalRefContact.componentInstance.contact = contact; modalRefContact.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefContact.dismiss(); this.getContactsData(); } }); } openModalNewTask() { const modalRefTask = this.modalService.open(NewTaskComponent, this.modalOptions); let task: TaskJsonld = {} as TaskJsonld; task.partner = this.partner.id ?? null; task.completed = false; modalRefTask.componentInstance.task = task; modalRefTask.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefTask.dismiss(); this.getTasksData(); } }); } openModalNewTaskNote(task: TaskJsonld) { const modalRefTaskNote = this.modalService.open(NewTaskNoteComponent, this.modalOptions); let taskNote: TaskNoteJsonld = {} as TaskNoteJsonld; taskNote.task = task.id ?? null; modalRefTaskNote.componentInstance.taskNote = taskNote; modalRefTaskNote.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefTaskNote.dismiss(); this.getTasksData(); } }); } openModalNewPosting() { const modalRefPosting = this.modalService.open(NewPostingComponent, this.modalOptions); let posting: PostJsonld = {} as PostJsonld; posting.partner = this.partner.id ?? null; modalRefPosting.componentInstance.posting = posting; modalRefPosting.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefPosting.dismiss(); this.getPostsData(); } }); } openModalNewComment(post: PostJsonld) { const modalRefComment = this.modalService.open(NewCommentComponent, this.modalOptions); let comment: CommentJsonld = {} as CommentJsonld; comment.post = post.id ?? null; modalRefComment.componentInstance.comment = comment; modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefComment.dismiss(); this.getPostsData(); } }); } openModalEditTask(task: TaskJsonld) { const modalRefTaskEdit = this.modalService.open(NewTaskComponent, this.modalOptions); modalRefTaskEdit.componentInstance.task = task; modalRefTaskEdit.componentInstance.dueAtValue = ApiConverter.convertDate(task.dueAt); modalRefTaskEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefTaskEdit.dismiss(); this.getTasksData(); } }); } openModalEditTaskNote(taskNote: TaskNoteJsonld) { const modalRefTaskNote = this.modalService.open(NewTaskNoteComponent, this.modalOptions); modalRefTaskNote.componentInstance.taskNote = taskNote; modalRefTaskNote.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefTaskNote.dismiss(); this.getTasksData(); } }); } openModalEditPosting(post: PostJsonld) { const modalRefPostingEdit = this.modalService.open(NewPostingComponent, this.modalOptions); modalRefPostingEdit.componentInstance.posting = post; modalRefPostingEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefPostingEdit.dismiss(); this.getPostsData(); } }); } openModalEditComment(comment: CommentJsonld) { const modalRefComment = this.modalService.open(NewCommentComponent, this.modalOptions); modalRefComment.componentInstance.comment = comment; modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefComment.dismiss(); this.getPostsData(); } }); } openModalEditPartner() { const modalRef = this.modalService.open(NewPartnerComponent, this.modalOptions); modalRef.componentInstance.partner = this.partner; modalRef.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRef.dismiss(); this.getPartnerData(); } }); } showTaskNotes(task: TaskJsonld) { if (task.id) { const currentVisibility = this.taskNotesVisibility.get(task.id); this.taskNotesVisibility.set(task.id, !currentVisibility); } } getPartnerFollowedStatus() { this.partnerFollowSub = this.partnerFollowService.partnerFollowsGetCollection( 1, 50, this.id // TODO: Follow für Partner auf User holen ).subscribe( data => { // this.partnerFollow = data; // TODO: Einblenden! if (this.partnerFollow === null) { this.userFollows == false; } else { this.userFollows == true; } } ); } followPartner(event: any) { if (!this.userFollows) { this.partnerFollowSub = this.partnerFollowService.partnerFollowsPost( { partner: this.partner.id } as PartnerFollowJsonld ).subscribe( data => { console.log(data); this.userFollows = !this.userFollows; } ); } else { if (this.partnerFollow.id !== null && this.partnerFollow.id !== undefined) { this.partnerFollowSub = this.partnerFollowService.partnerFollowsIdDelete( ApiConverter.extractId(this.partnerFollow.id) ).subscribe( data => { console.log(data); this.userFollows = !this.userFollows; } ); } } console.log(this.userFollows); } }