-
Notifications
You must be signed in to change notification settings - Fork 8
/
todos.store.ts
166 lines (159 loc) · 5.3 KB
/
todos.store.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { computed } from '@angular/core';
import { withCallState, withDevtools } from '@angular-architects/ngrx-toolkit';
import {
patchState,
signalStore,
withComputed,
withHooks,
withMethods,
withState,
} from '@ngrx/signals';
import { withEntities } from '@ngrx/signals/entities';
import { withCollectionService } from '@ngx-odm/rxdb/signals';
import { NgxRxdbUtils } from '@ngx-odm/rxdb/utils';
import { Todo, TodosFilter } from '@shared';
import { computedFrom } from 'ngxtension/computed-from';
import { firstPropertyValueOfObject } from 'rxdb/plugins/utils';
import { map, pipe } from 'rxjs';
import { v4 as uuid } from 'uuid';
const { isEmpty, isValidNumber } = NgxRxdbUtils;
export const TodoStore = signalStore(
{ providedIn: 'root' },
withDevtools('todo'),
withState({
newTodo: '',
}),
withEntities<Todo>(),
withCallState(),
// INFO: an instance of RxCollection will be provided by this
withCollectionService<Todo, TodosFilter>({
filter: 'ALL' as TodosFilter,
query: {},
countQuery: { selector: { completed: { $eq: false } } }, // count all remaining todos
}),
// INFO: Function calls in a template
// Over the years, Angular developers have learned to avoid calling functions inside templates because a function re-runs every change detection and used pure pipes instead. This would cause expensive computations to run multiple times unnecessarily if the passed arguments did not change.
// In a signal-based component, this idea no longer applies because the expressions will only re-evaluate as a result of a signal dependency change.
// With signals, we no longer have to care about handling subscriptions. It is absolutely fine to call a signal function in the template since only the part that depends on that signal will be updated.
withComputed(({ countAll, countFiltered, entities, query, newTodo, filter }) => {
const isAddTodoDisabled = computed(() => newTodo().length < 4);
const filtered = computed(() => {
const all = entities();
const queryValue = query();
if (!isEmpty(queryValue)) {
return all;
}
const filterValue = filter();
if (filterValue === 'ALL') {
return all;
}
return all.filter(todo => {
return todo.completed === (filterValue === 'COMPLETED');
});
});
const remaining = computed(() => countFiltered());
const title = computedFrom(
[countAll, remaining],
pipe(
map(([all, rem]) => {
if (!isValidNumber(all) || !isValidNumber(rem)) {
return '';
}
return `(${all - rem}/${all}) Todos done`;
})
)
);
const sortDir = computed(() => {
const curQuery = query();
if (isEmpty(curQuery?.sort)) {
return undefined;
}
return firstPropertyValueOfObject(curQuery?.sort?.[0]);
});
return {
isAddTodoDisabled,
filtered,
remaining,
title,
sortDir,
};
}),
withMethods(store => {
return {
newTodoChange(newTodo: string) {
patchState(store, { newTodo });
},
addTodo(event: Event) {
event.preventDefault();
const elm = event.target as HTMLInputElement;
if (store.isAddTodoDisabled()) {
return;
}
const payload: Todo = {
id: uuid(),
title: store.newTodo().trim(),
completed: false,
createdAt: new Date().toISOString(),
last_modified: Date.now(),
};
elm.value = '';
patchState(store, { newTodo: '' });
store.insert(payload);
},
setEditinigTodo(todo: Todo, event: Event, isEditing: boolean) {
const elm = event.target as HTMLElement;
if (isEditing) {
elm.contentEditable = 'plaintext-only';
elm.focus();
store.setCurrent(todo);
} else {
elm.contentEditable = 'false';
elm.innerText = todo.title;
store.setCurrent(undefined);
}
},
updateEditingTodo(todo: Todo, event: Event) {
event.preventDefault();
const elm = event.target as HTMLElement;
const payload: Todo = {
...todo,
title: elm.innerText.trim(),
last_modified: Date.now(),
};
store.update(payload);
this.setEditinigTodo(payload, event, false);
},
toggleTodo(todo: Todo) {
const payload: Todo = {
...todo,
completed: !todo.completed,
last_modified: Date.now(),
};
store.update(payload);
},
toggleAllTodos(completed: boolean) {
store.updateAllBy({ selector: { completed: { $eq: !completed } } }, { completed });
},
removeTodo(todo: Todo) {
store.remove(todo);
},
removeCompletedTodos() {
store.removeAllBy({ selector: { completed: { $eq: true } } });
},
filterTodos(filter: TodosFilter): void {
const selector =
filter === 'ALL' ? {} : { completed: { $eq: filter === 'COMPLETED' } };
store.updateQueryParams({ selector });
store.updateFilter(filter);
},
sortTodos(dir: 'asc' | 'desc'): void {
store.updateQueryParams({ sort: [{ last_modified: dir }] });
},
};
}),
withHooks({
onInit: store => {
store.sync(); // INFO: sync with remote example call
},
})
);