@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
3
3
import { applyMiddleware , combineReducers , createStore } from 'redux' ;
4
4
import { Provider , connect } from 'react-redux' ;
5
5
import { createLogger } from 'redux-logger' ;
6
+ import thunk from 'redux-thunk' ;
6
7
import { schema , normalize } from 'normalizr' ;
7
8
import uuid from 'uuid/v4' ;
8
9
import './index.css' ;
@@ -98,6 +99,20 @@ function applySetFilter(state, action) {
98
99
return action . filter ;
99
100
}
100
101
102
+ function notificationReducer ( state = { } , action ) {
103
+ switch ( action . type ) {
104
+ case TODO_ADD : {
105
+ return applySetNotifyAboutAddTodo ( state , action ) ;
106
+ }
107
+ default : return state ;
108
+ }
109
+ }
110
+
111
+ function applySetNotifyAboutAddTodo ( state , action ) {
112
+ const { name, id } = action . todo ;
113
+ return { ...state , [ id ] : 'Todo Created: ' + name } ;
114
+ }
115
+
101
116
// action creators
102
117
103
118
function doAddTodo ( id , name ) {
@@ -134,19 +149,28 @@ function getTodo(state, todoId) {
134
149
return state . todoState . entities [ todoId ] ;
135
150
}
136
151
152
+ function getNotifications ( state ) {
153
+ return getArrayOfObject ( state . notificationState ) ;
154
+ }
155
+
156
+ function getArrayOfObject ( object ) {
157
+ return Object . keys ( object ) . map ( key => object [ key ] ) ;
158
+ }
159
+
137
160
// store
138
161
139
162
const rootReducer = combineReducers ( {
140
163
todoState : todoReducer ,
141
164
filterState : filterReducer ,
165
+ notificationState : notificationReducer ,
142
166
} ) ;
143
167
144
168
const logger = createLogger ( ) ;
145
169
146
170
const store = createStore (
147
171
rootReducer ,
148
172
undefined ,
149
- applyMiddleware ( logger )
173
+ applyMiddleware ( thunk , logger )
150
174
) ;
151
175
152
176
// components
@@ -157,6 +181,15 @@ function TodoApp() {
157
181
< ConnectedFilter />
158
182
< ConnectedTodoCreate />
159
183
< ConnectedTodoList />
184
+ < ConnectedNotifications />
185
+ </ div >
186
+ ) ;
187
+ }
188
+
189
+ function Notifications ( { notifications } ) {
190
+ return (
191
+ < div >
192
+ { notifications . map ( note => < div key = { note } > { note } </ div > ) }
160
193
</ div >
161
194
) ;
162
195
}
@@ -278,10 +311,17 @@ function mapDispatchToPropsFilter(dispatch) {
278
311
} ;
279
312
}
280
313
314
+ function mapStateToPropsNotifications ( state , props ) {
315
+ return {
316
+ notifications : getNotifications ( state ) ,
317
+ } ;
318
+ }
319
+
281
320
const ConnectedTodoList = connect ( mapStateToPropsList ) ( TodoList ) ;
282
321
const ConnectedTodoItem = connect ( mapStateToPropsItem , mapDispatchToPropsItem ) ( TodoItem ) ;
283
322
const ConnectedTodoCreate = connect ( null , mapDispatchToPropsCreate ) ( TodoCreate ) ;
284
323
const ConnectedFilter = connect ( null , mapDispatchToPropsFilter ) ( Filter ) ;
324
+ const ConnectedNotifications = connect ( mapStateToPropsNotifications ) ( Notifications ) ;
285
325
286
326
ReactDOM . render (
287
327
< Provider store = { store } >
0 commit comments