mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
44 lines
942 B
TypeScript
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
|
|
});
|