Skip to content

Commit 5cc58b2

Browse files
committed
Add support for saving draft as root todo item
1 parent d58eadc commit 5cc58b2

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,44 @@ function createID() {
156156
return `id-${Math.random().toString(36).slice(2, 10)}`;
157157
}
158158

159-
export const addTodo = todoList.edit.add.prepend((inputs: InputTodo[]) => {
160-
function addIds(inputs: InputTodo[]): TodoInputShape[] {
161-
return inputs.map(({ title, subtasks = [] }) => ({
159+
export const addTodo = createAction({
160+
source: $todoDraft,
161+
target: {
162+
add: todoList.edit.add,
163+
todoDraft: $todoDraft,
164+
},
165+
fn(target, todoDraft) {
166+
if (!todoDraft.trim()) return;
167+
target.add({
162168
id: createID(),
163-
title,
164-
subtasks: addIds(subtasks),
165-
}));
166-
}
167-
return addIds(inputs);
169+
title: todoDraft.trim(),
170+
subtasks: [],
171+
});
172+
target.todoDraft.reinit();
173+
},
168174
});
169175

170-
addTodo([
171-
{ title: '🖱 Double-click to edit' },
172-
{ title: 'Effector models' },
173-
{
174-
title: 'Example task',
175-
subtasks: [
176-
{
177-
title: 'subtask #1',
178-
subtasks: [{ title: 'Foo' }, { title: 'Bar' }],
179-
},
180-
{ title: 'subtask #2' },
181-
],
182-
},
183-
]);
176+
function addIds(inputs: InputTodo[]): TodoInputShape[] {
177+
return inputs.map(({ title, subtasks = [] }) => ({
178+
id: createID(),
179+
title,
180+
subtasks: addIds(subtasks),
181+
}));
182+
}
183+
184+
todoList.edit.add(
185+
addIds([
186+
{ title: '🖱 Double-click to edit' },
187+
{ title: 'Effector models' },
188+
{
189+
title: 'Example task',
190+
subtasks: [
191+
{
192+
title: 'subtask #1',
193+
subtasks: [{ title: 'Foo' }, { title: 'Bar' }],
194+
},
195+
{ title: 'subtask #2' },
196+
],
197+
},
198+
]),
199+
);

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
import { ChangeEvent } from 'react';
1+
import type { KeyboardEvent, ChangeEvent } from 'react';
22
import { useUnit } from 'effector-react';
33
import { useEntityList } from '@effector/model-react';
44

5-
import { todoList, $todoDraft, editDraft } from '../model';
5+
import { todoList, $todoDraft, editDraft, addTodo } 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 [todoDraft, onDraftChange] = useUnit([$todoDraft, editDraft]);
13+
const [todoDraft, onDraftChange, saveDraft] = useUnit([
14+
$todoDraft,
15+
editDraft,
16+
addTodo,
17+
]);
1418
const onChangeDraft = (e: ChangeEvent<HTMLInputElement>) => {
1519
onDraftChange(e.target.value);
1620
};
21+
const onAddTodo = (e: KeyboardEvent<HTMLInputElement>) => {
22+
if (e.key !== 'Enter') return;
23+
e.preventDefault();
24+
saveDraft();
25+
};
1726
const onToggleAll = (e: ChangeEvent<HTMLInputElement>) => {
1827
// toggleAll(e.currentTarget.checked);
1928
};
@@ -28,6 +37,7 @@ export const App = () => {
2837
<input
2938
className="new-todo"
3039
placeholder="What needs to be done?"
40+
onKeyDown={onAddTodo}
3141
onChange={onChangeDraft}
3242
value={todoDraft}
3343
/>

0 commit comments

Comments
 (0)