Repository navigation
Expand file tree
/
Copy pathapp.component.ts
More file actions
93 lines (83 loc) · 2.55 KB
/
Copy pathapp.component.ts
File metadata and controls
93 lines (83 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { Component, computed, effect, signal } from "@angular/core";
import { FormsModule } from "@angular/forms";
type Priority = "high" | "med" | "low";
type Filter = "all" | "active" | "done";
interface Task {
id: string;
title: string;
priority: Priority;
done: boolean;
}
const STORAGE_KEY = "ff-task-tracker-v1";
@Component({
selector: "app-root",
standalone: true,
imports: [FormsModule],
template: `
<main>
<h1>Sprint Tasks ({{ remaining() }} open)</h1>
<form (ngSubmit)="addTask()">
<input [(ngModel)]="title" name="title" placeholder="Add task..." />
<select [(ngModel)]="priority" name="priority">
<option value="high">High</option>
<option value="med">Medium</option>
<option value="low">Low</option>
</select>
<button type="submit">Add</button>
</form>
<div>
@for (f of filters; track f) {
<button (click)="filter.set(f)" [attr.aria-pressed]="filter() === f">{{ f }}</button>
}
</div>
<ul>
@for (t of visible(); track t.id) {
<li>
<label>
<input type="checkbox" [checked]="t.done" (change)="toggle(t.id)" />
<span>[{{ t.priority }}] {{ t.title }}</span>
</label>
<button (click)="remove(t.id)">Delete</button>
</li>
}
</ul>
</main>
`,
})
export class AppComponent {
readonly filters: Filter[] = ["all", "active", "done"];
tasks = signal<Task[]>(this.load());
filter = signal<Filter>("all");
title = "";
priority: Priority = "med";
remaining = computed(() => this.tasks().filter((t) => !t.done).length);
visible = computed(() => {
const f = this.filter();
const list = this.tasks();
if (f === "active") return list.filter((t) => !t.done);
if (f === "done") return list.filter((t) => t.done);
return list;
});
constructor() {
effect(() => localStorage.setItem(STORAGE_KEY, JSON.stringify(this.tasks())));
}
private load(): Task[] {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
} catch {
return [];
}
}
addTask() {
const clean = this.title.trim();
if (!clean) return;
this.tasks.update((prev) => [{ id: crypto.randomUUID(), title: clean, priority: this.priority, done: false }, ...prev]);
this.title = "";
}
toggle(id: string) {
this.tasks.update((prev) => prev.map((t) => (t.id === id ? { ...t, done: !t.done } : t)));
}
remove(id: string) {
this.tasks.update((prev) => prev.filter((t) => t.id !== id));
}
}