added demo code to step 5

This commit is contained in:
Ken
2019-03-02 21:44:19 -08:00
parent 15bee6892b
commit 1f4baa2ca4
13 changed files with 128 additions and 154 deletions

View File

@@ -1,20 +1,43 @@
import { Store } from '../store';
import { addTodo, remove, complete, clear } from './pureFunctions';
import { combineReducers } from 'redux';
import { createReducer } from 'redux-starter-kit';
export function reducer(state: Store['todos'], payload: any): Store['todos'] {
switch (payload.type) {
case 'addTodo':
return addTodo(state, payload.id, payload.label);
export const todosReducer = createReducer<Store['todos']>(
{},
{
addTodo(state, action) {
state[action.id] = { label: action.label, completed: false };
},
case 'remove':
return remove(state, payload.id);
remove(state, action) {
delete state[action.id];
},
case 'complete':
return complete(state, payload.id);
clear(state, action) {
state[action.id].completed = !state[action.id].completed;
},
case 'clear':
return clear(state);
complete(state, action) {
Object.keys(state).forEach(key => {
if (state[key].completed) {
delete state[key];
}
});
},
edit(state, action) {
state[action.id].label = action.label;
}
}
);
return state;
}
export const filterReducer = createReducer<Store['filter']>('all', {
setFilter(state, action) {
return action.filter;
}
});
export const reducer = combineReducers({
todos: todosReducer,
filter: filterReducer
});