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, PartnerProductJsonld, PartnerProductService, 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"; import {ToggleComponent} from "@app/_components/toggle/toggle.component"; @Component({ selector: 'app-partners-detail', templateUrl: './partners-detail.component.html', styleUrl: './partners-detail.component.scss' }) export class PartnersDetailComponent implements OnInit, AfterViewInit { @ViewChild("toggleContacts", { static: true }) toggleContacts: ToggleComponent = new ToggleComponent(); @ViewChild("toggleTasks", { static: true }) toggleTasks: ToggleComponent = new ToggleComponent(); @ViewChild("togglePosts", { static: true }) togglePosts: ToggleComponent = new ToggleComponent(); @ViewChild(MatPaginator) contactsPaginator: MatPaginator; @ViewChild(MatPaginator) tasksPaginator: MatPaginator; @ViewChild(MatPaginator) postsPaginator: MatPaginator; protected user: User | null; protected readonly environment = environment; protected id: string; protected partnerDetailSub: Subscription; protected partner: PartnerJsonld; protected partnerProductSub: Subscription; protected partnerProducts: Array; protected partnerFollowSub: Subscription; protected partnerFollow: PartnerFollowJsonld | null; 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 commentsVisibility: 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 partnerProductService: PartnerProductService, private partnerFollowService: PartnerFollowService, private contactService: ContactService, private postService: PostService, private taskService: TaskService, protected apiConverter: ApiConverter ) { this.id = ""; this.partnerDetailSub = new Subscription(); this.partner = {} as PartnerJsonld; this.partnerProductSub = new Subscription(); this.partnerProducts = []; this.partnerFollowSub = new Subscription(); this.partnerFollow = null; 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; this.commentsVisibility = new Map(); } ngOnInit() { this.route.params.subscribe(params => { this.id = params['id']; }); this.getPartnerData(); this.getPartnerProducts(); 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; } ); } getPartnerProducts() { this.partnerProductSub = this.partnerProductService.partnerProductsGetCollection( this.contactsPageIndex + 1, this.contactsPageSize, this.id ).subscribe( data => { this.partnerProducts = data["hydra:member"]; } ); } 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, undefined, undefined, undefined, undefined, undefined, false, false ).subscribe( data => { this.posts = data["hydra:member"]; this.postsLength = Number(data["hydra:totalItems"]); this.posts.forEach(posts => { if (posts.id) { this.commentsVisibility.set(posts.id, false); } }); } ); } getTasksData() { this.tasksSub = this.taskService.tasksGetCollection( this.tasksPageIndex + 1, this.tasksPageSize, undefined, undefined, this.id ).subscribe( data => { this.tasks = data["hydra:member"]; console.log(this.tasks); this.tasksLength = Number(data["hydra:totalItems"]); this.tasks.forEach(task => { if (task.id) { this.taskNotesVisibility.set(task.id, false); } }); } ); } 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', this.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 = this.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); } } showComments(post: PostJsonld) { if (post.id) { const currentVisibility = this.commentsVisibility.get(post.id); this.commentsVisibility.set(post.id, !currentVisibility); } } getPartnerFollowedStatus() { this.partnerFollowSub = this.partnerFollowService.partnerFollowsGetCollection( 1, 50, this.id, undefined, this.user?.id ).subscribe( data => { let partnerFollows = data["hydra:member"]; if (partnerFollows.length > 0) { this.partnerFollow = partnerFollows[0]; } } ); } followPartner(event: any) { if (this.partnerFollow === null) { this.partnerFollowSub = this.partnerFollowService.partnerFollowsPost( { partner: this.partner.id, contact: null, partnerProduct: null } as PartnerFollowJsonld ).subscribe( data => { this.partnerFollow = data; } ); } else { this.partnerFollowSub = this.partnerFollowService.partnerFollowsIdDelete( this.apiConverter.extractId(this.partnerFollow.id) ).subscribe( data => { this.partnerFollow = null; } ); } } }