Skip to content

Commit 5ec77c2

Browse files
committed
feat: add task status options and refactor task item and list components to use dynamic status selection
1 parent a33c9e6 commit 5ec77c2

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

09-services-deep-dive/src/app/tasks/task.model.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
1+
import { InjectionToken, Provider } from "@angular/core";
2+
13
export type TaskStatus = 'OPEN' | 'IN_PROGRESS' | 'DONE';
24

5+
export const TASK_OPTIONS_LIST = new InjectionToken<TaskStatusOption>('task-options-list');
6+
7+
export const TaskStatusOptions: TaskStatusOption = [
8+
{
9+
value: 'open',
10+
taskStatus: 'OPEN',
11+
text: 'Open',
12+
},
13+
{
14+
value: 'in-progress',
15+
taskStatus: 'IN_PROGRESS',
16+
text: 'In-Progress',
17+
},
18+
{
19+
value: 'done',
20+
taskStatus: 'DONE',
21+
text: 'Completed',
22+
},
23+
]
24+
25+
type TaskStatusOption = {
26+
value: 'open' | 'in-progress' | 'done';
27+
taskStatus: TaskStatus;
28+
text: string;
29+
}[]
30+
31+
export const taskStatusOptionsProvider : Provider = {
32+
provide: TASK_OPTIONS_LIST,
33+
useValue: TaskStatusOptions
34+
}
35+
336
export interface Task {
437
id: string;
538
title: string;

09-services-deep-dive/src/app/tasks/tasks-list/task-item/task-item.component.html

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ <h3>{{ task().title }}</h3>
1212
<p>{{ task().description }}</p>
1313
<form (ngSubmit)="onChangeTaskStatus(task().id, status.value)">
1414
<select #status>
15-
<option value="open" [selected]="task().status === 'OPEN'">Open</option>
16-
<option value="in-progress" [selected]="task().status === 'IN_PROGRESS'">
17-
In-Progress
18-
</option>
19-
<option value="done" [selected]="task().status === 'DONE'">
20-
Completed
21-
</option>
15+
<option *ngFor="let option of taskStatusOptions" [value]="option.value" [selected]="task().status === option.taskStatus">{{ option.text }}</option>
2216
</select>
2317
<p>
2418
<button>Change Status</button>

09-services-deep-dive/src/app/tasks/tasks-list/task-item/task-item.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import { Component, computed, inject, input } from '@angular/core';
22
import { FormsModule } from '@angular/forms';
33

4-
import { Task, TaskStatus } from '../../task.model';
4+
import { TASK_OPTIONS_LIST, Task, TaskStatus } from '../../task.model';
55
import { TasksServiceToken } from '../../../../main';
6+
import { CommonModule } from '@angular/common';
67

78
@Component({
89
selector: 'app-task-item',
910
standalone: true,
10-
imports: [FormsModule],
11+
imports: [
12+
FormsModule,
13+
CommonModule
14+
],
1115
templateUrl: './task-item.component.html',
1216
styleUrl: './task-item.component.css',
1317
})
1418
export class TaskItemComponent {
1519
private tasksService = inject(TasksServiceToken);
20+
taskStatusOptions = inject(TASK_OPTIONS_LIST);
1621
task = input.required<Task>();
1722
taskStatus = computed(() => {
1823
switch (this.task().status) {

09-services-deep-dive/src/app/tasks/tasks-list/tasks-list.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ <h2>My Tasks</h2>
33
<p>
44
<select (change)="onChangeTasksFilter(filter.value)" #filter>
55
<option value="all">All</option>
6-
<option value="open">Open</option>
7-
<option value="in-progress">In-Progress</option>
8-
<option value="done">Completed</option>
6+
<option *ngFor="let option of taskStatusOptions" [value]="option.value">{{ option.text }}</option>
97
</select>
108
</p>
119
</header>

09-services-deep-dive/src/app/tasks/tasks-list/tasks-list.component.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { Component, computed, inject, signal } from '@angular/core';
2-
32
import { TaskItemComponent } from './task-item/task-item.component';
4-
import { TasksService } from '../tasks.service';
53
import { TasksServiceToken } from '../../../main';
4+
import { TASK_OPTIONS_LIST, taskStatusOptionsProvider } from '../task.model';
5+
import { CommonModule } from '@angular/common';
66

77
@Component({
88
selector: 'app-tasks-list',
99
standalone: true,
1010
templateUrl: './tasks-list.component.html',
1111
styleUrl: './tasks-list.component.css',
12-
imports: [TaskItemComponent],
12+
imports: [
13+
TaskItemComponent,
14+
CommonModule
15+
],
16+
providers: [
17+
taskStatusOptionsProvider
18+
]
1319
})
1420
export class TasksListComponent {
15-
private tasksService = inject(TasksServiceToken);
21+
private tasksService = inject(TasksServiceToken);
22+
taskStatusOptions = inject(TASK_OPTIONS_LIST);
1623
selectedFilter = signal<string>('all');
1724
tasks = computed(() => {
1825
switch (this.selectedFilter()) {

0 commit comments

Comments
 (0)