@@ -4,6 +4,7 @@ import { applyMiddleware, combineReducers, createStore } from 'redux';
44import { Provider , connect } from 'react-redux' ;
55import { createLogger } from 'redux-logger' ;
66import { schema , normalize } from 'normalizr' ;
7+ import uuid from 'uuid/v4' ;
78import './index.css' ;
89
910// schemas
@@ -19,8 +20,8 @@ const FILTER_SET = 'FILTER_SET';
1920// reducers
2021
2122const todos = [
22- { id : '0' , name : 'learn redux' } ,
23- { id : '1' , name : 'learn mobx' } ,
23+ { id : uuid ( ) , name : 'learn redux' } ,
24+ { id : uuid ( ) , name : 'learn mobx' } ,
2425] ;
2526
2627const normalizedTodos = normalize ( todos , [ todoSchema ] ) ;
@@ -121,7 +122,51 @@ const store = createStore(
121122// components
122123
123124function TodoApp ( ) {
124- return < ConnectedTodoList /> ;
125+ return (
126+ < div >
127+ < ConnectedTodoCreate />
128+ < ConnectedTodoList />
129+ </ div >
130+ ) ;
131+ }
132+
133+ class TodoCreate extends React . Component {
134+ constructor ( props ) {
135+ super ( props ) ;
136+
137+ this . state = {
138+ value : '' ,
139+ } ;
140+
141+ this . onCreateTodo = this . onCreateTodo . bind ( this ) ;
142+ this . onChangeName = this . onChangeName . bind ( this ) ;
143+ }
144+
145+ onChangeName ( event ) {
146+ this . setState ( { value : event . target . value } ) ;
147+ }
148+
149+ onCreateTodo ( event ) {
150+ this . props . onAddTodo ( this . state . value ) ;
151+ this . setState ( { value : '' } ) ;
152+ event . preventDefault ( ) ;
153+ }
154+
155+ render ( ) {
156+ return (
157+ < div >
158+ < form onSubmit = { this . onCreateTodo } >
159+ < input
160+ type = "text"
161+ placeholder = "Add Todo..."
162+ value = { this . state . value }
163+ onChange = { this . onChangeName }
164+ />
165+ < button type = "submit" > Add</ button >
166+ </ form >
167+ </ div >
168+ ) ;
169+ }
125170}
126171
127172function TodoList ( { todosAsIds } ) {
@@ -170,8 +215,15 @@ function mapDispatchToPropsItem(dispatch) {
170215 } ;
171216}
172217
218+ function mapDispatchToPropsCreate ( dispatch ) {
219+ return {
220+ onAddTodo : name => dispatch ( doAddTodo ( uuid ( ) , name ) ) ,
221+ } ;
222+ }
223+
173224const ConnectedTodoList = connect ( mapStateToPropsList ) ( TodoList ) ;
174225const ConnectedTodoItem = connect ( mapStateToPropsItem , mapDispatchToPropsItem ) ( TodoItem ) ;
226+ const ConnectedTodoCreate = connect ( null , mapDispatchToPropsCreate ) ( TodoCreate ) ;
175227
176228ReactDOM . render (
177229 < Provider store = { store } >
0 commit comments