majorly overhauled 2.8

This commit is contained in:
Ken
2019-02-26 21:27:23 -08:00
parent a84b72ae08
commit fdd6d11791
13 changed files with 214 additions and 251 deletions

View File

@@ -1,33 +1,21 @@
import { Store } from '../store';
import { addTodo, remove, complete, clear, setFilter } from './pureFunctions';
import { combineReducers } from 'redux';
import { createReducer } from 'redux-starter-kit';
function todoReducer(state: Store['todos'] = {}, action: any): Store['todos'] {
switch (action.type) {
case 'addTodo':
return addTodo(state, action.id, action.label);
case 'remove':
return remove(state, action.id);
case 'clear':
return clear(state);
case 'complete':
return complete(state, action.id);
const todoReducer = createReducer<Store['todos']>(
{},
{
addTodo,
remove,
clear,
complete
}
);
return state;
}
function filterReducer(state: Store['filter'] = 'all', action: any): Store['filter'] {
switch (action.type) {
case 'setFilter':
return setFilter(state, action.filter);
}
return state;
}
const filterReducer = createReducer<Store['filter']>('all', {
setFilter
});
export const reducer = combineReducers({
todos: todoReducer,

View File

@@ -5,25 +5,19 @@ describe('TodoApp reducers', () => {
it('can add an item', () => {
const state = <Store['todos']>{};
const newState = addTodo(state, '0', 'item1');
addTodo(state, { id: '0', label: 'item1' });
const keys = Object.keys(newState);
expect(newState).not.toBe(state);
const keys = Object.keys(state);
expect(keys.length).toBe(1);
expect(newState[keys[0]].label).toBe('item1');
expect(newState[keys[0]].completed).toBeFalsy();
expect(state['0'].label).toBe('item1');
expect(state['0'].completed).toBeFalsy();
});
it('can complete an item', () => {
const state = <Store['todos']>{};
let newState = addTodo(state, '0', 'item1');
const key = Object.keys(newState)[0];
newState = complete(newState, key);
expect(newState[key].completed).toBeTruthy();
addTodo(state, { id: '0', label: 'item1' });
complete(state, { id: '0' });
expect(state['0'].completed).toBeTruthy();
});
});

View File

@@ -1,40 +1,25 @@
import { Store, FilterTypes } from '../store';
import { Store } from '../store';
export function addTodo(state: Store['todos'], id: string, label: string): Store['todos'] {
return { ...state, [id]: { label, completed: false } };
export function addTodo(state: Store['todos'], action: any) {
state[action.id] = { label: action.label, completed: false };
}
export function edit(state: Store['todos'], id: string, label: string): Store['todos'] {
return { ...state, [id]: { ...state[id], label } };
export function remove(state: Store['todos'], action: any) {
delete state[action.id];
}
export function remove(state: Store['todos'], id: string) {
const newTodos = { ...state };
delete newTodos[id];
return newTodos;
}
export function complete(state: Store['todos'], id: string) {
const newTodos = { ...state };
newTodos[id].completed = !newTodos[id].completed;
return newTodos;
export function complete(state: Store['todos'], action: any) {
state[action.id].completed = !state[action.id].completed;
}
export function clear(state: Store['todos']) {
const newTodos = { ...state };
Object.keys(state).forEach(key => {
if (state[key].completed) {
delete newTodos[key];
delete state[key];
}
});
return newTodos;
}
export function setFilter(state: Store['filter'], filter: FilterTypes) {
return filter;
export function setFilter(state: Store['filter'], action: any) {
return action.filter;
}