Skip to content

Commit 8e667e3

Browse files
committed
Fix todos-with-undo example to use IDs instead of indices
1 parent 01dc17e commit 8e667e3

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

examples/todos-with-undo/actions.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ export const VisibilityFilters = {
88
SHOW_ACTIVE: 'SHOW_ACTIVE'
99
}
1010

11+
let nextTodoId = 0
12+
1113
export function addTodo(text) {
12-
return { type: ADD_TODO, text }
14+
return {
15+
id: nextTodoId++,
16+
type: ADD_TODO,
17+
text
18+
}
1319
}
1420

15-
export function completeTodo(index) {
16-
return { type: COMPLETE_TODO, index }
21+
export function completeTodo(id) {
22+
return { type: COMPLETE_TODO, id }
1723
}
1824

1925
export function setVisibilityFilter(filter) {

examples/todos-with-undo/components/TodoList.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ export default class TodoList extends Component {
55
render() {
66
return (
77
<ul>
8-
{this.props.todos.map((todo, index) =>
8+
{this.props.todos.map(todo =>
99
<Todo {...todo}
10-
key={index}
11-
onClick={() => this.props.onTodoClick(index)} />
10+
key={todo.id}
11+
onClick={() => this.props.onTodoClick(todo.id)} />
1212
)}
1313
</ul>
1414
)

examples/todos-with-undo/containers/App.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class App extends Component {
1515
onAddSubmit={text => dispatch(addTodo(text))} />
1616
<TodoList
1717
todos={visibleTodos}
18-
onTodoClick={index => dispatch(completeTodo(index))} />
18+
onTodoClick={id => dispatch(completeTodo(id))} />
1919
<Footer
2020
filter={visibilityFilter}
2121
onFilterChange={nextFilter => dispatch(setVisibilityFilter(nextFilter))}

examples/todos-with-undo/reducers.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,38 @@ function visibilityFilter(state = SHOW_ALL, action) {
1313
}
1414
}
1515

16+
function todo(state, action) {
17+
switch (action.type) {
18+
case ADD_TODO:
19+
return {
20+
id: action.id,
21+
text: action.text,
22+
completed: false
23+
}
24+
case COMPLETE_TODO:
25+
if (state.id !== action.id) {
26+
return state
27+
}
28+
return {
29+
...state,
30+
completed: true
31+
}
32+
default:
33+
return state
34+
}
35+
}
36+
1637
function todos(state = [], action) {
1738
switch (action.type) {
1839
case ADD_TODO:
1940
return [
2041
...state,
21-
{
22-
text: action.text,
23-
completed: false
24-
}
42+
todo(undefined, action)
2543
]
2644
case COMPLETE_TODO:
27-
return [
28-
...state.slice(0, action.index),
29-
Object.assign({}, state[action.index], {
30-
completed: true
31-
}),
32-
...state.slice(action.index + 1)
33-
]
45+
return state.map(t =>
46+
todo(t, action)
47+
)
3448
default:
3549
return state
3650
}

0 commit comments

Comments
 (0)