-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstore.ts
57 lines (51 loc) · 1.16 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import * as Redux from 'redux';
interface Todo {
id: number;
title: string;
completed: boolean;
}
interface State {
todos: Todo[];
}
interface Action {
type: string,
id?: number,
title?: string,
completed?: boolean
}
let reduxStore,
initialState = {
todos: [
{ id: 0, title: 'Learn React Basics', completed: false }
]
};
function todoReducer(state: State, action: Action): State {
switch (action.type) {
case 'ADD_TODO':
return {
todos: state.todos.concat({ id: state.todos.length, title: action.title, completed: false })
};
case 'COMPLETE_TODO':
let completeTodo = function completeTodo(): State {
var startArray = state.todos.slice(0, action.id),
todo = state.todos[action.id],
endArray = state.todos.slice(action.id + 1, state.todos.length);
return {
todos: startArray.concat(
[{ id: todo.id, title: todo.title, completed: action.completed }],
endArray
)
}
};
return completeTodo();
default:
return state;
}
}
reduxStore = Redux.createStore(todoReducer, initialState);
reduxStore.subscribe(
function logStore() {
console.log(reduxStore.getState());
}
);
export default reduxStore;