@@ -4,6 +4,7 @@ import { applyMiddleware, combineReducers, createStore } from 'redux';
4
4
import { Provider , connect } from 'react-redux' ;
5
5
import { createLogger } from 'redux-logger' ;
6
6
import { schema , normalize } from 'normalizr' ;
7
+ import uuid from 'uuid/v4' ;
7
8
import './index.css' ;
8
9
9
10
// schemas
@@ -19,8 +20,8 @@ const FILTER_SET = 'FILTER_SET';
19
20
// reducers
20
21
21
22
const 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' } ,
24
25
] ;
25
26
26
27
const normalizedTodos = normalize ( todos , [ todoSchema ] ) ;
@@ -121,7 +122,51 @@ const store = createStore(
121
122
// components
122
123
123
124
function 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
+ }
125
170
}
126
171
127
172
function TodoList ( { todosAsIds } ) {
@@ -170,8 +215,15 @@ function mapDispatchToPropsItem(dispatch) {
170
215
} ;
171
216
}
172
217
218
+ function mapDispatchToPropsCreate ( dispatch ) {
219
+ return {
220
+ onAddTodo : name => dispatch ( doAddTodo ( uuid ( ) , name ) ) ,
221
+ } ;
222
+ }
223
+
173
224
const ConnectedTodoList = connect ( mapStateToPropsList ) ( TodoList ) ;
174
225
const ConnectedTodoItem = connect ( mapStateToPropsItem , mapDispatchToPropsItem ) ( TodoItem ) ;
226
+ const ConnectedTodoCreate = connect ( null , mapDispatchToPropsCreate ) ( TodoCreate ) ;
175
227
176
228
ReactDOM . render (
177
229
< Provider store = { store } >
0 commit comments