-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtasks.ts
35 lines (32 loc) · 866 Bytes
/
tasks.ts
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
export interface Task {
id: string;
task: string;
}
export function getTasks(): Task[] {
try {
let lsTasks = localStorage.getItem("tasks");
if (lsTasks) return JSON.parse(lsTasks);
} catch (e) {}
let tasks = new Array(20).fill(0).map((_, idx) => ({
id: String(idx + 1),
task: `Task #${idx + 1}`,
}));
localStorage.setItem("tasks", JSON.stringify(tasks));
return tasks;
}
export function addTask(taskMsg: string): Task {
let task = {
id: Math.random().toString(32).substring(2),
task: taskMsg,
};
let tasks = getTasks();
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
return task;
}
export function deleteTask(id: string): void {
let tasks = getTasks();
let idx = tasks.findIndex((t) => t.id === id);
tasks.splice(idx, 1);
localStorage.setItem("tasks", JSON.stringify(tasks));
}