import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core'; import {environment} from "@environments/environment"; import {CommentJsonld, ContactJsonld, ContactService, PartnerJsonld, PostJsonld, PostService} from "@app/core/api/v1"; import {Subscription} from "rxjs"; import {ActivatedRoute} from "@angular/router"; 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 {ModalComponent} from "@app/_components/modal/modal.component"; import {NgbModal} from "@ng-bootstrap/ng-bootstrap"; import {NewContactComponent} from "@app/contacts/new-contact/new-contact.component"; import {ModalStatus} from "@app/_helpers/modal.states"; import {User} from "@app/_models"; import {AccountService} from "@app/_services"; import {NewCommentComponent} from "@app/postings/new-comment/new-comment.component"; @Component({ selector: 'app-contacts-detail', templateUrl: './contacts-detail.component.html', styleUrl: './contacts-detail.component.scss' }) export class ContactsDetailComponent implements OnInit, AfterViewInit { @ViewChild(MatPaginator) postsPaginator: MatPaginator; protected user: User | null; protected readonly environment = environment; protected contact: ContactJsonld; protected id: string; protected contactSub: Subscription; protected postsSub: Subscription; protected posts: Array; protected postsDataSource; protected postsLength: number; protected postsPageEvent: PageEvent; protected postsPageSize: number; protected postsPageIndex: number; constructor( private contactService: ContactService, private accountService: AccountService, private route: ActivatedRoute, private postService: PostService, private modalService: NgbModal ) { this.user = this.accountService.userValue; this.id = ""; this.contact = {} as ContactJsonld; this.contactSub = new Subscription(); 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.getContactData(); this.getPostsData(); console.log(this.contact.imageUrl); } ngAfterViewInit() { this.postsDataSource.paginator = this.postsPaginator; } getContactData() { // switch over this.dataType (customers, etc.) this.contactSub = this.contactService.contactsIdGet( this.id ).subscribe( data => { this.contact = data; console.log(this.contact); } ); } getPostsData() { this.postsSub = this.postService.postsGetCollection( this.postsPageIndex + 1, this.postsPageSize, this.contact.partner + '', [], this.id ).subscribe( data => { this.posts = data["hydra:member"]; this.postsLength = Number(data["hydra:totalItems"]); } ); } postsHandlePageEvent(e: PageEvent) { this.postsPageEvent = e; this.postsLength = e.length; this.postsPageIndex = e.pageIndex.valueOf(); this.postsPageSize = e.pageSize.valueOf(); this.getPostsData(); } openModalNewPosting() { const modalRefPosting = this.modalService.open(NewPostingComponent); let posting: PostJsonld = {} as PostJsonld; posting.contact = this.contact.id ?? null; posting.partner = this.contact.partner ?? 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); 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(); } }); } openModalEditPosting(post: PostJsonld) { const modalRefPostingEdit = this.modalService.open(NewPostingComponent); modalRefPostingEdit.componentInstance.posting = post; modalRefPostingEdit.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefPostingEdit.dismiss(); this.getPostsData(); } }); } openModalEditComment(comment: CommentJsonld) { console.log(comment); const modalRefComment = this.modalService.open(NewCommentComponent); modalRefComment.componentInstance.comment = comment; modalRefComment.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRefComment.dismiss(); this.getPostsData(); } }); } openModalEditContact() { const modalRef = this.modalService.open(NewContactComponent); modalRef.componentInstance.contact = this.contact; modalRef.componentInstance.submit.subscribe((modalStatus: ModalStatus) => { if (modalStatus === ModalStatus.Submitted) { modalRef.dismiss(); this.getContactData(); } }); } }