@@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
33import { applyMiddleware , combineReducers , createStore } from 'redux' ;
44import { Provider , connect } from 'react-redux' ;
55import { createLogger } from 'redux-logger' ;
6+ import thunk from 'redux-thunk' ;
67import { schema , normalize } from 'normalizr' ;
78import uuid from 'uuid/v4' ;
89import './index.css' ;
@@ -98,6 +99,20 @@ function applySetFilter(state, action) {
9899 return action . filter ;
99100}
100101
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+
101116// action creators
102117
103118function doAddTodo ( id , name ) {
@@ -134,19 +149,28 @@ function getTodo(state, todoId) {
134149 return state . todoState . entities [ todoId ] ;
135150}
136151
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+
137160// store
138161
139162const rootReducer = combineReducers ( {
140163 todoState : todoReducer ,
141164 filterState : filterReducer ,
165+ notificationState : notificationReducer ,
142166} ) ;
143167
144168const logger = createLogger ( ) ;
145169
146170const store = createStore (
147171 rootReducer ,
148172 undefined ,
149- applyMiddleware ( logger )
173+ applyMiddleware ( thunk , logger )
150174) ;
151175
152176// components
@@ -157,6 +181,15 @@ function TodoApp() {
157181 < ConnectedFilter />
158182 < ConnectedTodoCreate />
159183 < 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 > ) }
160193 </ div >
161194 ) ;
162195}
@@ -278,10 +311,17 @@ function mapDispatchToPropsFilter(dispatch) {
278311 } ;
279312}
280313
314+ function mapStateToPropsNotifications ( state , props ) {
315+ return {
316+ notifications : getNotifications ( state ) ,
317+ } ;
318+ }
319+
281320const ConnectedTodoList = connect ( mapStateToPropsList ) ( TodoList ) ;
282321const ConnectedTodoItem = connect ( mapStateToPropsItem , mapDispatchToPropsItem ) ( TodoItem ) ;
283322const ConnectedTodoCreate = connect ( null , mapDispatchToPropsCreate ) ( TodoCreate ) ;
284323const ConnectedFilter = connect ( null , mapDispatchToPropsFilter ) ( Filter ) ;
324+ const ConnectedNotifications = connect ( mapStateToPropsNotifications ) ( Notifications ) ;
285325
286326ReactDOM . render (
287327 < Provider store = { store } >
0 commit comments