import {Component, Input, OnChanges, OnInit, ViewChild} from '@angular/core'; import {TaskEntityInfo, TaskService} from "../../../api"; import {MatPaginator} from "@angular/material/paginator"; import {MatSort} from "@angular/material/sort"; import {MatTableDataSource} from "@angular/material/table"; import {TaskEditorComponent} from "../task-editor/task-editor.component"; import {TaskEditorData} from "../task-editor/TaskEditorData"; import {MatDialog} from "@angular/material/dialog"; import {MatSnackBar} from "@angular/material/snack-bar"; import {ClearTaskDialogComponent, ClearTaskDialogData} from "../clear-task-dialog/clear-task-dialog.component"; import * as moment from "moment/moment"; import {TaskStatus, TaskStatusService} from "../task-status.service"; import {SelectionModel} from "@angular/cdk/collections"; import {TaskWeeklySeriesCreatorComponent} from "../task-weekly-series-creator/task-weekly-series-creator.component"; @Component({ selector: 'app-task-dashboard', templateUrl: './task-dashboard.component.html', styleUrls: ['./task-dashboard.component.css'] }) export class TaskDashboardComponent implements OnChanges{ ngOnChanges(): void { if(this.subTasks.length == 0 && this.taskgroupID != undefined) { this.fetchTasks() } else if(this.subTasks.length > 0) { this.datasource.data = this.subTasks; this.datasource.paginator = this.paginator!; this.datasource.sort = this.sort!; this.displayedColumns = this.displayedColumns.filter(col => col !== 'select') } } @Input("taskgroupID") taskgroupID: number | undefined @Input("subTasks") subTasks: TaskEntityInfo[] = [] @Input("parentTaskID") parentTaskID: number | undefined @ViewChild(MatPaginator) paginator: MatPaginator | undefined @ViewChild(MatSort) sort: MatSort | undefined displayedColumns: string[] = ['select', 'status', 'name', 'eta', 'start', 'deadline', 'finished', 'edit', 'delete']; datasource: MatTableDataSource = new MatTableDataSource(); selection = new SelectionModel(true, []); /** Whether the number of selected elements matches the total number of rows. */ isAllSelected() { const numSelected = this.selection.selected.length; const numRows = this.datasource.data.length; return numSelected === numRows; } /** Selects all rows if they are not all selected; otherwise clear selection. */ toggleAllRows() { if (this.isAllSelected()) { this.selection.clear(); return; } this.selection.select(...this.datasource.data.filter(task => !task.hasTaskSerie)); } constructor(private taskService: TaskService, private dialog: MatDialog, private snackbar: MatSnackBar, public taskStatusService: TaskStatusService) { } applyFilter(event: Event) { const filterValue = (event.target as HTMLInputElement).value; this.datasource.filter = filterValue.trim().toLowerCase(); if (this.datasource.paginator) { this.datasource.paginator.firstPage(); } } getStatusOfTask(task: TaskEntityInfo) { const taskStatus = this.taskStatusService.computeTaskStatus(task); return this.taskStatusService.convertTaskStatusToColor(taskStatus); } deleteTask(deletedTask: TaskEntityInfo) { this.taskService.tasksTaskIDDelete(deletedTask.taskID).subscribe({ next: resp => { if(resp.status == "success") { this.datasource.data = this.datasource.data.filter(task => task.taskID !== deletedTask.taskID); } else { this.snackbar.open("Unexpected behavior", "", {duration: 2000}); } }, error: err => { if(err.status == 403) { this.snackbar.open("No permission", "", {duration: 2000}); } else if(err.status == 404) { this.snackbar.open("Task not found", "", {duration: 2000}); } else { this.snackbar.open("Unexpected error", "", {duration: 2000}); } } }) } editTask(task: TaskEntityInfo) { const taskEditorInfo: TaskEditorData = { task: task, taskgroupID: this.taskgroupID!, parentTask: undefined }; const dialogRef = this.dialog.open(TaskEditorComponent, {data: taskEditorInfo, minWidth: "400px"}) } clearTasks() { let clearTaskData: ClearTaskDialogData = { tasks: this.datasource.data, taskgroupID: this.taskgroupID, parentTaskID: this.parentTaskID, subtasks: this.subTasks.length > 0 } const dialogRef = this.dialog.open(ClearTaskDialogComponent, {data: clearTaskData, width: "600px"}); dialogRef.afterClosed().subscribe(res => { if(res != undefined) { this.datasource.data = this.datasource.data.filter(task => !res.includes(task)); } }) } addTask(task: TaskEntityInfo) { const data = this.datasource.data; data.push(task) this.datasource.data = data; } protected readonly TaskStatus = TaskStatus; repeatSelectedTasks() { const selectedTasks = this.selection.selected; const dialogRef = this.dialog.open(TaskWeeklySeriesCreatorComponent, { data: selectedTasks, minWidth: "400px" }); dialogRef.afterClosed().subscribe(res => { if(res) { this.fetchTasks() } }) } fetchTasks() { this.taskService.tasksTaskgroupIDStatusGet(this.taskgroupID!, "all").subscribe({ next: resp => { this.datasource = new MatTableDataSource(resp); this.datasource.paginator = this.paginator!; this.datasource.sort = this.sort!; resp.forEach(task => console.log(task)) } }) } }