1
1
import React , { PureComponent } from 'react' ;
2
- import { remove } from 'lodash' ;
2
+ import { remove , each } from 'lodash' ;
3
+ import { v4 as uuid } from 'uuid' ;
3
4
4
5
import TodoForm from './TodoForm' ;
5
6
import TodoList from './TodoList' ;
@@ -16,15 +17,31 @@ class App extends PureComponent {
16
17
17
18
this . handleTodoAdd = this . handleTodoAdd . bind ( this ) ;
18
19
this . handleTodoRemove = this . handleTodoRemove . bind ( this ) ;
20
+ this . handleTodoToggle = this . handleTodoToggle . bind ( this ) ;
19
21
}
20
22
21
- handleTodoAdd ( todo ) {
23
+ handleTodoAdd ( todoDescription ) {
24
+ const todo = {
25
+ id : uuid . v4 ( ) ,
26
+ label : todoDescription ,
27
+ completed : false ,
28
+ } ;
22
29
this . setState ( { todos : [ todo , ...this . state . todos ] } ) ;
23
30
}
24
31
25
- handleTodoRemove ( todo ) {
32
+ handleTodoRemove ( todoId ) {
33
+ const todos = [ ...this . state . todos ] ;
34
+ remove ( todos , t => t . id === todoId ) ;
35
+ this . setState ( { todos} ) ;
36
+ }
37
+
38
+ handleTodoToggle ( todoId ) {
26
39
const todos = [ ...this . state . todos ] ;
27
- remove ( todos , t => t === todo ) ;
40
+ each ( todos , ( todo ) => {
41
+ if ( todo . id === todoId ) {
42
+ todo . completed = ! todo . completed ;
43
+ }
44
+ } ) ;
28
45
this . setState ( { todos} ) ;
29
46
}
30
47
@@ -35,7 +52,11 @@ class App extends PureComponent {
35
52
< div className = { styles . app } >
36
53
< h2 > My Todo List</ h2 >
37
54
< TodoForm handleTodoAdd = { this . handleTodoAdd } />
38
- < TodoList todos = { todos } handleTodoRemove = { this . handleTodoRemove } />
55
+ < TodoList
56
+ todos = { todos }
57
+ handleTodoRemove = { this . handleTodoRemove }
58
+ handleTodoToggle = { this . handleTodoToggle }
59
+ />
39
60
</ div >
40
61
) ;
41
62
}
0 commit comments