Skip to content

Commit d58eadc

Browse files
committed
Implement adding child subtasks
1 parent bbbaadf commit d58eadc

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

apps/tree-todo-list/src/model.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ export const todoList = keyval(() => {
106106
target: $completed,
107107
});
108108

109+
const [addSubtask] = lazy(['event'], () => [
110+
createAction({
111+
source: $todoDraft,
112+
target: { todoDraft: $todoDraft, add: childsList.edit.add },
113+
fn(target, todoDraft) {
114+
target.add({
115+
id: createID(),
116+
title: todoDraft,
117+
subtasks: [],
118+
});
119+
target.todoDraft.reinit();
120+
},
121+
}),
122+
]);
123+
109124
return {
110125
key: 'id',
111126
state: {
@@ -123,6 +138,7 @@ export const todoList = keyval(() => {
123138
saveDraft,
124139
editMode,
125140
toggleCompleted,
141+
addSubtask,
126142
},
127143
optional: ['completed', 'editing', 'titleDraft'],
128144
};
@@ -132,10 +148,18 @@ export const $totalSize = combine(todoList.$items, (items) => {
132148
return items.reduce((acc, { subtasksTotal }) => acc + 1 + subtasksTotal, 0);
133149
});
134150

151+
export const $todoDraft = createStore('');
152+
export const editDraft = createEvent<string>();
153+
sample({ clock: editDraft, target: $todoDraft });
154+
155+
function createID() {
156+
return `id-${Math.random().toString(36).slice(2, 10)}`;
157+
}
158+
135159
export const addTodo = todoList.edit.add.prepend((inputs: InputTodo[]) => {
136160
function addIds(inputs: InputTodo[]): TodoInputShape[] {
137161
return inputs.map(({ title, subtasks = [] }) => ({
138-
id: `id-${Math.random().toString(36).slice(2, 10)}`,
162+
id: createID(),
139163
title,
140164
subtasks: addIds(subtasks),
141165
}));

apps/tree-todo-list/src/view/App.tsx

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
1-
import { KeyboardEvent, ChangeEvent } from 'react';
1+
import { ChangeEvent } from 'react';
22
import { useUnit } from 'effector-react';
33
import { useEntityList } from '@effector/model-react';
44

5-
import { todoList, addTodo } from '../model';
5+
import { todoList, $todoDraft, editDraft } from '../model';
66
import { TodoCount } from './TodoCount';
77
import { TodoFilters } from './TodoFilters';
88
import { TodoItem } from './TodoItem';
99

1010
import './main.css';
1111

1212
export const App = () => {
13-
const [addTodoClicked] = useUnit([addTodo]);
14-
// const draft = useUnit($descriptionDraft);
13+
const [todoDraft, onDraftChange] = useUnit([$todoDraft, editDraft]);
1514
const onChangeDraft = (e: ChangeEvent<HTMLInputElement>) => {
16-
// changeDraft(e.target.value);
17-
};
18-
const onAddTodo = (e: KeyboardEvent<HTMLInputElement>) => {
19-
if (e.key !== 'Enter') return;
20-
e.preventDefault();
21-
const input = e.currentTarget;
22-
if (input.value && input.value.trim()) {
23-
addTodoClicked([
24-
{
25-
title: input.value.trim(),
26-
},
27-
]);
28-
}
15+
onDraftChange(e.target.value);
2916
};
3017
const onToggleAll = (e: ChangeEvent<HTMLInputElement>) => {
3118
// toggleAll(e.currentTarget.checked);
@@ -41,9 +28,8 @@ export const App = () => {
4128
<input
4229
className="new-todo"
4330
placeholder="What needs to be done?"
44-
onKeyDown={onAddTodo}
4531
onChange={onChangeDraft}
46-
value={'---'}
32+
value={todoDraft}
4733
/>
4834
</header>
4935
<section className="main">

apps/tree-todo-list/src/view/TodoItem.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const TodoItem = ({ nesting }: { nesting: number }) => {
2828
remove(id);
2929
};
3030
const onAddChild = () => {
31-
// addTodoFromDraft({ childOf: id });
31+
api.addSubtask();
3232
};
3333
const onEdit = () => api.editMode('on');
3434
const onSave = () => api.saveDraft();

0 commit comments

Comments
 (0)