From 313d329636116043e95de2d13e86d54972fb5536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Fri, 15 Mar 2024 07:19:43 +0100 Subject: [PATCH] Use TaskStatus Service instead of duplicate implementation in TaskDashboard --- frontend/package-lock.json | 12 +++--- .../task-dashboard.component.html | 2 +- .../task-dashboard.component.ts | 15 ++++---- .../src/app/tasks/task-status.service.spec.ts | 16 ++++++++ frontend/src/app/tasks/task-status.service.ts | 38 +++++++++++++++++++ 5 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 frontend/src/app/tasks/task-status.service.spec.ts create mode 100644 frontend/src/app/tasks/task-status.service.ts diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 5f35219..2c5b91b 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -7220,9 +7220,9 @@ "dev": true }, "node_modules/follow-redirects": { - "version": "1.15.3", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz", - "integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "dev": true, "funding": [ { @@ -8103,9 +8103,9 @@ } }, "node_modules/ip": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", - "integrity": "sha512-WKa+XuLG1A1R0UWhl2+1XQSi+fZWMsYKffMZTTYsiZaUD8k2yDAj5atimTUD2TZkyCkNEeYE5NhFZmupOGtjYQ==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.1.tgz", + "integrity": "sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==", "dev": true }, "node_modules/ipaddr.js": { diff --git a/frontend/src/app/tasks/task-dashboard/task-dashboard.component.html b/frontend/src/app/tasks/task-dashboard/task-dashboard.component.html index 1a7294c..d8d33bc 100644 --- a/frontend/src/app/tasks/task-dashboard/task-dashboard.component.html +++ b/frontend/src/app/tasks/task-dashboard/task-dashboard.component.html @@ -29,7 +29,7 @@ Finished - + diff --git a/frontend/src/app/tasks/task-dashboard/task-dashboard.component.ts b/frontend/src/app/tasks/task-dashboard/task-dashboard.component.ts index 4429134..df32cb8 100644 --- a/frontend/src/app/tasks/task-dashboard/task-dashboard.component.ts +++ b/frontend/src/app/tasks/task-dashboard/task-dashboard.component.ts @@ -9,6 +9,7 @@ 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"; @Component({ selector: 'app-task-dashboard', @@ -39,7 +40,8 @@ export class TaskDashboardComponent implements OnChanges{ constructor(private taskService: TaskService, private dialog: MatDialog, - private snackbar: MatSnackBar) { + private snackbar: MatSnackBar, + public taskStatusService: TaskStatusService) { } @@ -53,13 +55,8 @@ export class TaskDashboardComponent implements OnChanges{ } getStatusOfTask(task: TaskEntityInfo) { - if(moment(task.deadline, 'YYYY-MM-DDTHH:mm:ss.SSSZ').isBefore(moment())) { - return "🔴"; - } else if(task.finishable && !task.hasPlannedSchedules){ - return "🟠"; - } else { - return "🟢"; - } + const taskStatus = this.taskStatusService.computeTaskStatus(task); + return this.taskStatusService.convertTaskStatusToColor(taskStatus); } deleteTask(deletedTask: TaskEntityInfo) { @@ -108,4 +105,6 @@ export class TaskDashboardComponent implements OnChanges{ data.push(task) this.datasource.data = data; } + + protected readonly TaskStatus = TaskStatus; } diff --git a/frontend/src/app/tasks/task-status.service.spec.ts b/frontend/src/app/tasks/task-status.service.spec.ts new file mode 100644 index 0000000..82e6a85 --- /dev/null +++ b/frontend/src/app/tasks/task-status.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { TaskStatusService } from './task-status.service'; + +describe('TaskStatusService', () => { + let service: TaskStatusService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(TaskStatusService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/frontend/src/app/tasks/task-status.service.ts b/frontend/src/app/tasks/task-status.service.ts new file mode 100644 index 0000000..ed10fe7 --- /dev/null +++ b/frontend/src/app/tasks/task-status.service.ts @@ -0,0 +1,38 @@ +import { Injectable } from '@angular/core'; +import {TaskEntityInfo} from "../../api"; +import * as moment from "moment"; + +export enum TaskStatus { + OK, + WARNING, + ATTENTION, + DEAD +} +@Injectable({ + providedIn: 'root' +}) +export class TaskStatusService { + + constructor() { } + + computeTaskStatus(task: TaskEntityInfo): TaskStatus { + if(!task.finished && task.deadline != null && moment(task.deadline, 'YYYY-MM-DDTHH:mm:ss.SSSZ').isBefore(moment())) { + return TaskStatus.DEAD; + } else if(!task.finished && task.deadline != null && !task.hasPlannedSchedules) { + return TaskStatus.ATTENTION + } else if(!task.finished && !task.hasPlannedSchedules) { + return TaskStatus.WARNING + } else { + return TaskStatus.OK; + } + } + + convertTaskStatusToColor(taskStatus: TaskStatus) { + switch (taskStatus) { + case TaskStatus.OK: return "🟢"; + case TaskStatus.WARNING: return "🟠"; + case TaskStatus.ATTENTION: return "🔴"; + case TaskStatus.DEAD: return "⚫"; + } + } +}