adding a redux todo list to playground

This commit is contained in:
Ken
2019-01-24 12:56:23 -08:00
parent 8b0a0f7569
commit a61d89ea14
12 changed files with 212 additions and 16 deletions

View File

@@ -0,0 +1,15 @@
import { Reducer } from 'redux';
import { ActionTypes, TodoAction } from '../actions';
export function createReducer<T>(
initialState: T,
handlers: { [actionType in ActionTypes]?: (state: T, action: TodoAction) => T }
): Reducer<T> {
return function reducer(state = initialState, action: TodoAction): T {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action);
} else {
return state;
}
};
}

View File

@@ -0,0 +1,22 @@
import { createReducer } from './createReducer';
import { Store, FilterTypes } from '../store';
import { combineReducers } from 'redux';
let counter = 0;
export const reducer = combineReducers<Store>({
todos: createReducer(
{},
{
add(state, action) {
const id = String(counter++);
return { ...state, [id]: { label: action.label, completed: false } };
}
}
),
filter: createReducer<FilterTypes>('all', {
filter(state, action) {
return action.filter;
}
})
});