From 1809a97373fce80f2cee88dc6b381387e7360173 Mon Sep 17 00:00:00 2001 From: faustin Date: Tue, 24 Dec 2024 11:49:10 +0100 Subject: [PATCH] refactor: creating statuses groups --- .../add-statuses-group-dialog.component.ts | 14 ++---- .../edit-status-group-dialog.component.ts | 14 ++---- .../form-statuses-group.component.html | 8 ++-- .../statuses/form-statuses-group.component.ts | 46 ++++++++++--------- .../manage-groups-dialog.component.ts | 4 +- src/app/dashboard/types.ts | 6 +-- 6 files changed, 41 insertions(+), 51 deletions(-) diff --git a/src/app/components/statuses/add-statuses-group-dialog.component.ts b/src/app/components/statuses/add-statuses-group-dialog.component.ts index 8d4e30a62..09da825a9 100644 --- a/src/app/components/statuses/add-statuses-group-dialog.component.ts +++ b/src/app/components/statuses/add-statuses-group-dialog.component.ts @@ -1,7 +1,7 @@ -import { Component, Inject, OnInit } from '@angular/core'; +import { Component, Inject } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { FormStatusesGroupComponent } from './form-statuses-group.component'; -import { AddStatusGroupDialogData, StatusLabeled, TasksStatusesGroup } from '../../dashboard/types'; +import { AddStatusGroupDialogData, TasksStatusesGroup } from '../../dashboard/types'; @Component({ selector: 'app-add-statuses-group-dialog', @@ -10,7 +10,7 @@ import { AddStatusGroupDialogData, StatusLabeled, TasksStatusesGroup } from '../ @@ -25,18 +25,12 @@ import { AddStatusGroupDialogData, StatusLabeled, TasksStatusesGroup } from '../ MatDialogModule, ] }) -export class AddStatusesGroupDialogComponent implements OnInit { - statuses: StatusLabeled[] = []; - +export class AddStatusesGroupDialogComponent { constructor( public _dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: AddStatusGroupDialogData, ) {} - ngOnInit(): void { - this.statuses = this.data.statuses; - } - onSubmit(result: TasksStatusesGroup) { this._dialogRef.close(result); } diff --git a/src/app/components/statuses/edit-status-group-dialog.component.ts b/src/app/components/statuses/edit-status-group-dialog.component.ts index c0e044264..2745cee0f 100644 --- a/src/app/components/statuses/edit-status-group-dialog.component.ts +++ b/src/app/components/statuses/edit-status-group-dialog.component.ts @@ -1,7 +1,7 @@ -import { Component, Inject, OnInit } from '@angular/core'; +import { Component, Inject } from '@angular/core'; import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { FormStatusesGroupComponent } from './form-statuses-group.component'; -import { EditStatusGroupDialogData, StatusLabeled, TasksStatusesGroup } from '../../dashboard/types'; +import { EditStatusGroupDialogData, TasksStatusesGroup } from '../../dashboard/types'; @Component({ selector: 'app-edit-statuses-group-dialog', @@ -10,7 +10,7 @@ import { EditStatusGroupDialogData, StatusLabeled, TasksStatusesGroup } from '.. @@ -25,18 +25,14 @@ import { EditStatusGroupDialogData, StatusLabeled, TasksStatusesGroup } from '.. MatDialogModule, ] }) -export class EditStatusesGroupDialogComponent implements OnInit { +export class EditStatusesGroupDialogComponent { group: TasksStatusesGroup; - statuses: StatusLabeled[] = []; constructor( public _dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: EditStatusGroupDialogData, - ) {} - - ngOnInit(): void { + ) { this.group = {...this.data.group}; - this.statuses = this.data.statuses; } onSubmit(result: TasksStatusesGroup) { diff --git a/src/app/components/statuses/form-statuses-group.component.html b/src/app/components/statuses/form-statuses-group.component.html index 0c999d910..e7372a912 100644 --- a/src/app/components/statuses/form-statuses-group.component.html +++ b/src/app/components/statuses/form-statuses-group.component.html @@ -13,15 +13,15 @@ Color - +

Statuses

- @for (status of statuses; track status.value) { - - {{ status.name }} + @for (status of allStatuses; track status) { + + {{ getLabel(status) }} }
diff --git a/src/app/components/statuses/form-statuses-group.component.ts b/src/app/components/statuses/form-statuses-group.component.ts index 2d67ab783..86e57babc 100644 --- a/src/app/components/statuses/form-statuses-group.component.ts +++ b/src/app/components/statuses/form-statuses-group.component.ts @@ -7,7 +7,7 @@ import { MatDialogModule } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { StatusLabelColor } from '@app/types/status'; -import { StatusLabeled, TasksStatusesGroup } from '../../dashboard/types'; +import { TasksStatusesGroup } from '../../dashboard/types'; @Component({ selector: 'app-form-statuses-group', @@ -47,31 +47,31 @@ mat-dialog-content { }) export class FormStatusesGroupComponent implements OnInit { @Input() group: TasksStatusesGroup | null = null; - @Input({ required: true }) statuses: { name: string, value: StatusLabelColor }[] = []; + @Input({ required: true }) set statuses(entry: Record) { + this.statusesLabelsColor = entry; + this.allStatuses = Object.keys(entry); + } + + private statusesLabelsColor: Record; + allStatuses: string[]; @Output() cancelChange = new EventEmitter(); @Output() submitChange = new EventEmitter(); - groupForm = new FormGroup({ - name: new FormControl(null, [ - Validators.required, - ]), - color: new FormControl(''), - statuses: new FormControl([]) - }); + groupForm: FormGroup; ngOnInit() { - if(this.group) { - this.groupForm.patchValue({ - name: this.group.name, - color: this.group.color ?? null, - statuses: [...this.group.statuses], - }); - } + this.groupForm = new FormGroup({ + name: new FormControl(this.group?.name ?? null, [ + Validators.required, + ]), + color: new FormControl(this.group?.color ?? null), + statuses: new FormControl(this.group?.statuses ? [...this.group.statuses] : []) + }); } - isChecked(status: StatusLabeled): boolean { - return this.group?.statuses.includes(Number(status.value) as TaskStatus) ?? false; + isChecked(status: string): boolean { + return this.group?.statuses.includes(Number(status) as TaskStatus) ?? false; } onCheckboxChange(e: MatCheckboxChange) { @@ -81,10 +81,8 @@ export class FormStatusesGroupComponent implements OnInit { if (e.checked) { statuses.value.push(status); if (!this.groupForm.value.name && statuses.value.length === 1) { - const status = this.statuses.find(status => status.value.label === e.source.value); - if (status) { - this.groupForm.patchValue({name: status.name}); - } + const statusLabelColor = this.statusesLabelsColor[status]; + this.groupForm.patchValue({name: statusLabelColor.label, color: statusLabelColor.color}); } } else { const index = statuses.value.findIndex(s => s === status); @@ -92,6 +90,10 @@ export class FormStatusesGroupComponent implements OnInit { } } + getLabel(value: string) { + return this.statusesLabelsColor[Number(value) as TaskStatus].label; + } + onSubmit() { const result: TasksStatusesGroup = { name: this.groupForm.value.name ?? '', diff --git a/src/app/components/statuses/manage-groups-dialog.component.ts b/src/app/components/statuses/manage-groups-dialog.component.ts index 4564a00f9..1f21d8d48 100644 --- a/src/app/components/statuses/manage-groups-dialog.component.ts +++ b/src/app/components/statuses/manage-groups-dialog.component.ts @@ -168,7 +168,7 @@ export class ManageGroupsDialogComponent { openAddStatusGroupModal(): void { const dialogRef: MatDialogRef = this.dialog.open(AddStatusesGroupDialogComponent, { data: { - statuses: this.tasksStatusesService.statusesRecord(), + statuses: this.tasksStatusesService.statuses, } }); @@ -183,7 +183,7 @@ export class ManageGroupsDialogComponent { const dialogRef: MatDialogRef = this.dialog.open(EditStatusesGroupDialogComponent, { data: { group, - statuses: this.tasksStatusesService.statusesRecord(), + statuses: this.tasksStatusesService.statuses, } }); diff --git a/src/app/dashboard/types.ts b/src/app/dashboard/types.ts index 01d633d72..9feb862f0 100644 --- a/src/app/dashboard/types.ts +++ b/src/app/dashboard/types.ts @@ -50,13 +50,11 @@ export type ManageGroupsDialogResult = { groups: TasksStatusesGroup[]; }; -export type StatusLabeled = { name: string, value: StatusLabelColor }; - export type AddStatusGroupDialogData = { - statuses: StatusLabeled[]; + statuses: Record; }; export type EditStatusGroupDialogData = { group: TasksStatusesGroup; - statuses: StatusLabeled[]; + statuses: Record; };