Skip to content
Snippets Groups Projects
Commit 85e216f7 authored by Marlène SIMONDANT's avatar Marlène SIMONDANT
Browse files

Refactor

parent eee2829d
No related branches found
No related tags found
1 merge request!983Draft: feat(dashboard) : create dashboard !
Pipeline #123752 passed
......@@ -108,7 +108,7 @@
</div>
<div class="content">
<!-- Terminé table -->
<app-orientations-table [orientations]="orientations.myOrientations" />
<app-orientations-table [type]="'myOrientations'" [orientations]="orientations.myOrientations" />
<!-- If there is no results -->
<div class="empty">
<p class="bold">Vous n'avez pas d’accompagnements terminés.</p>
......
......@@ -74,29 +74,29 @@
</div>
<div>
<app-tag-item
[label]="getStatusLabel(orientation.status)"
[label]="getLabel('status', orientation.status)"
[size]="'small'"
[color]="getStatusColor(orientation.status)"
[color]="getColor('status', orientation.status)"
[iconFolder]="'tags'"
[iconName]="getStatusIcon(orientation.status)"
[iconName]="getIcon('status', orientation.status)"
/>
</div>
<div>
<app-tag-item
*ngIf="type === 'myOrientations'"
[label]="getTypeLabel('rdv')"
[label]="getLabel('type', 'rdv')"
[size]="'small'"
[color]="getTypeColor('rdv')"
[color]="getColor('type', 'rdv')"
[iconFolder]="'tags'"
[iconName]="getTypeIcon('rdv')"
[iconName]="getIcon('type', 'rdv')"
/>
<app-tag-item
*ngIf="type !== 'myOrientations'"
[label]="getActionLabel('rdvTaken')"
[label]="getLabel('action', 'rdvTaken')"
[size]="'small'"
[color]="getActionColor('rdvTaken')"
[color]="getColor('action', 'rdvTaken')"
[iconFolder]="'tags'"
[iconName]="getActionIcon('rdvTaken')"
[iconName]="getIcon('action', 'rdvTaken')"
/>
</div>
<div class="hide-on-mobile">🢒</div>
......
import { Component, Input } from '@angular/core';
import { Action, ActionsKey, Status, StatusKey, Type, TypeKey } from '../orientation.enum';
import { Action, Status, Type } from '../orientation.enum';
type AllowedColor = 'white' | 'black' | 'red' | 'blue' | 'dark-blue' | 'grey' | 'green' | 'light-green';
@Component({
selector: 'app-orientations-table',
......@@ -8,21 +10,25 @@ import { Action, ActionsKey, Status, StatusKey, Type, TypeKey } from '../orienta
})
export class OrientationsTableComponent {
@Input() orientations: any[];
@Input() type: string;
// Centralized list of allowed colors
private readonly allowedColors: (
| 'white'
| 'black'
| 'red'
| 'blue'
| 'dark-blue'
| 'grey'
| 'green'
| 'light-green'
)[] = ['white', 'black', 'red', 'blue', 'dark-blue', 'grey', 'green', 'light-green'];
@Input() type?: string;
private readonly allowedColors: AllowedColor[] = [
'white',
'black',
'red',
'blue',
'dark-blue',
'grey',
'green',
'light-green',
];
private readonly enumMap = {
status: Status,
action: Action,
type: Type,
};
// Calculate the number of days between the current date and a given date.
public daysAgo(date: string | Date): number {
const createdDate = new Date(date);
const currentDate = new Date();
......@@ -30,67 +36,21 @@ export class OrientationsTableComponent {
return Math.floor(timeDifference / (1000 * 60 * 60 * 24));
}
// Get the label for a given status slug.
public getStatusLabel(slug: string): string {
return this.getPropertyFromEnum(Status, slug as StatusKey, 'label', slug);
}
// Get the color for a given status slug.
public getStatusColor(
slug: string,
): 'white' | 'black' | 'red' | 'blue' | 'dark-blue' | 'grey' | 'green' | 'light-green' {
const color = this.getPropertyFromEnum(Status, slug as StatusKey, 'color', null);
return this.allowedColors.includes(color as any) ? (color as any) : 'grey';
}
// Get the icon for a given status slug.
public getStatusIcon(slug: string): string {
return this.getPropertyFromEnum(Status, slug as StatusKey, 'icon', 'default-icon');
}
// Get the label for a given action slug.
public getActionLabel(slug: string): string {
return this.getPropertyFromEnum(Action, slug as ActionsKey, 'label', slug);
}
// Get the color for a given action slug.
public getActionColor(
slug: string,
): 'white' | 'black' | 'red' | 'blue' | 'dark-blue' | 'grey' | 'green' | 'light-green' {
const color = this.getPropertyFromEnum(Action, slug as ActionsKey, 'color', null);
return this.allowedColors.includes(color as any) ? (color as any) : 'grey';
}
// Get the icon for a given action slug.
public getActionIcon(slug: string): string {
return this.getPropertyFromEnum(Action, slug as ActionsKey, 'icon', 'default-icon');
}
// Get the label for a given type slug.
public getTypeLabel(slug: string): string {
return this.getPropertyFromEnum(Type, slug as TypeKey, 'label', slug);
public getLabel(category: 'status' | 'action' | 'type', slug: string): string {
return this.getProperty(category, slug, 'label', slug);
}
// Get the color for a given type slug.
public getTypeColor(
slug: string,
): 'white' | 'black' | 'red' | 'blue' | 'dark-blue' | 'grey' | 'green' | 'light-green' {
const color = this.getPropertyFromEnum(Type, slug as TypeKey, 'color', null);
return this.allowedColors.includes(color as any) ? (color as any) : 'grey';
public getColor(category: 'status' | 'action' | 'type', slug: string): AllowedColor {
const color = this.getProperty(category, slug, 'color', null);
return this.allowedColors.includes(color as AllowedColor) ? (color as AllowedColor) : 'grey';
}
// Get the icon for a given type slug.
public getTypeIcon(slug: string): string {
return this.getPropertyFromEnum(Type, slug as TypeKey, 'icon', 'default-icon');
public getIcon(category: 'status' | 'action' | 'type', slug: string): string {
return this.getProperty(category, slug, 'icon', 'default-icon');
}
// Generic helper method to fetch properties (label, color, icon) from the enum object.
private getPropertyFromEnum<T extends string | number | symbol>(
enumObj: Record<T, any>,
key: T,
property: string,
fallback: any,
): any {
return enumObj[key]?.[property] || fallback;
private getProperty(category: 'status' | 'action' | 'type', slug: string, property: string, fallback: any): any {
const enumObj = this.enumMap[category];
return enumObj[slug as keyof typeof enumObj]?.[property] || fallback;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment