11import React , { Component } from 'react' ;
2+
23import Todo from '../../components/Todo/Todo' ;
3- import TodoDetail from '../../components/TodoDetail/TodoDetail' ;
4+
45import { NavLink } from 'react-router-dom' ;
5- import NewTodo from './NewTodo/NewTodo' ;
6+
67import "./TodoList.css"
78
9+ import { connect } from 'react-redux' ;
10+ import * as actionTypes from '../../store/actions/actionTypes' ;
11+ import * as actionCreators from '../../store/actions/index' ;
12+
13+ import { withRouter } from 'react-router' ;
14+
815class TodoList extends Component {
9- state = {
10- todos : [
11- { id : 1 , title : 'SWPP' , content : 'take swpp class' , done : true } ,
12- { id : 2 , title : 'Movie' , content : 'watch movie' , done : false } ,
13- { id : 3 , title : 'Dinner' , content : 'eat dinner' , done : false } ,
14- ] ,
15- selectedTodo : null ,
16+ componentDidMount ( ) {
17+ this . props . onGetAll ( ) ;
1618 }
1719
18- clickTodoHandler = td => {
19- if ( this . state . selectedTodo === td ) {
20- this . setState ( { selectedTodo : null } ) ;
21- } else {
22- this . setState ( { selectedTodo : td } ) ;
23- }
20+ clickTodoHandler = ( td ) => {
21+ this . props . history . push ( '/todos/' + td . id ) ;
2422 }
2523
2624 render ( ) {
27- const todos = this . state . todos . map ( ( td ) => {
28- return ( < Todo key = { td . id } title = { td . title }
29- done = { td . done } clicked = { ( ) => this . clickTodoHandler ( td ) } /> ) ;
25+ const todos = this . props . storedTodos . map ( ( td ) => {
26+ return (
27+ < Todo
28+ key = { td . id }
29+ title = { td . title }
30+ done = { td . done }
31+ clickDetail = { ( ) => this . clickTodoHandler ( td ) }
32+ clickDone = { ( ) => this . props . onToggleTodo ( td . id ) }
33+ clickDelete = { ( ) => this . props . onDeleteTodo ( td . id ) } />
34+ ) ;
3035 } ) ;
31- let todoDetail = null ;
32- if ( this . state . selectedTodo ) {
33- todoDetail = < TodoDetail title = { this . state . selectedTodo . title }
34- content = { this . state . selectedTodo . content } />
35- }
36+
37+ let todo = null ;
3638 return (
3739 < div className = 'TodoList' >
3840 < div className = 'title' > { this . props . title } </ div >
3941 < div className = 'todos' > { todos } </ div >
40- { todoDetail }
42+ { todo }
4143 < NavLink to = 'new-todo' exact > New Todo</ NavLink >
4244 </ div >
4345 ) ;
4446 }
47+ }
4548
46-
47-
48-
49+ const mapStateToProps = state => {
50+ return {
51+ storedTodos : state . td . todos ,
52+ selectedTodo : state . td . selectedTodo ,
53+ } ;
4954}
50- export default TodoList ;
55+
56+ const mapDispatchToProps = dispatch => {
57+ return {
58+ onToggleTodo : ( id ) => dispatch ( actionCreators . toggleTodo ( id ) ) ,
59+ onDeleteTodo : ( id ) => dispatch ( actionCreators . deleteTodo ( id ) ) ,
60+ onGetAll : ( ) => dispatch ( actionCreators . getTodos ( ) ) ,
61+ onGetTodo : ( id ) => dispatch ( actionCreators . getTodo ( id ) ) ,
62+ } ;
63+ } ;
64+ export default connect ( mapStateToProps , mapDispatchToProps ) ( withRouter ( TodoList ) ) ;
0 commit comments