timemanager/frontend/src/app/dashboard/active-schedule/active-schedule.component.ts
2023-10-29 09:47:57 +01:00

90 lines
2.9 KiB
TypeScript

import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ScheduleInfo, ScheduleService, TaskOverviewInfo, TaskScheduleStopResponse} from "../../../api";
import {StopActiveScheduleInfo} from "./StopActiveScheduleInfo";
import {TaskOverviewComponent} from "../task-overview/task-overview.component";
import {MatDialog} from "@angular/material/dialog";
import {ForgottenTaskStartDialogComponent} from "../forgotten-task-start-dialog/forgotten-task-start-dialog.component";
@Component({
selector: 'app-active-schedule',
templateUrl: './active-schedule.component.html',
styleUrls: ['./active-schedule.component.css']
})
export class ActiveScheduleComponent implements OnInit{
activeSchedule: ScheduleInfo | undefined
startTime: number = 0;
currentTime: number = 0;
displayTime: string = "00:00:00"
@Output('onStopTask') scheduleStopEmitter = new EventEmitter<StopActiveScheduleInfo>;
@Output('registerForgotten') registerForgottenEmitter = new EventEmitter<TaskScheduleStopResponse>
constructor(private scheduleService: ScheduleService,
private dialog: MatDialog) {
}
updateTime() {
const now = Date.now();
const elapsed = this.activeSchedule != undefined ? now - this.startTime + this.currentTime : this.currentTime;
this.displayTime = this.formatTime(elapsed);
requestAnimationFrame(() => this.updateTime());
}
formatTime(milliseconds: number): string {
const seconds = Math.floor((milliseconds / 1000) % 60);
const minutes = Math.floor((milliseconds / 1000 / 60) % 60);
const hours = Math.floor(milliseconds / 1000 / 60 / 60);
return `${this.padNumber(hours)}:${this.padNumber(minutes)}:${this.padNumber(seconds)}`;
}
padNumber(num: number): string {
return num < 10 ? `0${num}` : `${num}`;
}
ngOnInit(): void {
this.scheduleService.schedulesActiveGet().subscribe({
next: resp => {
if(resp.scheduleID >= 0) {
this.activateSchedule(resp);
}
},
})
}
stopTask(finish: boolean) {
this.scheduleService.schedulesScheduleIDStopFinishPost(this.activeSchedule!.scheduleID, finish).subscribe({
next: resp => {
this.scheduleStopEmitter.emit({
schedule: this.activeSchedule!,
workedMinutes: resp.workTime
})
this.activeSchedule = undefined
}
})
}
activateSchedule(schedule: ScheduleInfo) {
this.activeSchedule = schedule;
this.startTime = new Date(this.activeSchedule.startTime).getTime();
this.updateTime();
}
finishTaskByOverview(task: TaskOverviewInfo) {
this.activeSchedule = undefined
}
openForgettedActivityDialog() {
const dialogRef = this.dialog.open(ForgottenTaskStartDialogComponent, {width: "400px"})
dialogRef.afterClosed().subscribe(res => {
if(res != undefined) {
this.registerForgottenEmitter.emit(res);
}
})
}
}