import {AfterViewInit, ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core'; import {User} from '@app/_models'; import {AccountService} from '@app/_services'; import {Subscription} from "rxjs"; import { PostJsonld, PostService, TaskJsonld, TaskNoteJsonld, TaskService, UserJsonld, UserService } from "@app/core/api/v1"; import {MatPaginator, MatPaginatorIntl, PageEvent} from "@angular/material/paginator"; import {NgbModal, NgbModalOptions} from "@ng-bootstrap/ng-bootstrap"; import {MatTableDataSource} from "@angular/material/table"; import {NewTaskComponent} from "@app/tasks/new-task/new-task.component"; import {ModalStatus} from "@app/_helpers/modal.states"; import {NewTaskNoteComponent} from "@app/tasks/new-task-note/new-task-note.component"; import {ApiConverter} from "@app/_helpers/api.converter"; @Component({ templateUrl: 'home.component.html', styleUrl: 'home.component.scss' }) export class HomeComponent implements OnInit, AfterViewInit { @ViewChild(MatPaginator) tasksPaginator: MatPaginator; protected user: User | null; protected postSub: Subscription; protected posts: Array; protected usersSub: Subscription; protected users: Array; protected userIsAdmin: boolean; protected readonly ApiConverter = ApiConverter; protected tasksSub: Subscription; protected tasks: Array; protected tasksDataSource; protected tasksLength: number; protected tasksPageEvent: PageEvent; protected tasksPageSize: number; protected tasksPageIndex: number; protected taskNotesVisibility: Map; protected modalOptions: NgbModalOptions = { centered: true }; constructor( private modalService: NgbModal, private accountService: AccountService, private postService: PostService, private userService: UserService, private taskService: TaskService ) { this.user = this.accountService.userValue; // this.accountService.user.subscribe(x => this.user = x); this.postSub = new Subscription(); this.posts = []; this.usersSub = new Subscription(); this.users = []; this.userIsAdmin = false; 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(); } ngOnInit(): void { if (this.user) { this.userIsAdmin = this.user.roles ? this.user.roles.includes('ROLE_ADMIN') : false; } this.postSub = this.postService.postsGetCollection().subscribe( data => { this.posts = data["hydra:member"]; } ); this.usersSub = this.userService.usersGetCollection().subscribe( data => { this.users = data["hydra:member"]; } ); this.getTasksData(); } ngAfterViewInit() { this.tasksDataSource.paginator = this.tasksPaginator; } getTasksData() { console.log(this.user?.id); this.tasksSub = this.taskService.tasksGetCollection( this.tasksPageIndex + 1, this.tasksPageSize, "", [], this.user?.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); } ); } tasksHandlePageEvent(e: PageEvent) { this.tasksPageEvent = e; this.tasksLength = e.length; this.tasksPageIndex = e.pageIndex.valueOf(); this.tasksPageSize = e.pageSize.valueOf(); this.getTasksData(); } openModalNewTask() { const modalRefTask = this.modalService.open(NewTaskComponent, this.modalOptions); let task: TaskJsonld = {} as TaskJsonld; task.partner = 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(); } }); } 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(); } }); } showTaskNotes(task: TaskJsonld) { if (task.id) { const currentVisibility = this.taskNotesVisibility.get(task.id); this.taskNotesVisibility.set(task.id, !currentVisibility); } } }