-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalApp.js
65 lines (53 loc) · 1.25 KB
/
finalApp.js
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
58
59
60
61
62
63
64
65
const { createStore, applyMiddleware } = require('redux');
const logger = store => next => action => {
console.log('dispatching', action)
let result = next(action)
console.log('state after action', store.getState())
return result
}
const defaultState = {
courses: [
{
name: 'Learning React',
topic: 'React',
},
{
name: 'Learning Angular',
topic: 'Angular',
},
{
name: 'Using Redux with Angular',
topic: 'Angular',
}
]
};
function reducer(state, action) {
switch (action.type) {
case 'ADD_COURSE':
return Object.assign({}, state, {
courses: [...state.courses, action.course]
});
default:
return state;
}
}
const store = createStore(reducer, defaultState, applyMiddleware(logger));
function addView(viewFunc) {
viewFunc(store.getState());
store.subscribe(() => {
viewFunc(store.getState());
})
}
addView((state) => {
console.log(`There are ${state.courses.length} courses in the library`);
});
addView((state) => {
console.log(`The latest course in the library: ${state.courses[state.courses.length -1].name}`);
});
store.dispatch({
type: 'ADD_COURSE',
course: {
name: 'This is the new course',
topic: 'Really does not matter'
}
});