import {Component, Inject, OnInit} from '@angular/core'; import {FormControl, UntypedFormControl, Validators} from "@angular/forms"; import * as _moment from 'moment'; import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from "@angular/material/core"; import {MomentDateAdapter} from "@angular/material-moment-adapter"; import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; import {TaskService} from "../../../api"; import {TaskEditorData} from "./TaskEditorData"; import {MatSnackBar} from "@angular/material/snack-bar"; import * as moment from "moment/moment"; @Component({ selector: 'app-task-editor', templateUrl: './task-editor.component.html', styleUrls: ['./task-editor.component.css'], }) export class TaskEditorComponent implements OnInit { nameCtrl: FormControl = new FormControl('', [Validators.required, Validators.maxLength(255)]) etaCtrl: FormControl = new FormControl(0, [Validators.required, Validators.min(0)]) startDate: FormControl = new FormControl(Date.now(), [Validators.required]) finishable: boolean = true; endDate: FormControl = new FormControl(''); constructor(private dialog: MatDialogRef, private taskService: TaskService, @Inject(MAT_DIALOG_DATA) public editorData: TaskEditorData, private snackbar: MatSnackBar) { } ngOnInit(): void { if(this.editorData.task != undefined) { this.nameCtrl.setValue(this.editorData.task.taskName); this.etaCtrl.setValue(this.editorData.task.eta) this.startDate.setValue(this.editorData.task.startDate); this.endDate.setValue(this.editorData.task.deadline); } } cancel() { this.dialog.close(); } submit() { if(this.editorData.task != undefined) { this.editTask() } else { this.createTask(); } } createTask() { let endDate_formatted: string|undefined = undefined; let startDate_formatted: string|undefined = undefined; if(this.endDate.value !== "") { endDate_formatted = moment(this.endDate.value).format('YYYY-MM-DDTHH:mm:ss.SSSZ'); } if(this.startDate.value !== "") { startDate_formatted = moment(this.startDate.value).format('YYYY-MM-DDTHH:mm:ss.SSSZ'); } this.taskService.tasksTaskgroupIDPut(this.editorData.taskgroupID, { taskName: this.nameCtrl.value, eta: this.etaCtrl.value, startDate: startDate_formatted, deadline: endDate_formatted, finishable: this.finishable }).subscribe({ next: resp => { this.dialog.close(resp); }, error: err => { if(err.status == 403) { this.snackbar.open("No permission", "", {duration: 2000}); } else if(err.status == 404) { this.snackbar.open("Taskgroup not found", "", {duration: 2000}); } else if(err.status == 409) { this.snackbar.open("Task already exists", "", {duration: 2000}); } else { this.snackbar.open("Unexpected error", "", {duration: 3000}); } } }) } editTask() { let endDate_formatted: string|undefined = undefined; let startDate_formatted: string|undefined = undefined; if(this.endDate.value !== "") { endDate_formatted = moment(this.endDate.value).format('YYYY-MM-DDTHH:mm:ss.SSSZ'); } if(this.startDate.value !== "") { startDate_formatted = moment(this.startDate.value).format('YYYY-MM-DDTHH:mm:ss.SSSZ'); } this.taskService.tasksTaskIDPost(this.editorData.task!.taskID, { taskName: this.nameCtrl.value, eta: this.etaCtrl.value, startDate: startDate_formatted, deadline: endDate_formatted, finishable: this.finishable }).subscribe({ next: resp => { this.editorData.task!.taskName = this.nameCtrl.value; this.editorData.task!.eta = this.etaCtrl.value; this.editorData.task!.startDate = this.startDate.value; this.editorData.task!.deadline = this.endDate.value; this.dialog.close(true); }, error: err => { if(err.status == 403) { this.snackbar.open("No permission", "", {duration: 2000}); } else if(err.status == 404) { this.snackbar.open("Taskgroup not found", "", {duration: 2000}); } else if(err.status == 409) { this.snackbar.open("Task already exists", "", {duration: 2000}); } else { this.snackbar.open("Unexpected error", "", {duration: 3000}); } } }) } }