Files
frontend-bootcamp/step2-07/demo/src/reducers/index.ts
2019-03-02 22:33:54 -08:00

44 lines
942 B
TypeScript

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