Repository navigation
Expand file tree
/
Copy pathApp.svelte
More file actions
77 lines (69 loc) · 1.95 KB
/
Copy pathApp.svelte
File metadata and controls
77 lines (69 loc) · 1.95 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
<script lang="ts">
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";
const initial = (): Task[] => {
try {
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
} catch {
return [];
}
};
let tasks = $state<Task[]>(initial());
let title = $state("");
let priority = $state<Priority>("med");
let filter = $state<Filter>("all");
const remaining = $derived(tasks.filter((t) => !t.done).length);
const visible = $derived(
filter === "active" ? tasks.filter((t) => !t.done)
: filter === "done" ? tasks.filter((t) => t.done)
: tasks
);
$effect(() => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(tasks));
});
function addTask(e: SubmitEvent) {
e.preventDefault();
const clean = title.trim();
if (!clean) return;
tasks.unshift({ id: crypto.randomUUID(), title: clean, priority, done: false });
title = "";
}
const remove = (id: string) => {
tasks = tasks.filter((t) => t.id !== id);
};
</script>
<main>
<h1>Sprint Tasks ({remaining} open)</h1>
<form onsubmit={addTask}>
<input bind:value={title} placeholder="Add task..." />
<select bind:value={priority}>
<option value="high">High</option>
<option value="med">Medium</option>
<option value="low">Low</option>
</select>
<button type="submit">Add</button>
</form>
<div>
{#each ["all", "active", "done"] as const as f}
<button onclick={() => (filter = f)} aria-pressed={filter === f}>{f}</button>
{/each}
</div>
<ul>
{#each visible as t (t.id)}
<li>
<label>
<input type="checkbox" bind:checked={t.done} />
<span>[{t.priority}] {t.title}</span>
</label>
<button onclick={() => remove(t.id)}>Delete</button>
</li>
{/each}
</ul>
</main>