-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
60 lines (48 loc) · 1.54 KB
/
main.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
/* global require */
const { applyMiddleware, createStore, compose } = require("redux");
const { logger } = require("redux-logger");
const { createActions, handleActions } = require("redux-actions");
const { ProjectChangeListener } = require("./project");
const actions = createActions(
"USER_LOGGED_IN",
"PROJECT_SELECTED",
"RESOURCES_CHANGED",
"RESOURCE_SELECTED",
"DATA_SYNCHRONIZED"
);
const reducer = handleActions({
[actions.userLoggedIn]: (state, action) => ({
...state,
user: action.payload
}),
[actions.projectSelected]: (state, action) => ({
...state,
project: action.payload,
resourceSelected: undefined
}),
[actions.resourcesChanged]: (state, action) => ({
...state,
resources: [...Object.keys(action.payload.containers)].sort()
}),
[actions.resourceSelected]: (state, action) => ({
...state,
resourceSelected: action.payload
}),
[actions.dataSynchronized]: (state, action) => ({
...state,
transactions: state.transactions.concat([
action.payload
]).slice(-10)
})
}, {
user: undefined,
project: undefined,
resources: [],
resourceSelected: undefined,
transactions: []
});
const store = createStore(reducer, applyMiddleware(logger));
window.DM = Object.assign(window.DM || {}, { store });
const dispatch = store.dispatch.bind(store);
Object.keys(actions).forEach(k => window.DM[k] = compose(dispatch, actions[k]));
(new ProjectChangeListener()).subscribeTo(store);