mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
checking in docs for github.io page
This commit is contained in:
10
docs/playground/src/reducers/__tests__/reducers.spec.ts
Normal file
10
docs/playground/src/reducers/__tests__/reducers.spec.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { reducer } from '../index';
|
||||
|
||||
describe('reducers', () => {
|
||||
it('should add item to the list', () => {
|
||||
const newStore = reducer({ todos: {}, filter: 'all' }, { type: 'add', label: 'hello' });
|
||||
const keys = Object.keys(newStore.todos);
|
||||
expect(keys.length).toBe(1);
|
||||
expect(newStore.todos[keys[0]].label).toBe('hello');
|
||||
});
|
||||
});
|
||||
10
docs/playground/src/reducers/createReducer.ts
Normal file
10
docs/playground/src/reducers/createReducer.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { ActionTypes, TodoActionLookup, actions } from '../actions';
|
||||
import { createGenericReducer, HandlerMap, ImmerReducer } from '../redux-utils/reducer';
|
||||
import { Reducer } from 'redux';
|
||||
|
||||
export function createReducer<T, AM extends ActionTypes | never = never>(
|
||||
initialState: T,
|
||||
handlerOrMap: HandlerMap<T, typeof actions> | ImmerReducer<T, TodoActionLookup[AM]>
|
||||
): Reducer<T> {
|
||||
return createGenericReducer<T, typeof actions, AM>(initialState, handlerOrMap);
|
||||
}
|
||||
42
docs/playground/src/reducers/index.ts
Normal file
42
docs/playground/src/reducers/index.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { createReducer } from './createReducer';
|
||||
import { Store, FilterTypes } from '../store';
|
||||
import { combineReducers } from 'redux';
|
||||
|
||||
export const reducer = combineReducers<Store>({
|
||||
todos: createReducer<Store['todos']>(
|
||||
{},
|
||||
{
|
||||
add(draft, action) {
|
||||
draft[action.id] = { label: action.label, completed: false };
|
||||
return draft;
|
||||
},
|
||||
|
||||
remove(draft, action) {
|
||||
delete draft[action.id];
|
||||
return draft;
|
||||
},
|
||||
|
||||
complete(draft, action) {
|
||||
draft[action.id].completed = !draft[action.id].completed;
|
||||
return draft;
|
||||
},
|
||||
|
||||
clear(draft) {
|
||||
Object.keys(draft).forEach(id => {
|
||||
if (draft[id].completed) {
|
||||
delete draft[id];
|
||||
}
|
||||
});
|
||||
return draft;
|
||||
},
|
||||
|
||||
edit(draft, action) {
|
||||
draft[action.id].label = action.label;
|
||||
return draft;
|
||||
}
|
||||
}
|
||||
),
|
||||
filter: createReducer<Store['filter'], 'setFilter'>('all', (draft, action) => {
|
||||
return action.filter as FilterTypes;
|
||||
})
|
||||
});
|
||||
Reference in New Issue
Block a user