mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
moving step2-7 to bonus content
This commit is contained in:
43
bonus-servicecalls/demo/src/reducers/index.ts
Normal file
43
bonus-servicecalls/demo/src/reducers/index.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
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
|
||||
});
|
||||
Reference in New Issue
Block a user