@@ -7,6 +7,14 @@ import { schema, normalize } from 'normalizr';
77import uuid from 'uuid/v4' ;
88import './index.css' ;
99
10+ // filters
11+
12+ const VISIBILITY_FILTERS = {
13+ SHOW_COMPLETED : item => item . completed ,
14+ SHOW_INCOMPLETED : item => ! item . completed ,
15+ SHOW_ALL : item => true ,
16+ } ;
17+
1018// schemas
1119
1220const todoSchema = new schema . Entity ( 'todo' ) ;
@@ -97,7 +105,10 @@ function doSetFilter(filter) {
97105// selectors
98106
99107function getTodosAsIds ( state ) {
100- return state . todoState . ids ;
108+ return state . todoState . ids
109+ . map ( id => state . todoState . entities [ id ] )
110+ . filter ( VISIBILITY_FILTERS [ state . filterState ] )
111+ . map ( todo => todo . id ) ;
101112}
102113
103114function getTodo ( state , todoId ) {
@@ -124,12 +135,33 @@ const store = createStore(
124135function TodoApp ( ) {
125136 return (
126137 < div >
138+ < ConnectedFilter />
127139 < ConnectedTodoCreate />
128140 < ConnectedTodoList />
129141 </ div >
130142 ) ;
131143}
132144
145+ function Filter ( { onSetFilter } ) {
146+ return (
147+ < div >
148+ Show
149+ < button
150+ type = "text"
151+ onClick = { ( ) => onSetFilter ( 'SHOW_ALL' ) } >
152+ All</ button >
153+ < button
154+ type = "text"
155+ onClick = { ( ) => onSetFilter ( 'SHOW_COMPLETED' ) } >
156+ Completed</ button >
157+ < button
158+ type = "text"
159+ onClick = { ( ) => onSetFilter ( 'SHOW_INCOMPLETED' ) } >
160+ Incompleted</ button >
161+ </ div >
162+ ) ;
163+ }
164+
133165class TodoCreate extends React . Component {
134166 constructor ( props ) {
135167 super ( props ) ;
@@ -221,9 +253,16 @@ function mapDispatchToPropsCreate(dispatch) {
221253 } ;
222254}
223255
256+ function mapDispatchToPropsFilter ( dispatch ) {
257+ return {
258+ onSetFilter : filterType => dispatch ( doSetFilter ( filterType ) ) ,
259+ } ;
260+ }
261+
224262const ConnectedTodoList = connect ( mapStateToPropsList ) ( TodoList ) ;
225263const ConnectedTodoItem = connect ( mapStateToPropsItem , mapDispatchToPropsItem ) ( TodoItem ) ;
226264const ConnectedTodoCreate = connect ( null , mapDispatchToPropsCreate ) ( TodoCreate ) ;
265+ const ConnectedFilter = connect ( null , mapDispatchToPropsFilter ) ( Filter ) ;
227266
228267ReactDOM . render (
229268 < Provider store = { store } >
0 commit comments