Files
YoutubeDL-Material/src/app/dialogs/update-task-schedule-dialog/update-task-schedule-dialog.component.ts
Isaac Abadi 091f81bb38 Added UI for managing tasks
Added ability to schedule tasks based on timestamp

Fixed mismatched types between frontend and openapi yaml

Simplified imports for several backend components
2022-04-21 03:01:49 -04:00

84 lines
2.4 KiB
TypeScript

import { Component, Inject, OnInit } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { Schedule } from 'api-types';
import { PostsService } from 'app/posts.services';
@Component({
selector: 'app-update-task-schedule-dialog',
templateUrl: './update-task-schedule-dialog.component.html',
styleUrls: ['./update-task-schedule-dialog.component.scss']
})
export class UpdateTaskScheduleDialogComponent implements OnInit {
enabled = true;
recurring = false;
days_of_week = [];
interval = 'daily';
time = null;
date = null;
today = new Date();
constructor(@Inject(MAT_DIALOG_DATA) public data: any, private dialogRef: MatDialogRef<UpdateTaskScheduleDialogComponent>, private postsService: PostsService) {
this.processTask(this.data.task);
this.postsService.getTask(this.data.task.key).subscribe(res => {
this.processTask(res['task']);
});
}
ngOnInit(): void {
}
processTask(task) {
if (!task['schedule']) {
this.enabled = false;
return;
}
const schedule: Schedule = task['schedule'];
this.recurring = schedule['type'] === Schedule.type.RECURRING;
if (this.recurring) {
this.time = `${schedule['data']['hour']}:${schedule['data']['minute']}`;
if (schedule['data']['dayOfWeek']) {
this.days_of_week = schedule['data']['dayOfWeek'];
this.interval = 'weekly';
} else {
this.interval = 'daily';
}
} else {
const schedule_date = new Date(schedule['data']['timestamp']);
this.time = `${schedule_date.getHours()}:${schedule_date.getMinutes()}`
this.date = schedule_date;
}
}
updateTaskSchedule(): void {
if (!this.enabled) {
this.dialogRef.close(null);
return;
}
if (!this.time) {
// needs time!
}
const hours = parseInt(this.time.split(':')[0]);
const minutes = parseInt(this.time.split(':')[1]);
const schedule: Schedule = {type: this.recurring ? Schedule.type.RECURRING : Schedule.type.TIMESTAMP, data: null};
if (this.recurring) {
schedule['data'] = {hour: hours, minute: minutes};
if (this.interval === 'weekly') {
schedule['data']['dayOfWeek'] = this.days_of_week;
}
} else {
this.date.setHours(hours, minutes);
console.log(this.date);
schedule['data'] = {timestamp: this.date.getTime()};
}
this.dialogRef.close(schedule);
}
}