11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33import { connect } from 'react-redux' ;
4- import { addTodo , toggleTodo , removeTodo } from 'actions/todos' ;
4+ import { addTodo , toggleTodo , removeTodo , fetchTodos } from 'actions/todos' ;
55import { Container , Header , Checkbox , List , Button , Form } from 'semantic-ui-react' ;
66import classnames from 'classnames' ;
77import css from './index.scss' ;
@@ -11,11 +11,19 @@ const cx = classnames.bind(css);
1111class TodosContainer extends Component {
1212
1313 static propTypes = {
14- todos : PropTypes . array . isRequired ,
14+ todos : PropTypes . object . isRequired ,
1515 dispatch : PropTypes . func . isRequired
1616 }
1717
18- state = { todoText : null } ;
18+ state = { todoText : '' } ;
19+
20+ componentDidMount ( ) {
21+ const { dispatch, todos : { isFetched } } = this . props ;
22+
23+ if ( ! isFetched ) {
24+ dispatch ( fetchTodos ( ) ) ;
25+ }
26+ }
1927
2028 submitTodo = ( ev ) => {
2129 const { dispatch } = this . props ;
@@ -43,7 +51,7 @@ class TodosContainer extends Component {
4351 }
4452
4553 render ( ) {
46- const { todos } = this . props ;
54+ const { todos : { todos } } = this . props ;
4755 const { todoText } = this . state ;
4856
4957 return (
@@ -63,7 +71,11 @@ class TodosContainer extends Component {
6371 />
6472 </ List . Content >
6573 < List . Content floated = "left" >
66- < Checkbox type = "checkbox" onChange = { ( ) => this . checkTodo ( id ) } />
74+ < Checkbox
75+ type = "checkbox"
76+ checked = { completed }
77+ onChange = { ( ) => this . checkTodo ( id ) }
78+ />
6779 </ List . Content >
6880 < List . Content className = { cx ( css . text , { [ css . completed ] : completed } ) } >
6981 { text }
@@ -76,7 +88,7 @@ class TodosContainer extends Component {
7688 < Form . Group >
7789 < Form . Input
7890 onChange = { this . onTodoChange }
79- value = { todoText }
91+ value = { todoText }
8092 type = "text"
8193 placeholder = "Add a todo..." />
8294 < Form . Button content = "Add" icon = "plus" />
@@ -87,8 +99,10 @@ class TodosContainer extends Component {
8799 }
88100}
89101
90- const mapStateToProps = ( state ) => ( {
91- todos : state . todos
92- } ) ;
102+ TodosContainer . fetchData = ( { store } ) => {
103+ return store . dispatch ( fetchTodos ( ) ) ;
104+ } ;
105+
106+ const mapStateToProps = ( { todos } ) => ( { todos } ) ;
93107
94108export default connect ( mapStateToProps ) ( TodosContainer ) ;
0 commit comments