-
Notifications
You must be signed in to change notification settings - Fork 2
/
store.mjs
62 lines (55 loc) · 1.55 KB
/
store.mjs
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
import { v4 } from 'uuid';
import { proxy, subscribe } from 'valtio';
import { Filters } from './index.mjs';
const KEY = 'todos-htm-preact';
/**
* Attempts to read items from localstorage.
* @returns {Array<Object>} Array of todo items
*/
const read = () => {
let data;
try {
data = JSON.parse(localStorage.getItem(KEY)) || [];
} catch (e) {
console.error('Invalid JSON', e);
data = [];
}
return data;
};
/**
* Writes todo items to localstorage.
* @param {Array<Object} data
*/
const write = (data) => {
localStorage.setItem(KEY, JSON.stringify(data));
};
export const store = proxy({
todos: read(),
reset(todos = []) {
this.todos = todos.map(({ id = v4(), status = Filters.ACTIVE, ...todo }) => ({ id, status, ...todo }));
},
add(name) {
this.todos = [{ id: v4(), status: Filters.ACTIVE, name }, ...this.todos];
},
updateItem(id, change) {
this.todos = this.todos.map((todo) => (todo.id === id ? { ...todo, ...change } : todo));
},
toggleAll(isComplete) {
this.todos = this.todos.map((todo) => ({ ...todo, status: isComplete ? Filters.COMPLETED : Filters.ACTIVE }));
},
toggle(id) {
this.todos = this.todos.map((todo) =>
todo.id === id
? { ...todo, status: todo.status === Filters.COMPLETED ? Filters.ACTIVE : Filters.COMPLETED }
: todo
);
},
clearCompleted() {
this.todos = this.todos.filter(({ status }) => status !== Filters.COMPLETED);
},
remove(removeId) {
this.todos = this.todos.filter(({ id }) => id !== removeId);
}
});
// Write any state change to localStorage
subscribe(store, () => write(store.todos));