mirror of
https://github.com/microsoft/frontend-bootcamp.git
synced 2026-01-26 14:56:42 +08:00
adding some redux steps
This commit is contained in:
3
step2-06/src/actions/index.ts
Normal file
3
step2-06/src/actions/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const addTodo = (label: string) => ({ type: 'addTodo', label });
|
||||
export const remove = (id: string) => ({ type: 'remove', id });
|
||||
export const complete = (id: string) => ({ type: 'complete', id });
|
||||
15
step2-06/src/index.tsx
Normal file
15
step2-06/src/index.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import { reducer } from './reducers';
|
||||
import { createStore, compose } from 'redux';
|
||||
import { addTodo } from './actions';
|
||||
|
||||
declare var window: any;
|
||||
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
||||
|
||||
const store = createStore(reducer, {}, composeEnhancers());
|
||||
|
||||
console.log(store.getState());
|
||||
|
||||
store.dispatch(addTodo('hello'));
|
||||
store.dispatch(addTodo('world'));
|
||||
|
||||
console.log(store.getState());
|
||||
19
step2-06/src/reducers/index.ts
Normal file
19
step2-06/src/reducers/index.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Store } from '../store';
|
||||
import { addTodo, remove, complete } from './pureFunctions';
|
||||
|
||||
let index = 0;
|
||||
|
||||
export function reducer(state: Store, payload: any): Store {
|
||||
switch (payload.type) {
|
||||
case 'addTodo':
|
||||
return addTodo(state, payload.label);
|
||||
|
||||
case 'remove':
|
||||
return remove(state, payload.id);
|
||||
|
||||
case 'complete':
|
||||
return complete(state, payload.id);
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
20
step2-06/src/reducers/pureFunctions.spec.ts
Normal file
20
step2-06/src/reducers/pureFunctions.spec.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { addTodo } from './pureFunctions';
|
||||
import { Store } from '../store';
|
||||
|
||||
describe('TodoApp reducers', () => {
|
||||
it('can add an item', () => {
|
||||
const state = <Store>{
|
||||
todos: {},
|
||||
filter: 'all'
|
||||
};
|
||||
|
||||
const newState = addTodo(state, 'item1');
|
||||
|
||||
const keys = Object.keys(newState.todos);
|
||||
|
||||
expect(newState).not.toBe(state);
|
||||
expect(keys.length).toBe(1);
|
||||
expect(newState.todos[keys[0]].label).toBe('item1');
|
||||
expect(newState.todos[keys[0]].completed).toBeFalsy();
|
||||
});
|
||||
});
|
||||
34
step2-06/src/reducers/pureFunctions.ts
Normal file
34
step2-06/src/reducers/pureFunctions.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Store } from '../store';
|
||||
|
||||
let index = 0;
|
||||
|
||||
export function addTodo(state: Store, label: string): Store {
|
||||
const { todos } = state;
|
||||
const id = index++;
|
||||
|
||||
return {
|
||||
...state,
|
||||
todos: { ...todos, [id]: { label, completed: false } }
|
||||
};
|
||||
}
|
||||
|
||||
export function remove(state: Store, id: string) {
|
||||
const newTodos = { ...state.todos };
|
||||
|
||||
delete newTodos[id];
|
||||
|
||||
return {
|
||||
...state,
|
||||
todos: newTodos
|
||||
};
|
||||
}
|
||||
|
||||
export function complete(state: Store, id: string) {
|
||||
const newTodos = { ...this.state.todos };
|
||||
newTodos[id].completed = !newTodos[id].completed;
|
||||
|
||||
return {
|
||||
...state,
|
||||
todos: newTodos
|
||||
};
|
||||
}
|
||||
14
step2-06/src/store/index.ts
Normal file
14
step2-06/src/store/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export type FilterTypes = 'all' | 'active' | 'completed';
|
||||
|
||||
export interface TodoItem {
|
||||
label: string;
|
||||
completed: boolean;
|
||||
}
|
||||
|
||||
export interface Store {
|
||||
todos: {
|
||||
[id: string]: TodoItem;
|
||||
};
|
||||
|
||||
filter: FilterTypes;
|
||||
}
|
||||
Reference in New Issue
Block a user