File tree Expand file tree Collapse file tree 4 files changed +38
-18
lines changed Expand file tree Collapse file tree 4 files changed +38
-18
lines changed Original file line number Diff line number Diff line change @@ -8,12 +8,18 @@ export const VisibilityFilters = {
8
8
SHOW_ACTIVE : 'SHOW_ACTIVE'
9
9
}
10
10
11
+ let nextTodoId = 0
12
+
11
13
export function addTodo ( text ) {
12
- return { type : ADD_TODO , text }
14
+ return {
15
+ id : nextTodoId ++ ,
16
+ type : ADD_TODO ,
17
+ text
18
+ }
13
19
}
14
20
15
- export function completeTodo ( index ) {
16
- return { type : COMPLETE_TODO , index }
21
+ export function completeTodo ( id ) {
22
+ return { type : COMPLETE_TODO , id }
17
23
}
18
24
19
25
export function setVisibilityFilter ( filter ) {
Original file line number Diff line number Diff line change @@ -5,10 +5,10 @@ export default class TodoList extends Component {
5
5
render ( ) {
6
6
return (
7
7
< ul >
8
- { this . props . todos . map ( ( todo , index ) =>
8
+ { this . props . todos . map ( todo =>
9
9
< Todo { ...todo }
10
- key = { index }
11
- onClick = { ( ) => this . props . onTodoClick ( index ) } />
10
+ key = { todo . id }
11
+ onClick = { ( ) => this . props . onTodoClick ( todo . id ) } />
12
12
) }
13
13
</ ul >
14
14
)
Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ class App extends Component {
15
15
onAddSubmit = { text => dispatch ( addTodo ( text ) ) } />
16
16
< TodoList
17
17
todos = { visibleTodos }
18
- onTodoClick = { index => dispatch ( completeTodo ( index ) ) } />
18
+ onTodoClick = { id => dispatch ( completeTodo ( id ) ) } />
19
19
< Footer
20
20
filter = { visibilityFilter }
21
21
onFilterChange = { nextFilter => dispatch ( setVisibilityFilter ( nextFilter ) ) }
Original file line number Diff line number Diff line change @@ -13,24 +13,38 @@ function visibilityFilter(state = SHOW_ALL, action) {
13
13
}
14
14
}
15
15
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
+
16
37
function todos ( state = [ ] , action ) {
17
38
switch ( action . type ) {
18
39
case ADD_TODO :
19
40
return [
20
41
...state ,
21
- {
22
- text : action . text ,
23
- completed : false
24
- }
42
+ todo ( undefined , action )
25
43
]
26
44
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
+ )
34
48
default :
35
49
return state
36
50
}
You can’t perform that action at this time.
0 commit comments